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

4.x Add a new addCheck variant allowing the caller to set the health check name; add tests; revise doc #7994

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 20 additions & 14 deletions docs/se/health.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ A health check is a Java functional interface that returns a
[source,java]
.Health check with a lambda expression:
----
HealthCheck hc = () -> HealthCheckResponse
Supplier<HealthCheckResponse> hcr = () -> HealthCheckResponse
.builder()
.detail("exampleCheck", "looking-good")
.detail("exampleValue", "looking-good")
.status(HealthCheckResponse.Status.UP) // always "up"
.build();
----
Expand All @@ -106,12 +106,12 @@ HealthCheck hc = () -> HealthCheckResponse
static HealthCheckResponse exampleHealthCheck() {
return HealthCheckResponse
.builder()
.detail("exampleHealthCheck", "looking-good")
.detail("exampleValue", "looking-good")
.status(HealthCheckResponse.Status.UP) // always "up"
.build();
}

HealthCheck hc = Main::exampleHealthCheck;
Supplier<HealthCheckResponse> hcr = Main::exampleHealthCheck;
----
A real health check would compute its status dynamically based on the conditions in the server rather than hard-coding a fixed value.

Expand Down Expand Up @@ -148,18 +148,24 @@ instance of `HealthObserver`:
----
WebServer server = WebServer.builder()
.config(config.get("server"))
.addFeature(ObserveFeature.create(HealthObserver.builder() // <1>
.addCheck(hc) // <2>
.details(true) // <3>
.build())) // <4>
.addFeature(ObserveFeature.create(HealthObserver.builder() // <1>
.addCheck(hcr, // <2>
HealthCheckType.READINESS, // <3>
"exampleCheck") // <4>
.details(true) // <5>
.build())) // <6>
.routing(Main::routing)
.build()
.start();
----
<1> Create a builder for the `HealthObserver`.
<2> Add the custom health check to the `HealthObserver.Builder`. Invoke `addCheck` multiple times to add multiple custom health checks.
<3> Set detailed HTTP response output to `true`.
<4> Build the `HealthObserver` so it can be added to the observability feature that is registered with the webserver.
<2> Create and add a custom health check to the builder, passing the supplier of the health check response (the `hcr` variable in this example).
<3> Set the type of health check.
<4> Set the name of the health check.
<5> Set the health observer to include details in the output. (optional)
<6> Build the `HealthObserver` so it can be added to the observability feature that is registered with the webserver.

Invoke `addCheck` multiple times to add multiple custom health checks.

Here is a sample response to the custom health check registered above:

Expand All @@ -169,11 +175,11 @@ Here is a sample response to the custom health check registered above:
{
"status": "UP",
"checks": [
{
"name": "Main$$Lambda/0x00000001320ac800",
{
"name": "exampleCheck",
"status": "UP",
"data": {
"exampleHealthCheck": "looking-good"
"exampleValue": "looking-good"
}
},
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.helidon.webserver.observe.health;

import java.util.function.Supplier;

import io.helidon.builder.api.Prototype;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
Expand Down Expand Up @@ -46,6 +48,33 @@ static void addCheck(HealthObserverConfig.BuilderBase<?, ?> builder, HealthCheck
}
}

/**
* Add a health check using the provided response supplier, type, and name.
*
* @param builder required for the custom method
* @param responseSupplier supplier of the health check response
* @param type type to use
* @param name name to use for the health check
*/
@Prototype.BuilderMethod
static void addCheck(HealthObserverConfig.BuilderBase<?, ?> builder,
Supplier<HealthCheckResponse> responseSupplier,
HealthCheckType type,
String name) {
addCheck(builder, new HealthCheck() {
@Override
public HealthCheckResponse call() {
return responseSupplier.get();
}

@Override
public String name() {
return name;
}
},
type);
}

/**
* Add the provided health checks.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2023 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.webserver.observe.health;

import java.util.Optional;

import io.helidon.common.testing.junit5.OptionalMatcher;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
import io.helidon.health.HealthCheckType;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;

class TestNamedResponseSupplier {


private static final String NAMED_CHECK_NAME = "testCheck";
@Test
void checkForName() {
HealthObserver healthObserver = HealthObserver.builder()
.addCheck(() -> HealthCheckResponse.builder() // Tests the newly-added method for fixing issue #7827
.status(true)
.build(),
HealthCheckType.READINESS,
NAMED_CHECK_NAME)
.addCheck(() -> HealthCheckResponse.builder() // Tests the pre-existing behavior
.status(true)
.build(),
HealthCheckType.LIVENESS)
.build();

Optional<HealthCheck> namedCheck = healthObserver.prototype()
.healthChecks()
.stream()
.filter(hc -> hc.name().equals(NAMED_CHECK_NAME))
.findFirst();

assertThat("Named check via supplier", namedCheck, OptionalMatcher.optionalPresent());

Optional<HealthCheck> unnamedCheck = healthObserver.prototype()
.healthChecks()
.stream()
.filter(hc -> !hc.name().equals(NAMED_CHECK_NAME))
.findFirst();

assertThat("Unnamed check via supplier", unnamedCheck, OptionalMatcher.optionalPresent());
}
}