Add skeletton to save traces.
This commit is contained in:
@@ -25,6 +25,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -33,28 +37,5 @@
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.postgresql</groupId>-->
|
||||
<!-- <artifactId>postgresql</artifactId>-->
|
||||
<!-- <scope>runtime</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.security</groupId>-->
|
||||
<!-- <artifactId>spring-security-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.codiki.exposition.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@EnableAsync
|
||||
public class TrafficTraceConfiguration {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.codiki.exposition.traffic;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.codiki.application.traffic.TrafficTraceUseCases;
|
||||
import org.codiki.domain.traffic.model.HttpMethod;
|
||||
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class ApiCallsLoggerAspect {
|
||||
private static final String HTTP_HEADER_CORRELATION_ID = "x-correlation-id";
|
||||
|
||||
private final TrafficTraceUseCases trafficTraceUseCases;
|
||||
|
||||
public ApiCallsLoggerAspect(TrafficTraceUseCases trafficTraceUseCases) {
|
||||
this.trafficTraceUseCases = trafficTraceUseCases;
|
||||
}
|
||||
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")
|
||||
public void logGetApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.PostMapping)")
|
||||
public void logPostApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.PutMapping)")
|
||||
public void logPutApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
|
||||
public void logDeleteApiCall(JoinPoint joinPoint) {
|
||||
logApiCall();
|
||||
}
|
||||
|
||||
private void logApiCall() {
|
||||
Optional.ofNullable(RequestContextHolder.getRequestAttributes())
|
||||
.filter(ServletRequestAttributes.class::isInstance)
|
||||
.map(ServletRequestAttributes.class::cast)
|
||||
.map(ServletRequestAttributes::getRequest)
|
||||
.ifPresent(request ->
|
||||
Optional.of(request.getMethod())
|
||||
.flatMap(HttpMethod::fromString)
|
||||
.ifPresent(queryHttpMethod -> {
|
||||
String queryUriPath = request.getRequestURI();
|
||||
String correlationId = request.getHeader(HTTP_HEADER_CORRELATION_ID);
|
||||
TrafficEndpoint endpoint = new TrafficEndpoint(queryHttpMethod, queryUriPath);
|
||||
trafficTraceUseCases.saveNewTrace(endpoint, correlationId);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user