Add skeletton to save traces.
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
package org.codiki.application.traffic;
|
||||||
|
|
||||||
|
import jakarta.annotation.Nullable;
|
||||||
|
import org.codiki.domain.traffic.exception.TrafficTraceCreationException;
|
||||||
|
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||||
|
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||||
|
import org.codiki.domain.traffic.port.TrafficTracePort;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static java.util.Objects.isNull;
|
||||||
|
import static org.codiki.domain.traffic.model.TrafficTrace.aTrafficTrace;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TrafficTraceUseCases {
|
||||||
|
private final TrafficTracePort trafficTracePort;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
public TrafficTraceUseCases(TrafficTracePort trafficTracePort, Clock clock) {
|
||||||
|
this.trafficTracePort = trafficTracePort;
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Async
|
||||||
|
public void saveNewTrace(TrafficEndpoint trafficEndpoint, @Nullable String correlationId) {
|
||||||
|
if (isNull(trafficEndpoint)) {
|
||||||
|
throw new TrafficTraceCreationException("Traffic endpoint should not be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
TrafficTrace newTrace = aTrafficTrace()
|
||||||
|
.withId(UUID.randomUUID())
|
||||||
|
.withDateTime(ZonedDateTime.now(clock))
|
||||||
|
.withEndpoint(trafficEndpoint)
|
||||||
|
.withCorrelationId(correlationId)
|
||||||
|
.build();
|
||||||
|
trafficTracePort.save(newTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.codiki.domain.traffic.exception;
|
||||||
|
|
||||||
|
import org.codiki.domain.exception.FunctionnalException;
|
||||||
|
|
||||||
|
public class TrafficTraceCreationException extends FunctionnalException {
|
||||||
|
public TrafficTraceCreationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.codiki.domain.traffic.model;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public enum HttpMethod {
|
||||||
|
GET, POST, PUT, DELETE;
|
||||||
|
|
||||||
|
public static Optional<HttpMethod> fromString(String methodAsString) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(method -> method.name().equals(methodAsString))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package org.codiki.domain.traffic.model;
|
||||||
|
|
||||||
|
public record TrafficEndpoint(
|
||||||
|
HttpMethod method,
|
||||||
|
String path
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package org.codiki.domain.traffic.model;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record TrafficTrace(
|
||||||
|
UUID id,
|
||||||
|
ZonedDateTime dateTime,
|
||||||
|
TrafficEndpoint endpoint,
|
||||||
|
String correlationId
|
||||||
|
) {
|
||||||
|
public static Builder aTrafficTrace() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private UUID id;
|
||||||
|
private ZonedDateTime dateTime;
|
||||||
|
private TrafficEndpoint endpoint;
|
||||||
|
private String correlationId;
|
||||||
|
|
||||||
|
private Builder() {}
|
||||||
|
|
||||||
|
public Builder withId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withDateTime(ZonedDateTime dateTime) {
|
||||||
|
this.dateTime = dateTime;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withEndpoint(TrafficEndpoint endpoint) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withCorrelationId(String correlationId) {
|
||||||
|
this.correlationId = correlationId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TrafficTrace build() {
|
||||||
|
return new TrafficTrace(id, dateTime, endpoint, correlationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.codiki.domain.traffic.port;
|
||||||
|
|
||||||
|
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface TrafficTracePort {
|
||||||
|
void save(TrafficTrace trace);
|
||||||
|
List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
|
||||||
|
List<TrafficTrace> getAllByCorrelationId(String correlationId);
|
||||||
|
Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
|
||||||
|
Integer countByCorrelationId(String correlationId);
|
||||||
|
}
|
||||||
@@ -25,6 +25,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
@@ -33,28 +37,5 @@
|
|||||||
<groupId>org.apache.tika</groupId>
|
<groupId>org.apache.tika</groupId>
|
||||||
<artifactId>tika-core</artifactId>
|
<artifactId>tika-core</artifactId>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
</project>
|
</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,61 @@
|
|||||||
|
package org.codiki.exposition.traffic;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
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);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.codiki.infrastructure.traffic;
|
||||||
|
|
||||||
|
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||||
|
import org.codiki.domain.traffic.port.TrafficTracePort;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TrafficTraceInMemoryAdapter implements TrafficTracePort {
|
||||||
|
private final List<TrafficTrace> traces = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(TrafficTrace trace) {
|
||||||
|
traces.add(trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TrafficTrace> getAllByCorrelationId(String correlationId) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer countByCorrelationId(String correlationId) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,11 @@
|
|||||||
<java.version>21</java.version>
|
<java.version>21</java.version>
|
||||||
<maven.compiler.source>21</maven.compiler.source>
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
<jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
|
||||||
<java-jwt.version>4.4.0</java-jwt.version>
|
<java-jwt.version>4.4.0</java-jwt.version>
|
||||||
<postgresql.version>42.7.0</postgresql.version>
|
|
||||||
<tika-core.version>2.9.0</tika-core.version>
|
<tika-core.version>2.9.0</tika-core.version>
|
||||||
<commons-lang3.version>3.14.0</commons-lang3.version>
|
<postgresql.version>42.7.4</postgresql.version>
|
||||||
|
<commons-lang3.version>3.17.0</commons-lang3.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-dependencies</artifactId>
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
<version>3.2.0</version>
|
<version>3.3.4</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -84,8 +84,6 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user