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

     1  package health
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-kit/log"
     7  	"github.com/go-kit/log/level"
     8  	"github.com/pkg/errors"
     9  	"google.golang.org/grpc"
    10  	"google.golang.org/grpc/health"
    11  	"google.golang.org/grpc/health/grpc_health_v1"
    12  )
    13  
    14  type Service interface {
    15  	SetServing()
    16  	SetNotServing()
    17  }
    18  
    19  type noopService struct{}
    20  
    21  var NoOpService = noopService{}
    22  
    23  func (noopService) SetServing() {}
    24  
    25  func (noopService) SetNotServing() {}
    26  
    27  type GRPCHealthService struct {
    28  	logger log.Logger
    29  	name   string
    30  	server *health.Server
    31  }
    32  
    33  func NewGRPCHealthService(server *health.Server, logger log.Logger, name string) *GRPCHealthService {
    34  	return &GRPCHealthService{
    35  		logger: logger,
    36  		name:   name,
    37  		server: server,
    38  	}
    39  }
    40  
    41  func (s *GRPCHealthService) setStatus(x grpc_health_v1.HealthCheckResponse_ServingStatus) {
    42  	level.Info(s.logger).Log("msg", "setting health status", "status", x)
    43  	s.server.SetServingStatus(s.name, x)
    44  }
    45  
    46  func (s *GRPCHealthService) SetServing() {
    47  	s.setStatus(grpc_health_v1.HealthCheckResponse_SERVING)
    48  }
    49  
    50  func (s *GRPCHealthService) SetNotServing() {
    51  	s.setStatus(grpc_health_v1.HealthCheckResponse_NOT_SERVING)
    52  }
    53  
    54  var NoOpClient = noOpClient{}
    55  
    56  type noOpClient struct{}
    57  
    58  func (noOpClient) Check(context.Context, *grpc_health_v1.HealthCheckRequest, ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
    59  	return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
    60  }
    61  
    62  func (noOpClient) List(ctx context.Context, in *grpc_health_v1.HealthListRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthListResponse, error) {
    63  	return nil, errors.New("not implemented")
    64  }
    65  
    66  func (noOpClient) Watch(context.Context, *grpc_health_v1.HealthCheckRequest, ...grpc.CallOption) (grpc_health_v1.Health_WatchClient, error) {
    67  	return nil, errors.New("not implemented")
    68  }