Skip to content

Commit

Permalink
PAGOPA-1215 afm calculator creare dashboard elk (#109)
Browse files Browse the repository at this point in the history
* [PAGOPA-1215] logging

* Bump to version 2.8.0-1-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1215] replicas

* [PAGOPA-1215] replicas

* [PAGOPA-1215] log

* Bump to version 2.8.0-2-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1215] response

* [PAGOPA-1215] commit

* Bump to version 2.8.0-3-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* Bump to version 2.8.0-4-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1215] logging aspect

---------

Co-authored-by: pagopa-github-bot <[email protected]>
  • Loading branch information
jacopocarlini and pagopa-github-bot authored Oct 25, 2023
1 parent 673dbfc commit ea9aa58
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 69 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ repos:
language_version: python3
stages: [ commit ]

- repo: https://github.com/maltzj/google-style-precommit-hook
rev: b7e9e7fcba4a5aea463e72fe9964c14877bd8130
hooks:
- id: google-style-java

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN java -Djarmode=layertools -jar application.jar extract


FROM ghcr.io/pagopa/docker-base-springboot-openjdk11:v1.0.1@sha256:bbbe948e91efa0a3e66d8f308047ec255f64898e7f9250bdb63985efd3a95dbf
ADD --chown=spring:spring https://search.maven.org/remotecontent?filepath=co/elastic/apm/elastic-apm-agent/1.36.0/elastic-apm-agent-1.36.0.jar ./apm-elk-agent.jar
ADD --chown=spring:spring https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.25.1/opentelemetry-javaagent.jar .

COPY --chown=spring:spring --from=builder dependencies/ ./
COPY --chown=spring:spring --from=builder snapshot-dependencies/ ./
Expand All @@ -24,4 +24,4 @@ COPY --chown=spring:spring --from=builder application/ ./

EXPOSE 8080

