1717
1818import jakarta .inject .Singleton ;
1919
20- import java .util .Collection ;
2120import java .util .List ;
2221import java .util .Map ;
2322import 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