From da02babb457a79457ebac4966416146c85f05bd7 Mon Sep 17 00:00:00 2001 From: Sriram Kasthuri Date: Wed, 18 Oct 2023 15:23:31 +0100 Subject: [PATCH] LCAM-985: Upgraded crime commons version and enabled micrometer tracing --- crime-evidence/build.gradle | 2 +- .../crime/evidence/CrimeEvidenceApplication.java | 3 ++- .../justice/laa/crime/evidence/dto/ErrorDTO.java | 1 + .../exception/CrimeEvidenceExceptionHandler.java | 14 +++++++++----- crime-evidence/src/main/resources/application.yaml | 3 +++ crime-evidence/src/main/resources/logback.xml | 2 +- .../config/CrimeEvidenceTestConfiguration.java | 2 ++ 7 files changed, 19 insertions(+), 8 deletions(-) diff --git a/crime-evidence/build.gradle b/crime-evidence/build.gradle index dfc11b4..e8bd2ef 100644 --- a/crime-evidence/build.gradle +++ b/crime-evidence/build.gradle @@ -20,7 +20,7 @@ def versions = [ mockwebserverVersion : "4.10.0", pitest : "1.4.10", commonsLang3Version : "3.10", - crimeCommonsVersion : "2.2.0", + crimeCommonsVersion : "2.7.0", wiremockVersion : "2.35.0" ] diff --git a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/CrimeEvidenceApplication.java b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/CrimeEvidenceApplication.java index 54dbbb3..7596f54 100644 --- a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/CrimeEvidenceApplication.java +++ b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/CrimeEvidenceApplication.java @@ -2,12 +2,13 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Slf4j -@SpringBootApplication +@SpringBootApplication(exclude = ZipkinAutoConfiguration.class) @ConfigurationPropertiesScan @EnableAspectJAutoProxy(proxyTargetClass = true) public class CrimeEvidenceApplication { diff --git a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/dto/ErrorDTO.java b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/dto/ErrorDTO.java index 06b1d28..70ca6a6 100644 --- a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/dto/ErrorDTO.java +++ b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/dto/ErrorDTO.java @@ -6,6 +6,7 @@ @Value @Builder public class ErrorDTO { + private String traceId; private String code; private String message; } diff --git a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/exception/CrimeEvidenceExceptionHandler.java b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/exception/CrimeEvidenceExceptionHandler.java index 849cd83..9f4b6f5 100644 --- a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/exception/CrimeEvidenceExceptionHandler.java +++ b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/exception/CrimeEvidenceExceptionHandler.java @@ -1,32 +1,36 @@ package uk.gov.justice.laa.crime.evidence.exception; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import uk.gov.justice.laa.crime.commons.exception.APIClientException; +import uk.gov.justice.laa.crime.commons.tracing.TraceIdHandler; import uk.gov.justice.laa.crime.evidence.dto.ErrorDTO; - -@RestControllerAdvice @Slf4j +@RestControllerAdvice +@RequiredArgsConstructor public class CrimeEvidenceExceptionHandler { - private static ResponseEntity buildErrorResponse(HttpStatus status, String errorMessage) { + private final TraceIdHandler traceIdHandler; + + private static ResponseEntity buildErrorResponse(HttpStatus status, String errorMessage, String traceId) { return new ResponseEntity<>(ErrorDTO.builder().code(status.toString()).message(errorMessage).build(), status); } @ExceptionHandler(APIClientException.class) public ResponseEntity handleApiClientError(APIClientException ex) { log.error("APIClientException: ", ex); - return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage(), traceIdHandler.getTraceId()); } @ExceptionHandler(CrimeEvidenceDataException.class) public ResponseEntity handleCrimeEvidenceDataException(CrimeEvidenceDataException ex) { log.error("CrimeEvidenceDataException: ", ex); - return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage(), traceIdHandler.getTraceId()); } } diff --git a/crime-evidence/src/main/resources/application.yaml b/crime-evidence/src/main/resources/application.yaml index 151e78a..b440d0d 100644 --- a/crime-evidence/src/main/resources/application.yaml +++ b/crime-evidence/src/main/resources/application.yaml @@ -14,6 +14,9 @@ management: web: exposure: include: health,info,metrics,prometheus + tracing: + propagation: + type: w3c,b3 springdoc: packagesToScan: uk.gov.justice.laa.crime.evidence diff --git a/crime-evidence/src/main/resources/logback.xml b/crime-evidence/src/main/resources/logback.xml index e2a5928..715013c 100644 --- a/crime-evidence/src/main/resources/logback.xml +++ b/crime-evidence/src/main/resources/logback.xml @@ -1,7 +1,7 @@ - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + %d{HH:mm:ss.SSS} traceId: %X{traceId:-} spanId: %X{spanId:-} [%thread] %-5level %logger{36} - %msg%n diff --git a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/config/CrimeEvidenceTestConfiguration.java b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/config/CrimeEvidenceTestConfiguration.java index 3d9039b..76e79d5 100644 --- a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/config/CrimeEvidenceTestConfiguration.java +++ b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/config/CrimeEvidenceTestConfiguration.java @@ -2,6 +2,7 @@ import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.jwt.JwtDecoder; @@ -9,6 +10,7 @@ import java.util.Map; @TestConfiguration +@ComponentScan(basePackages = {"uk.gov.justice.laa.crime.commons.tracing"}) public class CrimeEvidenceTestConfiguration { static final String SUB = "sub";