github.com/grafana/pyroscope@v1.18.0/pkg/compactor/compactor_http.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/grafana/mimir/blob/main/pkg/compactor/compactor_http.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package compactor
     7  
     8  import (
     9  	_ "embed" // Used to embed html template
    10  	"html/template"
    11  	"net/http"
    12  
    13  	"github.com/go-kit/log/level"
    14  	"github.com/grafana/dskit/services"
    15  
    16  	util_log "github.com/grafana/pyroscope/pkg/util"
    17  )
    18  
    19  var (
    20  	//go:embed status.gohtml
    21  	statusPageHTML     string
    22  	statusPageTemplate = template.Must(template.New("main").Parse(statusPageHTML))
    23  )
    24  
    25  type statusPageContents struct {
    26  	Message string
    27  }
    28  
    29  func writeMessage(w http.ResponseWriter, message string) {
    30  	w.WriteHeader(http.StatusOK)
    31  	err := statusPageTemplate.Execute(w, statusPageContents{Message: message})
    32  	if err != nil {
    33  		level.Error(util_log.Logger).Log("msg", "unable to serve compactor ring page", "err", err)
    34  	}
    35  }
    36  
    37  func (c *MultitenantCompactor) RingHandler(w http.ResponseWriter, req *http.Request) {
    38  	if c.State() != services.Running {
    39  		// we cannot read the ring before MultitenantCompactor is in Running state,
    40  		// because that would lead to race condition.
    41  		writeMessage(w, "Compactor is not running yet.")
    42  		return
    43  	}
    44  
    45  	c.ring.ServeHTTP(w, req)
    46  }