Skip to content

Commit

Permalink
feat(spring-boot): added support for explicit path for readiness and …
Browse files Browse the repository at this point in the history
…liveness probes

SpringBootHealthCheckEnricher adds different probes for liveness and readiness if
application.properties contains:

```
management.health.probes.enabled=true
```

Signed-off-by: l3002 <tanmaymathpal4545@gmail.com>
  • Loading branch information
l3002 authored Jun 7, 2024
1 parent db23126 commit c2e13f5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SpringBootConfiguration {
private String managementContextPath;
private String actuatorBasePath;
private String actuatorDefaultBasePath;
private boolean managementHealthProbesEnabled;

public static SpringBootConfiguration from(JavaProject project) {
final Properties properties = SpringBootUtil.getSpringBootApplicationProperties(
Expand All @@ -56,6 +57,7 @@ public static SpringBootConfiguration from(JavaProject project) {
.managementPort(Optional.ofNullable(properties.getProperty("management.port")).map(Integer::parseInt).orElse(null))
.serverPort(Integer.parseInt(properties.getProperty("server.port", DEFAULT_SERVER_PORT)))
.serverKeystore(properties.getProperty("server.ssl.key-store"))
.managementHealthProbesEnabled(Boolean.parseBoolean(properties.getProperty("management.health.probes.enabled")))
.managementKeystore(properties.getProperty("management.ssl.key-store"))
.servletPath(properties.getProperty("server.servlet-path"))
.serverContextPath(properties.getProperty("server.context-path"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class SpringBootHealthCheckEnricher extends AbstractHealthCheckEnricher {
private static final String SCHEME_HTTPS = "HTTPS";
private static final String SCHEME_HTTP = "HTTP";

private static final String LIVENESS_PROBE_SUFFIX = "/liveness";
private static final String READINESS_PROBE_SUFFIX = "/readiness";

@AllArgsConstructor
private enum Config implements Configs.Config {
READINESS_PROBE_INITIAL_DELAY_SECONDS("readinessProbeInitialDelaySeconds", "10"),
Expand Down Expand Up @@ -67,7 +70,7 @@ protected Probe getReadinessProbe() {
Integer timeout = Configs.asInteger(getConfig(Config.TIMEOUT_SECONDS));
Integer failureThreshold = Configs.asInteger(getConfig(Config.FAILURE_THRESHOLD));
Integer successThreshold = Configs.asInteger(getConfig(Config.SUCCESS_THRESHOLD));
return discoverSpringBootHealthCheck(initialDelay, period, timeout, failureThreshold, successThreshold);
return discoverSpringBootHealthCheck(initialDelay, period, timeout, failureThreshold, successThreshold, READINESS_PROBE_SUFFIX);
}

@Override
Expand All @@ -77,21 +80,21 @@ protected Probe getLivenessProbe() {
Integer timeout = Configs.asInteger(getConfig(Config.TIMEOUT_SECONDS));
Integer failureThreshold = Configs.asInteger(getConfig(Config.FAILURE_THRESHOLD));
Integer successThreshold = Configs.asInteger(getConfig(Config.SUCCESS_THRESHOLD));
return discoverSpringBootHealthCheck(initialDelay, period, timeout, failureThreshold, successThreshold);
return discoverSpringBootHealthCheck(initialDelay, period, timeout, failureThreshold, successThreshold, LIVENESS_PROBE_SUFFIX);
}

protected Probe discoverSpringBootHealthCheck(Integer initialDelay, Integer period, Integer timeout, Integer failureTh, Integer successTh) {
protected Probe discoverSpringBootHealthCheck(Integer initialDelay, Integer period, Integer timeout, Integer failureTh, Integer successTh, String suffix) {
try {
if (getContext().getProjectClassLoaders().isClassInCompileClasspath(true, REQUIRED_CLASSES)) {
return buildProbe(initialDelay, period, timeout, failureTh, successTh);
return buildProbe(initialDelay, period, timeout, failureTh, successTh, suffix);
}
} catch (Exception ex) {
log.error("Error while reading the spring-boot configuration", ex);
}
return null;
}

protected Probe buildProbe(Integer initialDelay, Integer period, Integer timeout, Integer failureTh, Integer successTh) {
protected Probe buildProbe(Integer initialDelay, Integer period, Integer timeout, Integer failureTh, Integer successTh, String suffix) {
final SpringBootConfiguration springBootConfiguration = SpringBootConfiguration.from(getContext().getProject());
Integer managementPort = springBootConfiguration.getManagementPort();
boolean usingManagementPort = managementPort != null;
Expand Down Expand Up @@ -122,9 +125,16 @@ protected Probe buildProbe(Integer initialDelay, Integer period, Integer timeout
actuatorBasePath = springBootConfiguration.getActuatorBasePath();
}

// adds suffix to probe paths when ManagementHealthProbesEnabled is true
String probePath = prefix + actuatorBasePath + Configs.asString(getConfig(Config.PATH));
if(springBootConfiguration.isManagementHealthProbesEnabled()){
probePath += suffix;
}
probePath = normalizeMultipleSlashes(probePath);

// lets default to adding a spring boot actuator health check
ProbeBuilder probeBuilder = new ProbeBuilder().
withNewHttpGet().withNewPort(port).withPath(normalizeMultipleSlashes(prefix + actuatorBasePath + Configs.asString(getConfig(Config.PATH)))).withScheme(scheme).endHttpGet();
withNewHttpGet().withNewPort(port).withPath(probePath).withScheme(scheme).endHttpGet();

if (initialDelay != null) {
probeBuilder = probeBuilder.withInitialDelaySeconds(initialDelay);
Expand Down
Loading

0 comments on commit c2e13f5

Please sign in to comment.