-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* added util methods for health checks * readme details * upgrade to java 17 and configurable down response * bytte ut lombok med records
- Loading branch information
Showing
15 changed files
with
395 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
52 changes: 52 additions & 0 deletions
52
src/main/java/no/idporten/actuator/config/HealthCheckEndpointConfig.java
This file contains 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,52 @@ | ||
package no.idporten.actuator.config; | ||
|
||
import no.idporten.actuator.monitor.ExternalDependencyHealthIndicator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.time.Duration; | ||
|
||
|
||
@Configuration | ||
@EnableConfigurationProperties(HealthCheckEndpointProperties.class) | ||
public class HealthCheckEndpointConfig implements InitializingBean { | ||
private final Logger log = LoggerFactory.getLogger(HealthCheckEndpointConfig.class); | ||
private final HealthCheckEndpointProperties healthCheckEndpointProperties; | ||
|
||
public HealthCheckEndpointConfig(GenericApplicationContext applicationContext, HealthCheckEndpointProperties healthCheckEndpointProperties) { | ||
this.healthCheckEndpointProperties = healthCheckEndpointProperties; | ||
if (healthCheckEndpointProperties.healthEndpoints() != null) { | ||
healthCheckEndpointProperties.healthEndpoints().values() | ||
.forEach(healthCheckEndpoint -> { | ||
log.info("Registering health check for {}", healthCheckEndpoint.name()); | ||
applicationContext.registerBean(healthCheckEndpoint.name() + "HealthCheck", ExternalDependencyHealthIndicator.class, () -> { | ||
RestTemplateBuilder builder = new RestTemplateBuilder(); | ||
RestTemplate restTemplate = builder.rootUri(healthCheckEndpoint.baseUri()) | ||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) | ||
.setConnectTimeout(Duration.ofMillis(healthCheckEndpoint.connectTimeoutMs())) | ||
.setReadTimeout(Duration.ofMillis(healthCheckEndpoint.readTimeoutMs())) | ||
.build(); | ||
return new ExternalDependencyHealthIndicator(restTemplate, healthCheckEndpoint); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
if (healthCheckEndpointProperties.healthEndpoints() == null || healthCheckEndpointProperties.healthEndpoints().isEmpty()) { | ||
log.info("No health check endpoints configured"); | ||
} else { | ||
log.info("Configured health check endpoints: {}", healthCheckEndpointProperties.healthEndpoints().keySet()); | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/no/idporten/actuator/config/HealthCheckEndpointProperties.java
This file contains 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,21 @@ | ||
package no.idporten.actuator.config; | ||
|
||
import no.idporten.actuator.monitor.HealthCheckEndpoint; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
import javax.validation.Valid; | ||
import java.io.Serializable; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
@Validated | ||
@ConfigurationProperties(prefix = "external-dependency-health-checks") | ||
public record HealthCheckEndpointProperties( | ||
Map<String, @Valid HealthCheckEndpoint> healthEndpoints) | ||
implements Serializable { | ||
|
||
public Map<String, HealthCheckEndpoint> healthEndpoints() { | ||
return healthEndpoints != null ? healthEndpoints : Collections.emptyMap(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/no/idporten/actuator/monitor/CustomStatus.java
This file contains 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,13 @@ | ||
package no.idporten.actuator.monitor; | ||
|
||
import org.springframework.boot.actuate.health.Status; | ||
|
||
import java.io.Serializable; | ||
|
||
public class CustomStatus implements Serializable { | ||
public static final Status DEGRADED = new Status("DEGRADED"); | ||
public static final Status EXTERNAL_DEPENDENCY_DOWN = new Status("EXTERNAL_DEPENDENCY_DOWN"); | ||
|
||
private CustomStatus() { | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/no/idporten/actuator/monitor/ExternalDependencyHealthIndicator.java
This file contains 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,62 @@ | ||
package no.idporten.actuator.monitor; | ||
|
||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.boot.actuate.health.Status; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class ExternalDependencyHealthIndicator implements HealthIndicator { | ||
|
||
private final Logger log = LoggerFactory.getLogger(ExternalDependencyHealthIndicator.class); | ||
private final RestTemplate restTemplate; | ||
private final HealthCheckEndpoint healthCheckEndpoint; | ||
|
||
public ExternalDependencyHealthIndicator(RestTemplate restTemplate, HealthCheckEndpoint healthCheckEndpoint) { | ||
this.restTemplate = restTemplate; | ||
this.healthCheckEndpoint = healthCheckEndpoint; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
try { | ||
HealthResponse health = this.getIntegrationHealth(); | ||
if (Status.UP.getCode().equals(health.status())) { | ||
return Health.up().build(); | ||
} else if (CustomStatus.DEGRADED.getCode().equals(health.status())) { | ||
return Health.status(CustomStatus.DEGRADED).build(); | ||
} else { | ||
return Health.status(healthCheckEndpoint.downStatus()).build(); | ||
} | ||
} catch (HttpClientErrorException e) { | ||
log.error("Health check to {} failed with exception {}", healthCheckEndpoint.name(), e.getMessage(), e); | ||
if (e.getStatusCode() == HttpStatus.NOT_FOUND) { | ||
log.error("Wrong configuration of {} health endpoint?", healthCheckEndpoint.name()); | ||
} | ||
return Health.status(healthCheckEndpoint.downStatus()).withException(e).build(); | ||
} catch (Exception e) { | ||
log.error("Health check to {} failed with exception {}", healthCheckEndpoint.name(), e.getMessage(), e); | ||
return Health.status(healthCheckEndpoint.downStatus()).withException(e).build(); | ||
} | ||
} | ||
|
||
protected HealthResponse getIntegrationHealth() { | ||
ResponseEntity<HealthResponse> forEntity = this.restTemplate.getForEntity(healthCheckEndpoint.endpoint(), | ||
HealthResponse.class); | ||
if (forEntity.getBody() != null) { | ||
return forEntity.getBody(); | ||
} else { | ||
return new HealthResponse(Status.DOWN.getCode()); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return this.healthCheckEndpoint.name(); | ||
} | ||
|
||
} |
Oops, something went wrong.