Last active
January 26, 2021 12:49
-
-
Save rponte/200f18ef7ecefc5b7b3bdcfc29d5c758 to your computer and use it in GitHub Desktop.
GRPC: simple and dirty bugfix on Micronaut GrpcServerHealthIndicator
This file contains hidden or 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
| package br.com.zup.edu.bugfix; | |
| import io.micronaut.context.annotation.Replaces; | |
| import io.micronaut.core.async.publisher.AsyncSingleResultPublisher; | |
| import io.micronaut.grpc.server.GrpcEmbeddedServer; | |
| import io.micronaut.grpc.server.health.GrpcServerHealthIndicator; | |
| import io.micronaut.health.HealthStatus; | |
| import io.micronaut.management.health.indicator.HealthResult; | |
| import org.reactivestreams.Publisher; | |
| import javax.inject.Singleton; | |
| import java.util.Map; | |
| import static io.micronaut.core.util.CollectionUtils.mapOf; | |
| @Singleton | |
| @Replaces(GrpcServerHealthIndicator.class) | |
| public class CustomGrpcServerHealthIndicator extends GrpcServerHealthIndicator { | |
| private static final String ID = "grpc-server"; | |
| private final GrpcEmbeddedServer server; | |
| public CustomGrpcServerHealthIndicator(GrpcEmbeddedServer server) { | |
| super(server); | |
| this.server = server; | |
| } | |
| @Override | |
| public Publisher<HealthResult> getResult() { | |
| return new AsyncSingleResultPublisher<>(this::getHealthResult); | |
| } | |
| private HealthResult getHealthResult() { | |
| final HealthStatus healthStatus = server.isRunning() ? HealthStatus.UP : HealthStatus.DOWN; | |
| /** | |
| * BUGFIX: it avoids to call the server.getPort() method when the gRPC-Server is DOWN because | |
| * it throws an unexpected exception that breaks the /health endpoint | |
| */ | |
| final String serverHost = server.getHost(); | |
| final int serverPort = server.getServerConfiguration().getServerPort(); // don't call the server.getPort() here! | |
| final Map details = mapOf("host", serverHost, "port", serverPort); | |
| return HealthResult | |
| .builder(ID, healthStatus) | |
| .details(details) | |
| .build(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this bug was fixed in this issue by me