github.com/grafana/pyroscope@v1.18.0/pkg/storegateway/gateway_ring_http.go (about)

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