Skip to content

Commit

Permalink
feat: health indicator for ext services
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Apr 9, 2024
1 parent f98dac2 commit 6cbf622
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
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;
}
}
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;
}
}
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);
}
}

0 comments on commit 6cbf622

Please sign in to comment.