Skip to content

Commit

Permalink
Add Health indicator (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmendrak authored and timja committed Dec 14, 2017
1 parent 639be96 commit 7fc2fbe
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'uk.gov.hmcts.reform'
version '0.0.4'
version '0.0.5'

checkstyle {
maxWarnings = 0
Expand Down Expand Up @@ -120,6 +120,7 @@ ext {

dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-feign'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
compile group: 'com.netflix.feign', name: 'feign-jackson', version: '8.18.0'
compile group: 'com.google.guava', name: 'guava', version: '23.0'
compile group: 'com.warrenstrange', name: 'googleauth', version: '1.1.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import uk.gov.hmcts.reform.authorisation.healthcheck.InternalHealth;

import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@EnableFeignClients
@FeignClient(name = "idam-s2s-auth", url = "${idam.s2s-auth.url}")
Expand All @@ -19,4 +24,11 @@ String serviceToken(@RequestParam("microservice") final String microservice,
@GetMapping(value = "/authorisation-check")
void authorise(@RequestHeader(AUTHORIZATION) final String authHeader,
@RequestParam("role") final String[] roles);

@RequestMapping(
method = RequestMethod.GET,
value = "/health",
headers = CONTENT_TYPE + "=" + APPLICATION_JSON_UTF8_VALUE
)
InternalHealth health();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.gov.hmcts.reform.authorisation.healthcheck;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.boot.actuate.health.Status;

import java.util.Map;

public class InternalHealth {
private final Status status;
private final Map<String, Object> details;

@JsonCreator
public InternalHealth(
@JsonProperty("status") Status status,
@JsonProperty("details") Map<String, Object> details
) {
this.status = status;
this.details = details;
}

public Status getStatus() {
return status;
}

public Map<String, Object> getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.hmcts.reform.authorisation.healthcheck;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.reform.authorisation.ServiceAuthorisationApi;

@Component
public class ServiceAuthHealthIndicator implements HealthIndicator {

private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAuthHealthIndicator.class);

private final ServiceAuthorisationApi serviceAuthorisationApi;

@Autowired
public ServiceAuthHealthIndicator(final ServiceAuthorisationApi serviceAuthorisationApi) {
this.serviceAuthorisationApi = serviceAuthorisationApi;
}

@Override
public Health health() {
try {
InternalHealth internalHealth = this.serviceAuthorisationApi.health();
return new Health.Builder(internalHealth.getStatus()).build();
} catch (Exception ex) {
LOGGER.error("Error on service auth healthcheck", ex);
return Health.down(ex).build();
}
}
}

0 comments on commit 7fc2fbe

Please sign in to comment.