|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2024 The gRPC-Spring Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.devh.boot.grpc.server.health; |
| 18 | + |
| 19 | + |
| 20 | +import static io.grpc.Status.NOT_FOUND; |
| 21 | + |
| 22 | +import org.springframework.boot.actuate.health.HealthComponent; |
| 23 | +import org.springframework.boot.actuate.health.HealthEndpoint; |
| 24 | +import org.springframework.boot.actuate.health.Status; |
| 25 | + |
| 26 | +import io.grpc.StatusException; |
| 27 | +import io.grpc.health.v1.HealthCheckRequest; |
| 28 | +import io.grpc.health.v1.HealthCheckResponse; |
| 29 | +import io.grpc.health.v1.HealthGrpc; |
| 30 | +import io.grpc.stub.StreamObserver; |
| 31 | + |
| 32 | +public class ActuatorGrpcHealth extends HealthGrpc.HealthImplBase { |
| 33 | + private final HealthEndpoint healthEndpoint; |
| 34 | + |
| 35 | + public ActuatorGrpcHealth(HealthEndpoint healthEndpoint) { |
| 36 | + this.healthEndpoint = healthEndpoint; |
| 37 | + } |
| 38 | + |
| 39 | + public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { |
| 40 | + |
| 41 | + if (!request.getService().isEmpty()) { |
| 42 | + HealthComponent health = healthEndpoint.healthForPath(request.getService()); |
| 43 | + if (health == null) { |
| 44 | + responseObserver.onError( |
| 45 | + new StatusException(NOT_FOUND.withDescription("unknown service " + request.getService()))); |
| 46 | + return; |
| 47 | + } |
| 48 | + Status status = health.getStatus(); |
| 49 | + HealthCheckResponse.ServingStatus result = resolveStatus(status); |
| 50 | + HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build(); |
| 51 | + responseObserver.onNext(response); |
| 52 | + responseObserver.onCompleted(); |
| 53 | + } else { |
| 54 | + Status status = healthEndpoint.health().getStatus(); |
| 55 | + HealthCheckResponse.ServingStatus result = resolveStatus(status); |
| 56 | + HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build(); |
| 57 | + responseObserver.onNext(response); |
| 58 | + responseObserver.onCompleted(); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private HealthCheckResponse.ServingStatus resolveStatus(Status status) { |
| 64 | + if (Status.UP.equals(status)) { |
| 65 | + return HealthCheckResponse.ServingStatus.SERVING; |
| 66 | + } |
| 67 | + if (Status.DOWN.equals(status) || Status.OUT_OF_SERVICE.equals(status)) { |
| 68 | + return HealthCheckResponse.ServingStatus.NOT_SERVING; |
| 69 | + } |
| 70 | + return HealthCheckResponse.ServingStatus.UNKNOWN; |
| 71 | + } |
| 72 | +} |
0 commit comments