ENTRYPOINT ["java","-javaagent:apm-elk-agent.jar","-javaagent:applicationinsights-agent.jar","org.springframework.boot.loader.JarLauncher"]
ENTRYPOINT ["java","-javaagent:opentelemetry-javaagent.jar","--enable-preview","org.springframework.boot.loader.JarLauncher"]
15 changes: 8 additions & 7 deletions helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ microservice-chart:
autoscaling:
enable: true
minReplica: 1
maxReplica: 2
maxReplica: 1
pollingInterval: 30 # seconds
cooldownPeriod: 60 # seconds
triggers:
Expand All @@ -77,7 +77,7 @@ microservice-chart:
fileConfig: {}
envConfig:
WEBSITE_SITE_NAME: 'pagopaafmcalculator' # required to show cloud role name in application insights
ENV: 'azure-dev'
ENV: 'dev'
APP_LOGGING_LEVEL: 'DEBUG'
CACHE_SIZE: '1000'
CACHE_EXPIRATION_TIME: '3m'
Expand All @@ -87,18 +87,19 @@ microservice-chart:
AMOUNT_THRESHOLD: '0'
ID_PSP_POSTE: 'BPPIITRRZZZ'
AMEX_ABI: '36019'
ELASTIC_APM_SERVICE_NAME: pagopa-afm-calculator
ELASTIC_APM_APPLICATION_PACKAGES: it.gov.pagopa.afm.calculator
ELASTIC_APM_SERVER_URL: http://quickstart-apm-http.elastic-system.svc:8200
ELASTIC_APM_LOG_SENDING: 'true'
OTEL_SERVICE_NAME: "pagopa-afm-calculator"
OTEL_RESOURCE_ATTRIBUTES: "deployment.environment=dev"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
envSecret:
APPLICATIONINSIGHTS_CONNECTION_STRING: 'ai-d-connection-string'
COSMOS_KEY: afm-marketplace-d-cosmos-pkey
AFM_SA_CONNECTION_STRING: afm-d-sa-connection-string
ELASTIC_APM_SECRET_TOKEN: elastic-apm-secret-token
OTEL_EXPORTER_OTLP_HEADERS: elastic-apm-secret-token
keyvault:
name: "pagopa-d-afm-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
13 changes: 7 additions & 6 deletions helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ microservice-chart:
fileConfig: {}
envConfig:
WEBSITE_SITE_NAME: 'pagopaafmcalculator' # required to show cloud role name in application insights
ENV: 'azure-prod'
ENV: 'prod'
APP_LOGGING_LEVEL: 'DEBUG'
CACHE_SIZE: '1000'
CACHE_EXPIRATION_TIME: '3m'
Expand All @@ -87,18 +87,19 @@ microservice-chart:
AMOUNT_THRESHOLD: '0'
ID_PSP_POSTE: 'BPPIITRRXXX'
AMEX_ABI: '36019'
ELASTIC_APM_SERVICE_NAME: pagopa-afm-calculator
ELASTIC_APM_APPLICATION_PACKAGES: it.gov.pagopa.afm.calculator
ELASTIC_APM_SERVER_URL: http://quickstart-apm-http.elastic-system.svc:8200
ELASTIC_APM_LOG_SENDING: 'true'
OTEL_SERVICE_NAME: "pagopa-afm-calculator"
OTEL_RESOURCE_ATTRIBUTES: "deployment.environment=prod"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
envSecret:
APPLICATIONINSIGHTS_CONNECTION_STRING: ai-p-connection-string
COSMOS_KEY: afm-marketplace-p-cosmos-pkey
AFM_SA_CONNECTION_STRING: afm-p-sa-connection-string
ELASTIC_APM_SECRET_TOKEN: elastic-apm-secret-token
OTEL_EXPORTER_OTLP_HEADERS: elastic-apm-secret-token
keyvault:
name: "pagopa-p-afm-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
15 changes: 8 additions & 7 deletions helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ microservice-chart:
autoscaling:
enable: true
minReplica: 1
maxReplica: 5
maxReplica: 1
pollingInterval: 10 # seconds
cooldownPeriod: 60 # seconds
triggers:
Expand All @@ -77,7 +77,7 @@ microservice-chart:
fileConfig: {}
envConfig:
WEBSITE_SITE_NAME: 'pagopaafmcalculator' # required to show cloud role name in application insights
ENV: 'azure-uat'
ENV: 'uat'
APP_LOGGING_LEVEL: 'DEBUG'
CACHE_SIZE: '1000'
CACHE_EXPIRATION_TIME: '3m'
Expand All @@ -87,18 +87,19 @@ microservice-chart:
AMOUNT_THRESHOLD: '0'
ID_PSP_POSTE: 'BPPIITRRXXX'
AMEX_ABI: '36019'
ELASTIC_APM_SERVICE_NAME: pagopa-afm-calculator
ELASTIC_APM_APPLICATION_PACKAGES: it.gov.pagopa.afm.calculator
ELASTIC_APM_SERVER_URL: http://quickstart-apm-http.elastic-system.svc:8200
ELASTIC_APM_LOG_SENDING: 'true'
OTEL_SERVICE_NAME: "pagopa-afm-calculator"
OTEL_RESOURCE_ATTRIBUTES: "deployment.environment=uat"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
envSecret:
APPLICATIONINSIGHTS_CONNECTION_STRING: ai-u-connection-string
COSMOS_KEY: afm-marketplace-u-cosmos-pkey
AFM_SA_CONNECTION_STRING: afm-u-sa-connection-string
ELASTIC_APM_SECRET_TOKEN: elastic-apm-secret-token
OTEL_EXPORTER_OTLP_HEADERS: elastic-apm-secret-token
keyvault:
name: "pagopa-u-afm-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
<artifactId>javaniotcpproxy</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>logback-ecs-encoder</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
package it.gov.pagopa.afm.calculator.config;

