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

CAMEL-17426: Prevent MicroProfile Health conflicting health data #6779

Merged
merged 1 commit into from
Jan 18, 2022
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
48 changes: 33 additions & 15 deletions components/camel-microprofile/camel-microprofile-health/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.health</groupId>
<artifactId>microprofile-health-api</artifactId>
<version>${microprofile-health-version}</version>
<scope>provided</scope>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-health</artifactId>
<version>${smallrye-health-version}</version>
</dependency>
<!-- smallrye health uses cdi api -->
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
<version>${smallrye-config-version}</version>
</dependency>

<!-- smallrye health uses CDI api -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>${cdi-api-2.0-version}</version>
<scope>provided</scope>
</dependency>

<!-- testing -->
Expand All @@ -64,17 +70,10 @@
<artifactId>camel-test-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-health</artifactId>
<version>${smallrye-health-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
<version>${smallrye-config-version}</version>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<version>${weld-junit5-version}</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -104,4 +103,23 @@
</dependency>

</dependencies>

<profiles>
<profile>
<id>jdk17-build</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ for this component:

== Usage

This component provides a custom `HealthCheckRegistry` implementation that needs to be registered on the `CamelContext`.
[source,java]
----
HealthCheckRegistry registry = new CamelMicroProfileHealthCheckRegistry();
camelContext.setExtension(HealthCheckRegistry.class, registry);
----

By default, Camel health checks are registered as both MicroProfile Health liveness and readiness checks. To have finer control over whether a Camel health check should
be considered either a readiness or liveness check, you can extend `AbstractHealthCheck` and override the `isLiveness()` and `isReadiness()` methods.

Expand Down Expand Up @@ -57,14 +64,3 @@ public class MyHealthCheck extends AbstractHealthCheck {
}
}
----

== Auto discovery

The expectation is that this component is run within a MicroProfile container, where CDI and a library implementing the MicroProfile health specification is available.
E.g https://github.com/smallrye/smallrye-health[SmallRye Health].

In this scenario, the readiness and liveness checks are automatically discovered and registered for you.

However, it's still possible to manually
register Health checks without CDI. Ensure your camel health checks are available in the Camel registry and add an instance of
`CamelMicroProfileReadinessCheck` and `CamelMicroProfileLivenessCheck` to the health check registry of your MicroProfile Health implementation.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.microprofile.health;

import java.util.Map;

import org.apache.camel.impl.health.AbstractHealthCheck;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;

import static org.apache.camel.health.HealthCheck.*;

/**
* A MicroProfile {@link HealthCheck} that invokes the supplied Camel health check, reports its health status and
* associated details.
*/
final class CamelMicroProfileHealthCheck implements HealthCheck {

private final org.apache.camel.health.HealthCheck camelHealthCheck;

CamelMicroProfileHealthCheck(org.apache.camel.health.HealthCheck camelHealthCheck) {
this.camelHealthCheck = camelHealthCheck;
}

@Override
public HealthCheckResponse call() {
final HealthCheckResponseBuilder builder = HealthCheckResponse.builder();
builder.name(camelHealthCheck.getId());
builder.up();

Result result = camelHealthCheck.call();
Map<String, Object> details = result.getDetails();
boolean enabled = true;

if (details.containsKey(AbstractHealthCheck.CHECK_ENABLED)) {
enabled = (boolean) details.get(AbstractHealthCheck.CHECK_ENABLED);
}

if (enabled) {
CamelMicroProfileHealthHelper.applyHealthDetail(builder, result);

if (result.getState() == State.DOWN) {
builder.down();
}
} else {
builder.withData(AbstractHealthCheck.CHECK_ENABLED, false);
}

return builder.build();
}
}
Loading