-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: health indicator for ext services
- Loading branch information
1 parent
f98dac2
commit 6cbf622
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/ca/bc/gov/app/health/BcRegistryApiHealthIndicator.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 ca.bc.gov.app.health; | ||
|
||
import io.micrometer.observation.annotation.Observed; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@Slf4j | ||
@Observed | ||
public class BcRegistryApiHealthIndicator implements HealthIndicator { | ||
|
||
private final WebClient addressCompleteApi; | ||
private Health apiHealth = Health.unknown().build(); | ||
|
||
public BcRegistryApiHealthIndicator( | ||
@Qualifier("bcRegistryApi") WebClient addressCompleteApi | ||
) { | ||
this.addressCompleteApi = addressCompleteApi; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
addressCompleteApi | ||
.get() | ||
.uri(uriBuilder -> | ||
uriBuilder | ||
.path("/registry-search/api/v1/businesses/search/suggest") | ||
.queryParam("query", "value:XX000000") | ||
.build(Map.of()) | ||
) | ||
.exchangeToMono(clientResponse -> { | ||
if (clientResponse.statusCode().is2xxSuccessful()) { | ||
return Mono.just(Health.up().build()); | ||
} else { | ||
return Mono.just(Health.down().build()); | ||
} | ||
}) | ||
.doOnNext(health -> log.info("BC Registry API health: {}", health)) | ||
.subscribe( | ||
health -> apiHealth = health, | ||
error -> apiHealth = Health.down().withException(error).build() | ||
); | ||
log.info("Checking Bc Registry API health"); | ||
return apiHealth; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
backend/src/main/java/ca/bc/gov/app/health/CanadaPostApiHealthIndicator.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,58 @@ | ||
package ca.bc.gov.app.health; | ||
|
||
import ca.bc.gov.app.configuration.ForestClientConfiguration; | ||
import io.micrometer.observation.annotation.Observed; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@Slf4j | ||
@Observed | ||
public class CanadaPostApiHealthIndicator implements HealthIndicator { | ||
|
||
private final WebClient addressCompleteApi; | ||
private final ForestClientConfiguration.AddressCompleteConfiguration configuration; | ||
private Health apiHealth = Health.unknown().build(); | ||
|
||
public CanadaPostApiHealthIndicator( | ||
ForestClientConfiguration configuration, | ||
@Qualifier("addressCompleteApi") WebClient addressCompleteApi | ||
) { | ||
this.configuration = configuration.getAddressComplete(); | ||
this.addressCompleteApi = addressCompleteApi; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
addressCompleteApi | ||
.get() | ||
.uri(uriBuilder -> | ||
uriBuilder | ||
.path("/find/v2.10/json3.ws") | ||
.queryParam("key", configuration.getApiKey()) | ||
.build(Map.of()) | ||
) | ||
.exchangeToMono(clientResponse -> { | ||
if (clientResponse.statusCode().is2xxSuccessful()) { | ||
return Mono.just(Health.up().build()); | ||
} else { | ||
return Mono.just(Health.down().build()); | ||
} | ||
}) | ||
.doOnNext(health -> log.info("Canada Post API health: {}", health)) | ||
.subscribe( | ||
health -> apiHealth = health, | ||
error -> apiHealth = Health.down().withException(error).build() | ||
); | ||
log.info("Checking Canada Post API health"); | ||
|
||
|
||
return apiHealth; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
backend/src/main/java/ca/bc/gov/app/health/ChesHealthIndicator.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,71 @@ | ||
package ca.bc.gov.app.health; | ||
|
||
import ca.bc.gov.app.configuration.ForestClientConfiguration; | ||
import ca.bc.gov.app.dto.ches.CommonExposureJwtDto; | ||
import io.micrometer.observation.annotation.Observed; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@Slf4j | ||
@Observed | ||
public class ChesHealthIndicator implements HealthIndicator { | ||
|
||
private final WebClient chesApi; | ||
|
||
private final WebClient authApi; | ||
private Health apiHealth = Health.unknown().build(); | ||
|
||
public ChesHealthIndicator( | ||
@Qualifier("chesApi") WebClient chesApi, | ||
@Qualifier("authApi") WebClient authApi | ||
) { | ||
this.chesApi = chesApi; | ||
this.authApi = authApi; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
getToken() | ||
.flatMap(token -> | ||
chesApi | ||
.get() | ||
.uri("/health") | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.exchangeToMono(clientResponse -> { | ||
if (clientResponse.statusCode().is2xxSuccessful()) { | ||
return Mono.just(Health.up().build()); | ||
} else { | ||
return Mono.just(Health.down().build()); | ||
} | ||
}) | ||
) | ||
.doOnNext(health -> log.info("CHES API health: {}", health)) | ||
.subscribe( | ||
health -> apiHealth = health, | ||
error -> apiHealth = Health.down().withException(error).build() | ||
); | ||
log.info("Checking CHES API health"); | ||
return apiHealth; | ||
} | ||
|
||
private Mono<String> getToken() { | ||
return | ||
authApi | ||
.post() | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
.body(BodyInserters.fromFormData("grant_type", "client_credentials")) | ||
.retrieve() | ||
.bodyToMono(CommonExposureJwtDto.class) | ||
.map(CommonExposureJwtDto::accessToken); | ||
} | ||
} |