-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1103 from charlesmst/actuator-healthcheck
Actuator healthcheck
- Loading branch information
Showing
10 changed files
with
455 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ver-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/config/HealthOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2016-2024 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.devh.boot.grpc.server.config; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* GRPC Health service options. | ||
*/ | ||
@Data | ||
public class HealthOptions { | ||
|
||
/** | ||
* Implementation of gRPC health service. Defaults to {@link HealthType#GRPC GRPC}. To disable health service set to | ||
* {@link HealthType#NONE NONE}. | ||
* | ||
* @see net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration | ||
* @param type The implementation of gRPC health service. | ||
* @return GRPC, ACTUATOR or NONE. | ||
*/ | ||
private HealthType type = HealthType.GRPC; | ||
} |
41 changes: 41 additions & 0 deletions
41
...server-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/config/HealthType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2016-2024 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.devh.boot.grpc.server.config; | ||
|
||
|
||
/** | ||
* Enum to specify the type of health service to use in GRPC. | ||
*/ | ||
public enum HealthType { | ||
/** | ||
* Use the standard GRPC health service from io.grpc. | ||
* | ||
* @see net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration#grpcHealthService | ||
*/ | ||
GRPC, | ||
/** | ||
* Uses a bridge to the Spring Boot Actuator health service. | ||
* | ||
* @see net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration#grpcHealthServiceActuator | ||
* @see net.devh.boot.grpc.server.health.ActuatorGrpcHealth | ||
*/ | ||
ACTUATOR, | ||
/** | ||
* No health service will be created. | ||
*/ | ||
NONE | ||
} |
72 changes: 72 additions & 0 deletions
72
...pring-boot-starter/src/main/java/net/devh/boot/grpc/server/health/ActuatorGrpcHealth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2016-2024 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.devh.boot.grpc.server.health; | ||
|
||
|
||
import static io.grpc.Status.NOT_FOUND; | ||
|
||
import org.springframework.boot.actuate.health.HealthComponent; | ||
import org.springframework.boot.actuate.health.HealthEndpoint; | ||
import org.springframework.boot.actuate.health.Status; | ||
|
||
import io.grpc.StatusException; | ||
import io.grpc.health.v1.HealthCheckRequest; | ||
import io.grpc.health.v1.HealthCheckResponse; | ||
import io.grpc.health.v1.HealthGrpc; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
public class ActuatorGrpcHealth extends HealthGrpc.HealthImplBase { | ||
private final HealthEndpoint healthEndpoint; | ||
|
||
public ActuatorGrpcHealth(HealthEndpoint healthEndpoint) { | ||
this.healthEndpoint = healthEndpoint; | ||
} | ||
|
||
public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { | ||
|
||
if (!request.getService().isEmpty()) { | ||
HealthComponent health = healthEndpoint.healthForPath(request.getService()); | ||
if (health == null) { | ||
responseObserver.onError( | ||
new StatusException(NOT_FOUND.withDescription("unknown service " + request.getService()))); | ||
return; | ||
} | ||
Status status = health.getStatus(); | ||
HealthCheckResponse.ServingStatus result = resolveStatus(status); | ||
HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} else { | ||
Status status = healthEndpoint.health().getStatus(); | ||
HealthCheckResponse.ServingStatus result = resolveStatus(status); | ||
HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
} | ||
|
||
private HealthCheckResponse.ServingStatus resolveStatus(Status status) { | ||
if (Status.UP.equals(status)) { | ||
return HealthCheckResponse.ServingStatus.SERVING; | ||
} | ||
if (Status.DOWN.equals(status) || Status.OUT_OF_SERVICE.equals(status)) { | ||
return HealthCheckResponse.ServingStatus.NOT_SERVING; | ||
} | ||
return HealthCheckResponse.ServingStatus.UNKNOWN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.