Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for HTTP health checks to gRPC example #87

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/webserver/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>io.grpc</groupId>
<artifactId>grpc-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.grpc</groupId>
<artifactId>helidon-grpc-core</artifactId>
Expand All @@ -54,6 +58,14 @@
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-grpc</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver.observe</groupId>
<artifactId>helidon-webserver-observe-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health-checks</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.helidon.examples.webserver.grpc;

import io.helidon.config.Config;
import io.helidon.logging.common.LogConfig;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.grpc.GrpcRouting;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.health.HealthObserver;

class GrpcMain {

Expand All @@ -33,9 +36,24 @@ private GrpcMain() {
public static void main(String[] args) {
LogConfig.configureRuntime();

// initialize global config from default configuration
Config config = Config.create();
Config.global(config);
Config serverConfig = config.get("server");

// create a health check to verify gRPC endpoint
ObserveFeature observe = ObserveFeature.builder()
.addObserver(HealthObserver.builder()
.details(true)
spericas marked this conversation as resolved.
Show resolved Hide resolved
.addCheck(new StringServiceHealthCheck(serverConfig))
.build())
.build();

// start server and register gRPC routing and health check
WebServer.builder()
.port(8080)
.config(serverConfig)
.addRouting(GrpcRouting.builder().service(new StringService()))
.addFeature(observe)
.build()
.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.examples.webserver.grpc;

import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.config.Config;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
import io.helidon.health.HealthCheckType;
import io.helidon.webclient.api.WebClient;
import io.helidon.webclient.grpc.GrpcClient;

class StringServiceHealthCheck implements HealthCheck {

private final WebClient webClient;

StringServiceHealthCheck(Config config) {
Tls clientTls = Tls.builder()
.trust(trust -> trust
.keystore(store -> store
.passphrase("password")
.trustStore(true)
.keystore(Resource.create("client.p12"))))
.build();
int serverPort = config.get("port").asInt().get();
this.webClient = WebClient.builder()
.tls(clientTls)
.baseUri("https://localhost:" + serverPort)
.build();
}

@Override
public String name() {
return StringService.class.getSimpleName();
}

@Override
public HealthCheckType type() {
return HealthCheckType.READINESS;
}

/**
* Self-invocation to verify gRPC endpoint is available and ready.
*
* @return health check response
*/
@Override
public HealthCheckResponse call() {
try {
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
StringServiceGrpc.StringServiceBlockingStub service =
StringServiceGrpc.newBlockingStub(grpcClient.channel());
Strings.StringMessage res = service.upper(
Strings.StringMessage.newBuilder().setText("hello").build());
spericas marked this conversation as resolved.
Show resolved Hide resolved
return HealthCheckResponse.builder()
.status(res.getText().equals("HELLO"))
.get();
} catch (Exception e) {
return HealthCheckResponse.builder()
.status(false)
.get();
}
}
}
7 changes: 6 additions & 1 deletion examples/webserver/grpc/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

server:
port: 0
port: 8080
tls:
trust:
keystore:
Expand All @@ -28,3 +28,8 @@ server:
passphrase: "password"
resource:
resource-path: "server.p12"
features:
observe:
observers:
health:
details: true
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.http.Status;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;
import io.helidon.webclient.grpc.GrpcClient;
import io.helidon.webserver.Router;
Expand All @@ -38,7 +40,9 @@
import io.grpc.stub.StreamObserver;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand Down Expand Up @@ -132,6 +136,19 @@ void testUnaryUpperInterceptor() {
assertThat(res.getText(), is("[[HELLO]]"));
}

/**
* Tests server health using HTTP, not gRPC.
*/
@Test
void testHealthHttp() {
try (HttpClientResponse res = webClient.get("/observe/health").request()) {
assertThat(res.status(), is(Status.OK_200));
String value = res.as(String.class);
assertThat(value, containsString("UP"));
assertThat(value, not(containsString("DOWN")));
}
}

static Strings.StringMessage newStringMessage(String data) {
return Strings.StringMessage.newBuilder().setText(data).build();
}
Expand Down
5 changes: 5 additions & 0 deletions examples/webserver/grpc/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ server:
passphrase: "password"
resource:
resource-path: "server.p12"
features:
observe:
observers:
health:
details: true

grpc-client:
poll-wait-time: PT30S
Expand Down