-
Notifications
You must be signed in to change notification settings - Fork 158
Add Microprofile Health #25186
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
Merged
Merged
Add Microprofile Health #25186
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
dd7c5e4
Initial implementation
Thihup a1c4b38
Update version
Thihup 0490f26
Fix TCK tests
Thihup 7be24a9
Fix Checkstyle issue
Thihup 3b9cc17
Ignore Microprofile Config if it is not enabled
Thihup 1ec8dd8
Remove unused system properties
Thihup f0b52b8
Remove unused method
Thihup b2842fe
Remove unused Import-Package declarations
Thihup cfab442
Fix checkstyle
Thihup 84b51ef
Move config file from META-INF to WEB-INF/classes/META-INF
Thihup 6f5fbdf
Remove unused BeansXmlTransformer
Thihup c21a583
Improve error handling
Thihup 1df0d36
Rename Arquillian extension
Thihup 7d13b7d
Remove unused archive processor
Thihup 2d80b6d
Add license
Thihup 88fcd1e
Rename variables
Thihup 75ad0f6
Remove unused Helidon Health dependency
Thihup ef217b5
Merge branch 'master' of https://github.com/eclipse-ee4j/glassfish in…
Thihup 2b2e6e8
Update version
Thihup 9bea997
CI
Thihup 4f2fc1e
Remove unused internal dependencies
Thihup 87035d1
Avoid Impl suffix
Thihup 35bb755
Add system property to allow disabling the Health servlet
Thihup 96528c9
Split Health implementation
Thihup c8ceff3
Merge branch 'master' of https://github.com/eclipse-ee4j/glassfish in…
Thihup c1355d9
Update version
Thihup f80f757
Merge branch 'master' of https://github.com/eclipse-ee4j/glassfish in…
Thihup 67e74ef
Fix imports
Thihup f629daa
Fix imports
Thihup f3052c6
Fix imports
Thihup eb5af30
Set content type and encoding for health response body
OndroMih 62ae7a0
Feedback from review
Thihup 1d978a3
Merge remote-tracking branch 'origin/microprofile-health' into microp…
Thihup 2b3b195
Rename package
Thihup 15cc213
Remove Export-Package instruction
Thihup 6843a6c
Get application name from the InvocationManager
Thihup 2cdb35e
Merge branch 'master' of https://github.com/eclipse-ee4j/glassfish in…
Thihup 09f1833
Version 7.0.23-SNAPSHOT
Thihup 26bcf7c
Remove unused condition
Thihup ca9355e
Fix checkstyle error
Thihup 4094cdf
Avoid NPE
Thihup 524ff27
Correctly get the InvocationManager
Thihup b44368c
Filter correct HealthChecks
Thihup 83f501d
Fix Checkstyle warnings
Thihup c33c15f
Remove unused Cleaner
Thihup d52d37b
Remove CDI.destroy call
Thihup dcd9d88
Use EnumSet instead of HashSet
Thihup c38df5b
Use bean reference to create HealthCheck instances
Thihup 8d3d903
Set context class loader for health check execution
Thihup 6ce625a
Fix formatting
Thihup f33de39
Fix checkstyle warnings
Thihup bdf5e22
MP Health: Add context name to response
OndroMih 08b8c52
Merge pull request #1 from OndroMih/ondromih-mp-health-app-context
Thihup e448ac8
Merge branch 'master' of https://github.com/eclipse-ee4j/glassfish in…
Thihup 7e69293
Merge branch 'master' into microprofile-health
OndroMih cdaaf77
Update version to 7.1.0-SNAPSHOT
OndroMih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
...ver/distributions/glassfish/src/main/patches/microprofile-health-api/META-INF/MANIFEST.MF
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-Description: MicroProfile Health :: API | ||
Bundle-License: Apache License, Version 2.0 | ||
Bundle-SymbolicName: org.eclipse.microprofile.health | ||
Bnd-LastModified: 1664892780713 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-DocURL: https://microprofile.io/project/eclipse/microprofile-he | ||
alth/microprofile-health-api | ||
Bundle-Vendor: Eclipse Foundation | ||
Import-Package: jakarta.enterprise.util;version="[3.0,5)",jakarta.inje | ||
ct;version="[2.0,4)",org.eclipse.microprofile.health;version="[3.0,5) | ||
",org.eclipse.microprofile.health.spi;version="[1.1,2)" | ||
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))" | ||
Tool: Bnd-5.2.0.202010142003 | ||
Export-Package: org.eclipse.microprofile.health;version="3.0";uses:="j | ||
akarta.enterprise.util,jakarta.inject,org.eclipse.microprofile.health | ||
.spi",org.eclipse.microprofile.health.spi;version="1.1";uses:="org.ec | ||
lipse.microprofile.health" | ||
Bundle-Name: MicroProfile Health Check Bundle | ||
Bundle-Version: 4.0.1 | ||
Bundle-SCM: url="https://github.com/eclipse/microprofile-health/microp | ||
rofile-health-api",connection="scm:git:https://github.com/eclipse/mic | ||
roprofile-health.git/microprofile-health-api",developer-connection="s | ||
cm:git:[email protected]:eclipse/microprofile-health.git/microprofile-he | ||
alth-api",tag="4.0.1" | ||
Build-Jdk-Spec: 1.8 | ||
Created-By: 1.8.0_292 (AdoptOpenJDK) | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.glassfish.main.microprofile</groupId> | ||
<artifactId>microprofile-parent</artifactId> | ||
<version>7.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>health-glassfish</artifactId> | ||
<packaging>glassfish-jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.glassfish.main.microprofile</groupId> | ||
<artifactId>microprofile-health</artifactId> | ||
<version>${project.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.json.bind</groupId> | ||
<artifactId>jakarta.json.bind-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.enterprise</groupId> | ||
<artifactId>jakarta.enterprise.cdi-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<release>17</release> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
<configuration> | ||
<supportedProjectTypes> | ||
<supportedProjectType>glassfish-jar</supportedProjectType> | ||
</supportedProjectTypes> | ||
<instructions> | ||
<Include-Resource> | ||
{maven-resources}, | ||
META-INF/hk2-locator/=target/classes/META-INF/hk2-locator/, | ||
</Include-Resource> | ||
</instructions> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>osgi-bundle</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>bundle</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
99 changes: 99 additions & 0 deletions
99
...src/main/java/org/glassfish/microprofile/health/service/CollectHealthChecksExtension.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to Eclipse Foundation. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package org.glassfish.microprofile.health.service; | ||
|
||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.spi.AfterDeploymentValidation; | ||
import jakarta.enterprise.inject.spi.Annotated; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.enterprise.inject.spi.Extension; | ||
import jakarta.enterprise.inject.spi.ProcessBean; | ||
|
||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
|
||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.Liveness; | ||
import org.eclipse.microprofile.health.Readiness; | ||
import org.eclipse.microprofile.health.Startup; | ||
import org.glassfish.api.invocation.InvocationManager; | ||
import org.glassfish.hk2.api.ServiceLocator; | ||
import org.glassfish.hk2.utilities.ServiceLocatorUtilities; | ||
import org.glassfish.internal.api.Globals; | ||
import org.glassfish.microprofile.health.HealthCheckInfo; | ||
import org.glassfish.microprofile.health.HealthReporter; | ||
|
||
|
||
public class CollectHealthChecksExtension implements Extension { | ||
private final HealthReporter service; | ||
private final Set<HealthCheckBeanAndKind> healthChecks = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
private final InvocationManager invocationManager; | ||
|
||
record HealthCheckBeanAndKind(Bean<HealthCheck> bean, Set<HealthCheckInfo.Kind> kind) { | ||
} | ||
|
||
public CollectHealthChecksExtension() { | ||
ServiceLocator defaultBaseServiceLocator = Globals.getDefaultBaseServiceLocator(); | ||
invocationManager = Globals.get(InvocationManager.class); | ||
HealthReporter healthReporterService = defaultBaseServiceLocator.getService(HealthReporter.class); | ||
if (healthReporterService == null) { | ||
ServiceLocatorUtilities.addClasses(defaultBaseServiceLocator, true, HealthReporter.class); | ||
healthReporterService = defaultBaseServiceLocator.getService(HealthReporter.class); | ||
} | ||
service = healthReporterService; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> void processBeans(@Observes ProcessBean<T> beans) { | ||
Annotated annotated = beans.getAnnotated(); | ||
boolean livenessPresent = annotated.isAnnotationPresent(Liveness.class); | ||
boolean readinessPresent = annotated.isAnnotationPresent(Readiness.class); | ||
boolean startupPresent = annotated.isAnnotationPresent(Startup.class); | ||
if (livenessPresent || readinessPresent || startupPresent) { | ||
Bean<T> bean = beans.getBean(); | ||
if (bean.getTypes().contains(HealthCheck.class)) { | ||
Set<HealthCheckInfo.Kind> kinds = EnumSet.noneOf(HealthCheckInfo.Kind.class); | ||
if (livenessPresent) { | ||
kinds.add(HealthCheckInfo.Kind.LIVE); | ||
} | ||
if (readinessPresent) { | ||
kinds.add(HealthCheckInfo.Kind.READY); | ||
} | ||
if (startupPresent) { | ||
kinds.add(HealthCheckInfo.Kind.STARTUP); | ||
} | ||
|
||
healthChecks.add(new HealthCheckBeanAndKind((Bean<HealthCheck>) bean, kinds)); | ||
} | ||
} | ||
} | ||
|
||
public void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager beanManager) { | ||
healthChecks.forEach(healthCheckBeanAndKind -> { | ||
Bean<HealthCheck> bean = healthCheckBeanAndKind.bean(); | ||
CreationalContext<HealthCheck> creationalContext = beanManager.createCreationalContext(bean); | ||
if (beanManager.getReference(bean, HealthCheck.class, creationalContext) instanceof HealthCheck healthCheck) { | ||
service.addHealthCheck(invocationManager.getCurrentInvocation().getAppName(), | ||
new HealthCheckInfo(healthCheck, healthCheckBeanAndKind.kind())); | ||
} | ||
}); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...alth-glassfish/src/main/java/org/glassfish/microprofile/health/service/HealthService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to Eclipse Foundation. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package org.glassfish.microprofile.health.service; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.glassfish.api.StartupRunLevel; | ||
import org.glassfish.api.event.EventListener; | ||
import org.glassfish.api.event.Events; | ||
import org.glassfish.hk2.api.PostConstruct; | ||
import org.glassfish.hk2.runlevel.RunLevel; | ||
import org.glassfish.internal.api.Globals; | ||
import org.glassfish.internal.data.ApplicationInfo; | ||
import org.glassfish.internal.deployment.Deployment; | ||
import org.glassfish.microprofile.health.HealthReporter; | ||
import org.jvnet.hk2.annotations.Service; | ||
|
||
@Service(name = "healthcheck-service") | ||
@RunLevel(StartupRunLevel.VAL) | ||
public class HealthService implements EventListener, PostConstruct { | ||
|
||
@Inject | ||
Events events; | ||
|
||
@Override | ||
public void postConstruct() { | ||
events.register(this); | ||
} | ||
|
||
@Override | ||
public void event(Event<?> event) { | ||
|
||
HealthReporter service = Globals.getDefaultHabitat().getService(HealthReporter.class); | ||
|
||
if (service == null) { | ||
return; | ||
} | ||
|
||
if (event.is(Deployment.APPLICATION_UNLOADED) && event.hook() instanceof ApplicationInfo appInfo) { | ||
service.removeAllHealthChecksFrom(appInfo.getName()); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...alth-glassfish/src/main/java/org/glassfish/microprofile/health/service/HealthServlet.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to Eclipse Foundation. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package org.glassfish.microprofile.health.service; | ||
|
||
import jakarta.json.bind.Jsonb; | ||
import jakarta.json.bind.JsonbBuilder; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.glassfish.internal.api.Globals; | ||
import org.glassfish.microprofile.health.HealthReport; | ||
import org.glassfish.microprofile.health.HealthReporter; | ||
|
||
public class HealthServlet extends HttpServlet { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(HealthServlet.class.getName()); | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
HealthReport healthReport; | ||
try { | ||
healthReport = Globals.getDefaultHabitat().getService(HealthReporter.class) | ||
.getReport(getReportKind(req.getRequestURI())); | ||
|
||
int httpStatus = switch (healthReport.status()) { | ||
case UP -> HttpServletResponse.SC_OK; | ||
case DOWN -> HttpServletResponse.SC_SERVICE_UNAVAILABLE; | ||
}; | ||
resp.setStatus(httpStatus); | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Unable to fetch health check status", e); | ||
healthReport = new HealthReport(HealthCheckResponse.Status.DOWN, List.of()); | ||
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
Jsonb jsonb = JsonbBuilder.create(); | ||
String json = jsonb.toJson(healthReport); | ||
resp.setCharacterEncoding("UTF-8"); | ||
resp.setContentType("application/json"); | ||
resp.getWriter().println(json); | ||
} | ||
|
||
private static HealthReporter.ReportKind getReportKind(String path) { | ||
if (path.endsWith("health/live")) { | ||
return HealthReporter.ReportKind.LIVE; | ||
} else if (path.endsWith("health/ready")) { | ||
return HealthReporter.ReportKind.READY; | ||
} else if (path.endsWith("health/started")) { | ||
return HealthReporter.ReportKind.STARTED; | ||
} else { | ||
return HealthReporter.ReportKind.ALL; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.