import java.util.Arrays;
import java.util.stream.StreamSupport;
import javax.annotation.PostConstruct;
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.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.stream.StreamSupport;

@Aspect
@Component
@Slf4j
public class LoggingAspect {

@Value("${info.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";

@Value("${info.application.artifactId}")
private String artifactId;

@Value("${info.application.version}")
private String version;

@Value("${info.properties.environment}")
private String environment;

@Autowired HttpServletRequest httRequest;

@Autowired HttpServletResponse httpResponse;

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restController() {
// all rest controllers
}

@Pointcut("execution(* it.gov.pagopa.afm.calculator.repository..*.*(..))")
@Pointcut("@within(org.springframework.stereotype.Repository)")
public void repository() {
// all repository methods
}

@Pointcut("execution(* it.gov.pagopa.afm.calculator.service..*.*(..))")
@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);
log.info("-> Starting {} version {} - environment {}", artifactId, version, environment);
}

/**
Expand All @@ -75,41 +90,46 @@ public void handleContextRefresh(ContextRefreshedEvent event) {
!(prop.toLowerCase().contains("credentials")
|| prop.toLowerCase().contains("password")
|| prop.toLowerCase().contains("pass")
|| prop.toLowerCase().contains("pwd")))
|| prop.toLowerCase().contains("pwd")
|| prop.toLowerCase().contains("key")
|| prop.toLowerCase().contains("secret")))
.forEach(prop -> log.debug("{}: {}", prop, env.getProperty(prop)));
}

@Before(value = "restController()")
public void logApiInvocation(JoinPoint joinPoint) {
@Around(value = "restController()")
public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable {
MDC.put(METHOD, joinPoint.getSignature().getName());
MDC.put(START_TIME, String.valueOf(System.currentTimeMillis()));
log.info("{} {}", httRequest.getMethod(), httRequest.getRequestURI());
log.info(
"Invoking API operation {} - args: {}",
joinPoint.getSignature().getName(),
joinPoint.getArgs());
}

@AfterReturning(value = "restController()", returning = "result")
public void returnApiInvocation(JoinPoint joinPoint, Object result) {
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(* it.gov.pagopa.afm.calculator.exception.ErrorHandler.*(..))",
returning = "result")
public void trowingApiInvocation(JoinPoint joinPoint, Object result) {
log.info("Failed API operation {} - error: {}", joinPoint.getSignature().getName(), result);
}

@Around(value = "repository() || service()")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
log.trace(
"Time taken for Execution of {} is: {}ms",
joinPoint.getSignature().toShortString(),
(endTime - startTime));
return result;
@AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result")
public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity<?> result) {
MDC.put(STATUS, "KO");
MDC.put(CODE, String.valueOf(result.getStatusCodeValue()));
MDC.put(RESPONSE_TIME, getExecutionTime());
log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result);
MDC.remove(STATUS);
MDC.remove(CODE);
MDC.remove(RESPONSE_TIME);
MDC.remove(START_TIME);
}

@Around(value = "repository() || service()")
Expand All @@ -120,4 +140,14 @@ public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable {
log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result);
return result;
}

private 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 "1";
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package it.gov.pagopa.afm.calculator.config;

import java.io.IOException;
import java.util.UUID;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
Expand Down Expand Up @@ -46,7 +43,6 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

// set requestId in MDC
MDC.put("requestId", requestId);
log.debug("{} {}", httRequest.getMethod(), httRequest.getRequestURI());

// set requestId in the response header
((HttpServletResponse) response).setHeader(HEADER_REQUEST_ID, requestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.azure.spring.data.cosmos.repository.CosmosRepository;
import it.gov.pagopa.afm.calculator.entity.PaymentType;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface PaymentTypeRepository extends CosmosRepository<PaymentType, String> {
Optional<PaymentType> findByName(String name);
}
Loading

0 comments on commit ea9aa58

Please sign in to comment.