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

     1  package pyroscope
     2  
     3  import (
     4  	"context"
     5  
     6  	"connectrpc.com/grpchealth"
     7  	"github.com/gorilla/mux"
     8  	"github.com/grafana/dskit/grpcutil"
     9  )
    10  
    11  type checker struct {
    12  	checks []grpcutil.Check
    13  }
    14  
    15  func RegisterHealthServer(mux *mux.Router, checks ...grpcutil.Check) {
    16  	prefix, handler := grpchealth.NewHandler(&checker{checks: checks})
    17  	mux.NewRoute().PathPrefix(prefix).Handler(handler)
    18  }
    19  
    20  func (c *checker) Check(ctx context.Context, req *grpchealth.CheckRequest) (*grpchealth.CheckResponse, error) {
    21  	for _, check := range c.checks {
    22  		if !check(ctx) {
    23  			return &grpchealth.CheckResponse{Status: grpchealth.StatusNotServing}, nil
    24  		}
    25  	}
    26  	return &grpchealth.CheckResponse{Status: grpchealth.StatusServing}, nil
    27  }