generated from pagopa/template-java-spring-microservice
-
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.
- Loading branch information
1 parent
9667ab1
commit 1c233ea
Showing
11 changed files
with
252 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"selfDiagnostics": { | ||
"destination": "console", | ||
"level": "INFO" | ||
}, | ||
"sampling": { | ||
"percentage": 5, | ||
"overrides": [ | ||
{ | ||
"telemetryType": "request", | ||
"percentage": 5 | ||
}, | ||
{ | ||
"telemetryType": "dependency", | ||
"percentage": 5 | ||
}, | ||
{ | ||
"telemetryType": "trace", | ||
"percentage": 5 | ||
}, | ||
{ | ||
"telemetryType": "exception", | ||
"percentage": 100 | ||
} | ||
] | ||
} | ||
} |
202 changes: 136 additions & 66 deletions
202
src/main/java/it/gov/pagopa/payments/config/LoggingAspect.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 |
---|---|---|
@@ -1,95 +1,165 @@ | ||
package it.gov.pagopa.payments.config; | ||
|
||
import static it.gov.pagopa.payments.utils.CommonUtil.deNull; | ||
|
||
import it.gov.pagopa.payments.exception.AppError; | ||
import it.gov.pagopa.payments.model.ProblemJson; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import javax.annotation.PostConstruct; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.aspectj.lang.reflect.CodeSignature; | ||
import org.slf4j.MDC; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.core.env.AbstractEnvironment; | ||
import org.springframework.core.env.EnumerablePropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.MutablePropertySources; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.Arrays; | ||
import java.util.stream.StreamSupport; | ||
|
||
@Aspect | ||
@Component | ||
@Slf4j | ||
public class LoggingAspect { | ||
|
||
@Value("${application.name}") | ||
private String name; | ||
public static final String START_TIME = "startTime"; | ||
public static final String METHOD = "method"; | ||
public static final String STATUS = "status"; | ||
public static final String CODE = "httpCode"; | ||
public static final String RESPONSE_TIME = "responseTime"; | ||
public static final String FAULT_CODE = "faultCode"; | ||
public static final String FAULT_DETAIL = "faultDetail"; | ||
public static final String REQUEST_ID = "requestId"; | ||
public static final String OPERATION_ID = "operationId"; | ||
public static final String ARGS = "args"; | ||
|
||
@Value("${application.version}") | ||
private String version; | ||
final HttpServletRequest httRequest; | ||
|
||
@Value("${properties.environment}") | ||
private String environment; | ||
final HttpServletResponse httpResponse; | ||
|
||
/** | ||
* Log essential info of application during the startup. | ||
*/ | ||
@PostConstruct | ||
public void logStartup() { | ||
log.info("-> Starting {} version {} - environment {}", name, version, environment); | ||
} | ||
@Value("${info.application.name}") | ||
private String name; | ||
|
||
/** | ||
* If DEBUG log-level is enabled prints the env variables and the application properties. | ||
* | ||
* @param event Context of application | ||
*/ | ||
@EventListener | ||
public void handleContextRefresh(ContextRefreshedEvent event) { | ||
final Environment env = event.getApplicationContext().getEnvironment(); | ||
log.debug("Active profiles: {}", Arrays.toString(env.getActiveProfiles())); | ||
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources(); | ||
StreamSupport.stream(sources.spliterator(), false) | ||
.filter(EnumerablePropertySource.class::isInstance) | ||
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames()) | ||
.flatMap(Arrays::stream) | ||
.distinct() | ||
.filter( | ||
prop -> | ||
!(prop.toLowerCase().contains("credentials") | ||
|| prop.toLowerCase().contains("password") | ||
|| prop.toLowerCase().contains("pass") | ||
|| prop.toLowerCase().contains("pwd"))) | ||
.forEach(prop -> log.debug("{}: {}", prop, env.getProperty(prop))); | ||
} | ||
@Value("${info.application.version}") | ||
private String version; | ||
|
||
@Value("${info.properties.environment}") | ||
private String environment; | ||
|
||
@AfterReturning(value = "execution(* it.gov.pagopa.payments.controller..*.*(..)) || execution(* it.gov.pagopa.payments.endpoints..*.*(..))", returning = "result") | ||
public void returnApiInvocation(JoinPoint joinPoint, Object result) { | ||
log.debug("Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result); | ||
} | ||
public LoggingAspect(HttpServletRequest httRequest, HttpServletResponse httpResponse) { | ||
this.httRequest = httRequest; | ||
this.httpResponse = httpResponse; | ||
} | ||
|
||
private static String getDetail(ResponseEntity<ProblemJson> result) { | ||
if (result != null && result.getBody() != null && result.getBody().getDetail() != null) { | ||
return result.getBody().getDetail(); | ||
} else return AppError.UNKNOWN.getDetails(); | ||
} | ||
|
||
@AfterReturning( | ||
value = "execution(* it.gov.pagopa.payments.exception.ErrorHandler.*(..))", | ||
returning = "result") | ||
public void trowingApiInvocation(JoinPoint joinPoint, Object result) { | ||
log.info("Failed API operation {} - error: {}", joinPoint.getSignature().getName(), result); | ||
private static String getTitle(ResponseEntity<ProblemJson> result) { | ||
if (result != null && result.getBody() != null && result.getBody().getTitle() != null) { | ||
return result.getBody().getTitle(); | ||
} else return AppError.UNKNOWN.getTitle(); | ||
} | ||
|
||
public static String getExecutionTime() { | ||
String startTime = MDC.get(START_TIME); | ||
if (startTime != null) { | ||
long endTime = System.currentTimeMillis(); | ||
long executionTime = endTime - Long.parseLong(startTime); | ||
return String.valueOf(executionTime); | ||
} | ||
return "-"; | ||
} | ||
|
||
@Around(value = "execution(* it.gov.pagopa.payments.service..*.*(..))") | ||
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { | ||
long startTime = System.currentTimeMillis(); | ||
Object result = joinPoint.proceed(); | ||
long endTime = System.currentTimeMillis(); | ||
log.debug("Time taken for Execution of {} is: {}ms", joinPoint.getSignature().toShortString(), (endTime - startTime)); | ||
return result; | ||
private static Map<String, String> getParams(ProceedingJoinPoint joinPoint) { | ||
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature(); | ||
Map<String, String> params = new HashMap<>(); | ||
int i = 0; | ||
for (var paramName : codeSignature.getParameterNames()) { | ||
params.put(paramName, deNull(joinPoint.getArgs()[i++])); | ||
} | ||
return params; | ||
} | ||
|
||
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)") | ||
public void restController() { | ||
// all rest controllers | ||
} | ||
|
||
@Before(value = "execution(* it.gov.pagopa.payments..*.*(..))") | ||
public void logTrace(JoinPoint joinPoint) { | ||
log.trace("Trace method {} - args: {}", joinPoint.getSignature().toShortString(), joinPoint.getArgs()); | ||
@Pointcut("@within(org.springframework.ws.server.endpoint.annotation.Endpoint)") | ||
public void endpointClass() { | ||
// all rest controllers | ||
} | ||
|
||
@Pointcut("@within(org.springframework.stereotype.Repository)") | ||
public void repository() { | ||
// all repository methods | ||
} | ||
|
||
@Pointcut("@within(org.springframework.stereotype.Service)") | ||
public void service() { | ||
// all service methods | ||
} | ||
|
||
/** Log essential info of application during the startup. */ | ||
@PostConstruct | ||
public void logStartup() { | ||
log.info("-> Starting {} version {} - environment {}", name, version, environment); | ||
} | ||
|
||
@Around(value = "restController() || endpointClass()") | ||
public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable { | ||
MDC.put(METHOD, joinPoint.getSignature().getName()); | ||
MDC.put(START_TIME, String.valueOf(System.currentTimeMillis())); | ||
MDC.put(OPERATION_ID, UUID.randomUUID().toString()); | ||
if (MDC.get(REQUEST_ID) == null) { | ||
var requestId = UUID.randomUUID().toString(); | ||
MDC.put(REQUEST_ID, requestId); | ||
} | ||
Map<String, String> params = getParams(joinPoint); | ||
MDC.put(ARGS, params.toString()); | ||
|
||
log.info("Invoking API operation {} - args: {}", joinPoint.getSignature().getName(), params); | ||
|
||
Object result = joinPoint.proceed(); | ||
|
||
MDC.put(STATUS, "OK"); | ||
MDC.put(CODE, String.valueOf(httpResponse.getStatus())); | ||
MDC.put(RESPONSE_TIME, getExecutionTime()); | ||
log.info( | ||
"Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result); | ||
MDC.remove(STATUS); | ||
MDC.remove(CODE); | ||
MDC.remove(RESPONSE_TIME); | ||
MDC.remove(START_TIME); | ||
return result; | ||
} | ||
|
||
@AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result") | ||
public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity<ProblemJson> result) { | ||
MDC.put(STATUS, "KO"); | ||
MDC.put(CODE, String.valueOf(result.getStatusCode().value())); | ||
MDC.put(RESPONSE_TIME, getExecutionTime()); | ||
MDC.put(FAULT_CODE, getTitle(result)); | ||
MDC.put(FAULT_DETAIL, getDetail(result)); | ||
log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result); | ||
MDC.clear(); | ||
} | ||
|
||
@Around(value = "repository() || service()") | ||
public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable { | ||
Map<String, String> params = getParams(joinPoint); | ||
log.debug("Call method {} - args: {}", joinPoint.getSignature().toShortString(), params); | ||
Object result = joinPoint.proceed(); | ||
log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result); | ||
return result; | ||
} | ||
} |
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
Oops, something went wrong.