Skip to content

Commit 08b8c52

Browse files
authored
Merge pull request #1 from OndroMih/ondromih-mp-health-app-context
MP Health: Add context name to response
2 parents f33de39 + bdf5e22 commit 08b8c52

File tree

3 files changed

+100
-20
lines changed

3 files changed

+100
-20
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
package org.glassfish.microprofile.health;
17+
18+
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
24+
import org.eclipse.microprofile.health.HealthCheckResponse;
25+
26+
/**
27+
*
28+
* @author Ondro Mihalyi
29+
*/
30+
public class GlassFishHealthCheckResponse extends HealthCheckResponse {
31+
32+
private Map<String, Object> data;
33+
34+
public GlassFishHealthCheckResponse(String name, Status status, Optional<Map<String, Object>> data) {
35+
super(name, status, null);
36+
this.data = data.orElse(null);
37+
}
38+
39+
public GlassFishHealthCheckResponse() {
40+
this(null, null, null);
41+
}
42+
43+
public GlassFishHealthCheckResponse addData(String key, Object value) {
44+
if (data == null) {
45+
data = new HashMap<>();
46+
}
47+
data.put(key, value);
48+
return this;
49+
}
50+
51+
@Override
52+
public Optional<Map<String, Object>> getData() {
53+
return Optional.ofNullable(data);
54+
}
55+
56+
57+
58+
}

appserver/microprofile/health/src/main/java/org/glassfish/microprofile/health/GlassFishHealthCheckResponseBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public HealthCheckResponseBuilder status(boolean up) {
7070

7171
@Override
7272
public HealthCheckResponse build() {
73-
return new HealthCheckResponse(name, status, data.isEmpty() ? null : Optional.of(data));
73+
return new GlassFishHealthCheckResponse(name, status, data.isEmpty() ? Optional.empty() : Optional.of(data));
7474
}
7575
}

appserver/microprofile/health/src/main/java/org/glassfish/microprofile/health/HealthReporter.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import jakarta.inject.Singleton;
1919

20-
import java.util.Collection;
2120
import java.util.List;
2221
import java.util.Map;
2322
import java.util.Optional;
@@ -35,6 +34,8 @@ public class HealthReporter {
3534

3635
private static final String MP_DEFAULT_STARTUP_EMPTY_RESPONSE = "mp.health.default.startup.empty.response";
3736
private static final String MP_DEFAULT_READINESS_EMPTY_RESPONSE = "mp.health.default.readiness.empty.response";
37+
private static final String CONTEXT_KEY = "context";
38+
3839
private static final Logger LOGGER = Logger.getLogger(HealthReporter.class.getName());
3940

4041
private final Map<String, List<HealthCheckInfo>> applicationHealthChecks = new ConcurrentHashMap<>();
@@ -52,6 +53,13 @@ private static HealthCheckResponse callHealthCheck(HealthCheck healthCheck) {
5253
}
5354
}
5455

56+
private static HealthCheckResponse addContextToResponse(HealthCheckResponse response, String contextName) {
57+
if (response instanceof GlassFishHealthCheckResponse gfResponse) {
58+
return gfResponse.addData(CONTEXT_KEY, contextName);
59+
} else {
60+
return response;
61+
}
62+
}
5563
private static HealthCheckResponse buildHealthCheckResponse(String name, Exception e) {
5664
return HealthCheckResponse.builder()
5765
.down()
@@ -62,15 +70,18 @@ private static HealthCheckResponse buildHealthCheckResponse(String name, Excepti
6270

6371
public enum ReportKind {
6472
/**
65-
* Return only health checks of kind {@link org.eclipse.microprofile.health.Liveness}
73+
* Return only health checks of kind
74+
* {@link org.eclipse.microprofile.health.Liveness}
6675
*/
6776
LIVE,
6877
/**
69-
* Return only health checks of kind {@link org.eclipse.microprofile.health.Readiness}
78+
* Return only health checks of kind
79+
* {@link org.eclipse.microprofile.health.Readiness}
7080
*/
7181
READY,
7282
/**
73-
* Return only health checks of kind {@link org.eclipse.microprofile.health.Startup}
83+
* Return only health checks of kind
84+
* {@link org.eclipse.microprofile.health.Startup}
7485
*/
7586
STARTED,
7687
/**
@@ -80,34 +91,45 @@ public enum ReportKind {
8091

8192
private HealthCheckResponse.Status getEmptyResponse() {
8293
return switch (this) {
83-
case LIVE -> getValue(MP_DEFAULT_STARTUP_EMPTY_RESPONSE)
84-
.orElse(HealthCheckResponse.Status.UP);
85-
case READY -> getValue(MP_DEFAULT_READINESS_EMPTY_RESPONSE)
86-
.orElse(HealthCheckResponse.Status.UP);
87-
case STARTED, ALL -> HealthCheckResponse.Status.UP;
94+
case LIVE ->
95+
getValue(MP_DEFAULT_STARTUP_EMPTY_RESPONSE)
96+
.orElse(HealthCheckResponse.Status.UP);
97+
case READY ->
98+
getValue(MP_DEFAULT_READINESS_EMPTY_RESPONSE)
99+
.orElse(HealthCheckResponse.Status.UP);
100+
case STARTED, ALL ->
101+
HealthCheckResponse.Status.UP;
88102
};
89103
}
90104

91105
public boolean filter(HealthCheckInfo healthCheck) {
92106
return switch (this) {
93-
case LIVE -> healthCheck.kind().contains(HealthCheckInfo.Kind.LIVE);
94-
case READY -> healthCheck.kind().contains(HealthCheckInfo.Kind.READY);
95-
case STARTED -> healthCheck.kind().contains(HealthCheckInfo.Kind.STARTUP);
96-
case ALL -> true;
107+
case LIVE ->
108+
healthCheck.kind().contains(HealthCheckInfo.Kind.LIVE);
109+
case READY ->
110+
healthCheck.kind().contains(HealthCheckInfo.Kind.READY);
111+
case STARTED ->
112+
healthCheck.kind().contains(HealthCheckInfo.Kind.STARTUP);
113+
case ALL ->
114+
true;
97115
};
98116
}
99117
}
100118

101119
public HealthReport getReport(ReportKind reportKind) {
102120
HealthCheckResponse.Status emptyResponse = reportKind.getEmptyResponse();
103121

104-
List<HealthCheckResponse> healthCheckResults = applicationHealthChecks.values()
122+
List<HealthCheckResponse> healthCheckResults = applicationHealthChecks.entrySet()
105123
.stream()
106-
.flatMap(Collection::stream)
107-
.filter(reportKind::filter)
108-
.map(HealthCheckInfo::healthCheck)
109-
.map(HealthReporter::callHealthCheck)
110-
.toList();
124+
.flatMap(entry -> {
125+
String contextName = entry.getKey();
126+
return entry.getValue()
127+
.stream()
128+
.filter(reportKind::filter)
129+
.map(HealthCheckInfo::healthCheck)
130+
.map(HealthReporter::callHealthCheck)
131+
.map(response -> addContextToResponse(response, entry.getKey()));
132+
}).toList();
111133

112134
HealthCheckResponse.Status overallStatus;
113135
if (healthCheckResults.isEmpty()) {

0 commit comments

Comments
 (0)