generated from pagopa/template-java-spring-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from pagopa/PAGOPA-1595-elk
feat: Add OpenTelemetry agent [PAGOPA-1595]
- Loading branch information
Showing
14 changed files
with
193 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export BLOB_SAS_URL="<to-set>" | ||
export BLOB_SAS_TOKEN="<to-set>" | ||
export BLOB_CONNECTION_STRING="<to-set>" | ||
COSMOS_URI="<to-set>" | ||
COSMOS_KEY="<to-set>" | ||
DB_NAME="<gpd-db-name>" | ||
CONTAINER_NAME="gpd-upload-status-container-name" | ||
POST_FILE_RETRY_AFTER="10000" | ||
LOG_LEVEL=INFO | ||
OTEL_SERVICE_NAME=pagopa-gpd-upload |
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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
# | ||
# Build stage | ||
# | ||
FROM maven:3.8.2-openjdk-17-slim AS build | ||
COPY src /home/app/src | ||
COPY pom.xml /home/app | ||
RUN mvn -f /home/app/pom.xml clean package -Dmaven.test.skip=true | ||
FROM maven:3.8.4-openjdk-17-slim AS build | ||
WORKDIR /app | ||
COPY pom.xml . | ||
COPY src ./src | ||
RUN mvn -B -DskipTests=true clean package -Dmaven.test.skip=true | ||
|
||
# | ||
# Package stage | ||
# | ||
FROM openjdk:17-alpine | ||
COPY --from=build /home/app/target/pagopa-gpd-upload*.jar /usr/local/lib/app.jar | ||
RUN true | ||
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.25.1/opentelemetry-javaagent.jar /opt/opentelemetry-javaagent.jar | ||
COPY --from=build /app/target/pagopa-gpd-upload*.jar /app/app.jar | ||
EXPOSE 8080 | ||
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar"] | ||
ENTRYPOINT ["java", "-javaagent:/opt/opentelemetry-javaagent.jar", "-jar", "/app/app.jar"] |
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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/it/gov/pagopa/gpd/upload/config/ExampleAnnotation.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package it.gov.pagopa.gpd.upload.config; | ||
|
||
import io.micronaut.aop.Around; | ||
import java.lang.annotation.*; | ||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Documented | ||
@Retention(RUNTIME) | ||
@Target({TYPE, METHOD}) | ||
@Around | ||
public @interface ExampleAnnotation { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/it/gov/pagopa/gpd/upload/config/ExampleInterceptor.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package it.gov.pagopa.gpd.upload.config; | ||
|
||
import io.micronaut.aop.InterceptorBean; | ||
import io.micronaut.aop.MethodInterceptor; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.core.annotation.Nullable; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
|
||
@Singleton | ||
@InterceptorBean(ExampleAnnotation.class) | ||
public class ExampleInterceptor implements MethodInterceptor<Object, Object> { | ||
|
||
@Nullable | ||
@Override | ||
public Object intercept(MethodInvocationContext<Object, Object> context) { | ||
// <ExampleInterceptor proxy code here> | ||
// @ExampleAnnotation to use it around method | ||
return context.proceed(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/it/gov/pagopa/gpd/upload/config/LogAspect.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package it.gov.pagopa.gpd.upload.config; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.MutableHttpResponse; | ||
import io.micronaut.http.annotation.Filter; | ||
import io.micronaut.http.filter.HttpServerFilter; | ||
import io.micronaut.http.filter.ServerFilterChain; | ||
import io.reactivex.rxjava3.core.Flowable; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.reactivestreams.Publisher; | ||
import org.slf4j.MDC; | ||
|
||
import java.util.UUID; | ||
|
||
|
||
@Filter("/*") | ||
@Slf4j | ||
public class LogAspect implements HttpServerFilter { | ||
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 RESPONSE = "response"; | ||
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"; | ||
|
||
@Override | ||
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) { | ||
long startTime = System.currentTimeMillis(); | ||
String path = request.getPath(); | ||
MDC.put(METHOD, request.getMethod() + " " + path.substring(path.lastIndexOf('/'))); | ||
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); | ||
} | ||
String params = request.getParameters().asMap().toString(); | ||
MDC.put(ARGS, params); | ||
|
||
log.info("Invoking API operation {} - args: {}", request.getMethodName(), params); | ||
|
||
return Flowable.fromPublisher(chain.proceed(request)).flatMap(response -> { | ||
|
||
MDC.put(STATUS, "OK"); | ||
MDC.put(CODE, String.valueOf(response.getStatus().getCode())); | ||
MDC.put(RESPONSE_TIME, String.valueOf(System.currentTimeMillis() - startTime)); | ||
MDC.put(RESPONSE, toJsonString(response.getBody().toString())); | ||
log.info("Successful API operation {} - result: {}", request.getMethodName(), response); | ||
MDC.remove(RESPONSE); | ||
MDC.remove(STATUS); | ||
MDC.remove(CODE); | ||
MDC.remove(RESPONSE_TIME); | ||
MDC.remove(START_TIME); | ||
|
||
return Flowable.just(response); | ||
}); | ||
} | ||
|
||
private static String toJsonString(Object param) { | ||
try { | ||
return new ObjectMapper() | ||
.registerModule(new JavaTimeModule()) | ||
.writeValueAsString(param); | ||
} catch (JsonProcessingException e) { | ||
log.warn("An error occurred when trying to parse a parameter", e); | ||
return "parsing error"; | ||
} | ||
} | ||
} |
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,14 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<!-- encoders are assigned the type | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
<encoder> | ||
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern> | ||
<encoder class="co.elastic.logging.logback.EcsEncoder"> | ||
<serviceName>${OTEL_SERVICE_NAME}</serviceName> | ||
<serviceVersion>${project.version}</serviceVersion> | ||
<serviceEnvironment>${ENV}</serviceEnvironment> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
<logger name="io.micronaut" level="INFO"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> | ||
|
||
</configuration> |