github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/compactor/compactor_http.go (about)

     1  package compactor
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  
     7  	"github.com/go-kit/log/level"
     8  	"github.com/grafana/dskit/services"
     9  
    10  	util_log "github.com/cortexproject/cortex/pkg/util/log"
    11  )
    12  
    13  var (
    14  	compactorStatusPageTemplate = template.Must(template.New("main").Parse(`
    15  	<!DOCTYPE html>
    16  	<html>
    17  		<head>
    18  			<meta charset="UTF-8">
    19  			<title>Cortex Compactor Ring</title>
    20  		</head>
    21  		<body>
    22  			<h1>Cortex Compactor Ring</h1>
    23  			<p>{{ .Message }}</p>
    24  		</body>
    25  	</html>`))
    26  )
    27  
    28  func writeMessage(w http.ResponseWriter, message string) {
    29  	w.WriteHeader(http.StatusOK)
    30  	err := compactorStatusPageTemplate.Execute(w, struct {
    31  		Message string
    32  	}{Message: message})
    33  
    34  	if err != nil {
    35  		level.Error(util_log.Logger).Log("msg", "unable to serve compactor ring page", "err", err)
    36  	}
    37  }
    38  
    39  func (c *Compactor) RingHandler(w http.ResponseWriter, req *http.Request) {
    40  	if !c.compactorCfg.ShardingEnabled {
    41  		writeMessage(w, "Compactor has no ring because sharding is disabled.")
    42  		return
    43  	}
    44  
    45  	if c.State() != services.Running {
    46  		// we cannot read the ring before Compactor is in Running state,
    47  		// because that would lead to race condition.
    48  		writeMessage(w, "Compactor is not running yet.")
    49  		return
    50  	}
    51  
    52  	c.ring.ServeHTTP(w, req)
    53  }