WithHealthEndpointAt returns a ServeMuxOption that will add an endpoint to the created ServeMux at the path specified by endpointPath. When called the handler will forward the request to the upstream grpc service health check (defined in the gRPC Health Checking Protocol). See here https://grpc-eco
(healthCheckClient grpc_health_v1.HealthClient, endpointPath string)
| 301 | // |
| 302 | // If you define a service as query parameter, this will also be forwarded as service in the HealthCheckRequest. |
| 303 | func WithHealthEndpointAt(healthCheckClient grpc_health_v1.HealthClient, endpointPath string) ServeMuxOption { |
| 304 | return func(s *ServeMux) { |
| 305 | // error can be ignored since pattern is definitely valid |
| 306 | _ = s.HandlePath( |
| 307 | http.MethodGet, endpointPath, func(w http.ResponseWriter, r *http.Request, _ map[string]string, |
| 308 | ) { |
| 309 | _, outboundMarshaler := MarshalerForRequest(s, r) |
| 310 | annotatedContext, err := AnnotateContext(r.Context(), s, r, grpc_health_v1.Health_Check_FullMethodName, WithHTTPPathPattern(endpointPath)) |
| 311 | if err != nil { |
| 312 | s.errorHandler(r.Context(), s, outboundMarshaler, w, r, err) |
| 313 | return |
| 314 | } |
| 315 | |
| 316 | var md ServerMetadata |
| 317 | resp, err := healthCheckClient.Check(annotatedContext, &grpc_health_v1.HealthCheckRequest{ |
| 318 | Service: r.URL.Query().Get("service"), |
| 319 | }, grpc.Header(&md.HeaderMD), grpc.Trailer(&md.TrailerMD)) |
| 320 | annotatedContext = NewServerMetadataContext(annotatedContext, md) |
| 321 | if err != nil { |
| 322 | s.errorHandler(annotatedContext, s, outboundMarshaler, w, r, err) |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | w.Header().Set("Content-Type", "application/json") |
| 327 | |
| 328 | if resp.GetStatus() != grpc_health_v1.HealthCheckResponse_SERVING { |
| 329 | switch resp.GetStatus() { |
| 330 | case grpc_health_v1.HealthCheckResponse_NOT_SERVING, grpc_health_v1.HealthCheckResponse_UNKNOWN: |
| 331 | err = status.Error(codes.Unavailable, resp.String()) |
| 332 | case grpc_health_v1.HealthCheckResponse_SERVICE_UNKNOWN: |
| 333 | err = status.Error(codes.NotFound, resp.String()) |
| 334 | } |
| 335 | |
| 336 | s.errorHandler(annotatedContext, s, outboundMarshaler, w, r, err) |
| 337 | return |
| 338 | } |
| 339 | |
| 340 | _ = outboundMarshaler.NewEncoder(w).Encode(resp) |
| 341 | }) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // WithHealthzEndpoint returns a ServeMuxOption that will add a /healthz endpoint to the created ServeMux. |
| 346 | // |