feat: add health check service to gRPC server
This commit is contained in:
parent
5f0ae449b2
commit
7a0b0f1540
@ -12,6 +12,8 @@ import (
|
|||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/health"
|
||||||
|
grpc_health_v1 "google.golang.org/grpc/health/grpc_health_v1"
|
||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
"google.golang.org/grpc/reflection"
|
"google.golang.org/grpc/reflection"
|
||||||
|
|
||||||
@ -79,12 +81,15 @@ func main() {
|
|||||||
grpc.ChainUnaryInterceptor(loggingUnaryInterceptor),
|
grpc.ChainUnaryInterceptor(loggingUnaryInterceptor),
|
||||||
grpc.ChainStreamInterceptor(loggingStreamInterceptor),
|
grpc.ChainStreamInterceptor(loggingStreamInterceptor),
|
||||||
)
|
)
|
||||||
|
healthSrv := health.NewServer()
|
||||||
|
healthSrv.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
||||||
|
|
||||||
// 7. Register services.
|
// 7. Register services.
|
||||||
hav1.RegisterEntityServiceServer(srv, grpcadapter.NewEntityGRPC(entityApp))
|
hav1.RegisterEntityServiceServer(srv, grpcadapter.NewEntityGRPC(entityApp))
|
||||||
hav1.RegisterLightServiceServer(srv, grpcadapter.NewLightGRPC(lightApp))
|
hav1.RegisterLightServiceServer(srv, grpcadapter.NewLightGRPC(lightApp))
|
||||||
hav1.RegisterSwitchServiceServer(srv, grpcadapter.NewSwitchGRPC(switchApp))
|
hav1.RegisterSwitchServiceServer(srv, grpcadapter.NewSwitchGRPC(switchApp))
|
||||||
hav1.RegisterEventServiceServer(srv, &grpcadapter.EventGRPC{})
|
hav1.RegisterEventServiceServer(srv, &grpcadapter.EventGRPC{})
|
||||||
|
grpc_health_v1.RegisterHealthServer(srv, healthSrv)
|
||||||
reflection.Register(srv)
|
reflection.Register(srv)
|
||||||
|
|
||||||
// 8. Start listener.
|
// 8. Start listener.
|
||||||
@ -107,6 +112,7 @@ func main() {
|
|||||||
slog.Info("shutting down")
|
slog.Info("shutting down")
|
||||||
|
|
||||||
// 11. Graceful stop, then flush telemetry.
|
// 11. Graceful stop, then flush telemetry.
|
||||||
|
healthSrv.SetServingStatus("", grpc_health_v1.HealthCheckResponse_NOT_SERVING)
|
||||||
srv.GracefulStop()
|
srv.GracefulStop()
|
||||||
|
|
||||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user