Skip to content

Commit

Permalink
Merge pull request #14 from pagopa/logging-kibana
Browse files Browse the repository at this point in the history
Logging kibana
  • Loading branch information
jacopocarlini authored Jul 31, 2024
2 parents 5f08366 + a12f341 commit 832161f
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 86 deletions.
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: gpd-payments-pull
description: Microservice pagopa-gpd-payments-pull
type: application
version: 0.19.0
appVersion: 1.1.1
version: 0.22.0
appVersion: 1.1.1-3-logging-kibana
dependencies:
- name: microservice-chart
version: 2.8.0
Expand Down
6 changes: 3 additions & 3 deletions helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "gpd-payments-pull"
image:
repository: ghcr.io/pagopa/pagopa-gpd-payments-pull
tag: "1.1.1"
tag: "1.1.1-3-logging-kibana"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down Expand Up @@ -69,9 +69,9 @@ microservice-chart:
value: "70"
fileConfig: {}
envConfig:
APP_NAME: "gpd-payments-pull"
APP_NAME: "pagopa-gpd-payments-pull"
APP_ENVIRONMENT: "dev"
WEBSITE_SITE_NAME: 'gpd-payments-pull' # required to show cloud role name in application insights
WEBSITE_SITE_NAME: 'pagopa-gpd-payments-pull' # required to show cloud role name in application insights
APP_LOGGING_LEVEL: 'DEBUG'
DEFAULT_LOGGING_LEVEL: 'INFO'
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopa-gpd-payments-pull,deployment.environment=dev"
Expand Down
6 changes: 3 additions & 3 deletions helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "gpd-payments-pull"
image:
repository: ghcr.io/pagopa/pagopa-gpd-payments-pull
tag: "1.1.1"
tag: "1.1.1-3-logging-kibana"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down Expand Up @@ -69,9 +69,9 @@ microservice-chart:
value: "70"
fileConfig: {}
envConfig:
APP_NAME: "gpd-payments-pull"
APP_NAME: "pagopa-gpd-payments-pull"
APP_ENVIRONMENT: "prod"
WEBSITE_SITE_NAME: 'gpd-payments-pull' # required to show cloud role name in application insights
WEBSITE_SITE_NAME: 'pagopa-gpd-payments-pull' # required to show cloud role name in application insights
APP_LOGGING_LEVEL: 'DEBUG'
DEFAULT_LOGGING_LEVEL: 'INFO'
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopa-gpd-payments-pull,deployment.environment=prod"
Expand Down
6 changes: 3 additions & 3 deletions helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "gpd-payments-pull"
image:
repository: ghcr.io/pagopa/pagopa-gpd-payments-pull
tag: "1.1.1"
tag: "1.1.1-3-logging-kibana"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down Expand Up @@ -69,9 +69,9 @@ microservice-chart:
value: "70"
fileConfig: {}
envConfig:
APP_NAME: "gpd-payments-pull"
APP_NAME: "pagopa-gpd-payments-pull"
APP_ENVIRONMENT: "uat"
WEBSITE_SITE_NAME: 'gpd-payments-pull' # required to show cloud role name in application insights
WEBSITE_SITE_NAME: 'pagopa-gpd-payments-pull' # required to show cloud role name in application insights
APP_LOGGING_LEVEL: 'DEBUG'
DEFAULT_LOGGING_LEVEL: 'INFO'
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopa-gpd-payments-pull,deployment.environment=uat"
Expand Down
12 changes: 6 additions & 6 deletions infra/04_apim_api.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ locals {
// GPD Payments Pull
display_name = "GPD Payments Pull"
description = "API for GPD Payments Pull"
path = "gpd/payments/pull"
path = "pagopa-gpd-payments-pull"
subscription_required = true
service_url = null
}
Expand All @@ -17,7 +17,7 @@ locals {

resource "azurerm_api_management_api_version_set" "api_gpd_payments_pull_api" {

name = format("%s-gpd-payments-pull-api", var.env_short)
name = "pagopa-${var.env_short}-gpd-payments-pull-api"
resource_group_name = local.apim.rg
api_management_name = local.apim.name
display_name = local.apim_gpd_payments_pull.display_name
Expand All @@ -31,22 +31,22 @@ resource "azurerm_api_management_api_version_set" "api_gpd_payments_pull_api" {
module "apim_gpd_payments_pull_api_v1" {
source = "git::https://github.com/pagopa/terraform-azurerm-v3.git//api_management_api?ref=v6.4.1"

name = format("%s-gpd-payments-pull-api", var.env_short)
name = "pagopa-${var.env_short}-gpd-payments-pull-api"
api_management_name = local.apim.name
resource_group_name = local.apim.rg
product_ids = [local.apim.gpd_payments_pull_product_id]
product_ids = [local.apim.gpd_payments_pull_product_id]
subscription_required = local.apim_gpd_payments_pull.subscription_required
version_set_id = azurerm_api_management_api_version_set.api_gpd_payments_pull_api.id
api_version = "v1"

description = local.apim_gpd_payments_pull.description
display_name = local.apim_gpd_payments_pull.display_name
path = local.apim_gpd_payments_pull.path
protocols = ["https"]
protocols = ["https"]
service_url = local.apim_gpd_payments_pull.service_url

content_format = "openapi"
content_value = file("../openapi/openapi.json")
content_value = file("../openapi/openapi.json")

xml_content = templatefile("./policy/_base_policy.xml", {
hostname = local.hostname
Expand Down
2 changes: 1 addition & 1 deletion integration-test/src/config/.env.dev
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GPD_HOST=https://api.dev.platform.pagopa.it/gpd/api/v1
GPD_PULL_HOST=https://api.dev.platform.pagopa.it/gpd/payments/pull/v1
GPD_PULL_HOST=https://api.dev.platform.pagopa.it/gpd-payments-pull/v1

ORGANIZATIONAL_FISCAL_CODE=77777777777
IUV_OK=11101751670642134
Expand Down
2 changes: 1 addition & 1 deletion integration-test/src/config/.env.uat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GPD_HOST=https://api.uat.platform.pagopa.it/gpd/api/v1
GPD_PULL_HOST=https://api.uat.platform.pagopa.it/gpd/payments/pull/v1
GPD_PULL_HOST=https://api.uat.platform.pagopa.it/gpd-payments-pull/v1

ORGANIZATIONAL_FISCAL_CODE=77777777777
IUV_OK=03163674189686371
Expand Down
4 changes: 2 additions & 2 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"title": "gpd-payments-pull",
"description": "GPD Payments Pull Services",
"termsOfService": "https://www.pagopa.gov.it/",
"version": "1.1.1"
"version": "1.1.1-3-logging-kibana"
},
"servers": [
{
"url": "http://localhost:8080",
"description": "Localhost base URL"
},
{
"url": "https://{host}/gpd/payments/pull/api/v1",
"url": "https://{host}/gpd-payments-pull/api/v1",
"description": "Base URL",
"variables": {
"host": {
Expand Down
10 changes: 4 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion>

<groupId>it.gov.pagopa</groupId>
<artifactId>gpd-payments-pull</artifactId>
<name>GPD Payments Pull Services</name>
<version>1.1.1</version>
<artifactId>pagopa-gpd-payments-pull</artifactId>
<name>PagoPA GPD Payments Pull Services</name>
<version>1.1.1-3-logging-kibana</version>

<properties>
<jacoco.version>0.8.7</jacoco.version>
Expand Down Expand Up @@ -57,11 +57,9 @@
</dependency>

<dependency>
<groupId>io.quarkiverse.loggingjson</groupId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-logging-json</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/it/gov/pagopa/gpd/payments/pull/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
info = @Info(title = "GPD Payments Pull Services", version = "${quarkus.application.version}"),
servers = {
@Server(url = "http://localhost:8080", description = "Localhost base URL"),
@Server(url = "https://{host}/gpd/payments/pull/api/v1", description = "Base URL",
@Server(url = "https://{host}/gpd-payments-pull/api/v1", description = "Base URL",
variables = {
@ServerVariable(name = "host",
enumeration = {"api.dev.platform.pagopa.it","api.uat.platform.pagopa.it","api.platform.pagopa.it"},
defaultValue = "api.dev.platform.pagopa.it")})
@ServerVariable(name = "host",
enumeration = {"api.dev.platform.pagopa.it", "api.uat.platform.pagopa.it", "api.platform.pagopa.it"},
defaultValue = "api.dev.platform.pagopa.it")})
},
components =
@Components(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import it.gov.pagopa.gpd.payments.pull.models.ErrorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static io.quarkiverse.loggingjson.providers.KeyValueStructuredArgument.kv;
import static it.gov.pagopa.gpd.payments.pull.util.CommonUtil.mapToJSON;

@Logged
Expand All @@ -28,52 +27,45 @@ private static String getParams(InvocationContext context) {
for (int i = 0; i < context.getParameters().length; i++) {
String name = context.getMethod().getParameters()[i].getName();
Object value = context.getParameters()[i];
if("taxCode".equals(name) && value != null) {
if ("taxCode".equals(name) && value != null) {
value = value.hashCode();
}
params.put(name, value);
}
return mapToJSON(params);

}

@AroundInvoke
Object logInvocation(InvocationContext context) throws Exception {
var startTime = System.currentTimeMillis();

String args = getParams(context);

Object ret = null;
Object ret;
try {
ret = context.proceed();
} catch (Exception e) {
var endTime = System.currentTimeMillis();
int httpCode = 400;
String faultCode = "UNKNOWN";
if(e instanceof PaymentNoticeException ex) {
if (e instanceof PaymentNoticeException ex) {
faultCode = ex.getErrorCode().getErrorCode();
httpCode = ex.getErrorCode().getStatus().getStatusCode();
}

logger.error("Failed API Invocation getPaymentNotices",
kv("method", "getPaymentNotices"),
kv("startTime", startTime),
kv("args", mapToJSON(args)),
kv("responseTime", endTime - startTime),
kv("status", "KO"),
kv("httpCode", httpCode),
kv("requestId", UUID.randomUUID().toString()),
kv("operationId", UUID.randomUUID().toString()),
kv("faultCode", faultCode),
kv("faultDetail", e.getMessage()),
kv("response", mapToJSON(buildErrorResponse(httpCode, faultCode, e.getMessage()))));
MDC.put("responseTime", String.valueOf(endTime - startTime));
MDC.put("status", "KO");
MDC.put("httpCode", String.valueOf(httpCode));
MDC.put("faultCode", faultCode);
MDC.put("faultDetail", e.getMessage());
MDC.put("args", mapToJSON(args));
MDC.put("response", mapToJSON(buildErrorResponse(httpCode, faultCode, e.getMessage())));
logger.error("Failed API Invocation getPaymentNotices");
throw e;
}

return ret;
}


private ErrorResponse buildErrorResponse(int status, String errorCode, String message) {
return ErrorResponse.builder()
.title(String.valueOf(status))
Expand All @@ -82,5 +74,4 @@ private ErrorResponse buildErrorResponse(int status, String errorCode, String me
.instance(errorCode)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import java.util.List;

import static it.gov.pagopa.gpd.payments.pull.exception.PaymentNoticeException.getHttpStatus;
import static it.gov.pagopa.gpd.payments.pull.util.CommonUtil.mapToJSON;
import static org.jboss.resteasy.reactive.RestResponse.Status.BAD_REQUEST;
import static org.jboss.resteasy.reactive.RestResponse.StatusCode.INTERNAL_SERVER_ERROR;

public class ExceptionMapper {

private static final String FAULT_CODE = "faultCode";
private static final String FAULT_DETAIL = "faultDetail";
private static final String RESPONSE = "response";
private static final String STATUS = "status";

Logger logger = LoggerFactory.getLogger(ExceptionMapper.class);

private ErrorResponse buildErrorResponse(Response.Status status, AppErrorCodeEnum errorCode, String message) {
Expand All @@ -31,14 +38,13 @@ private ErrorResponse buildErrorResponse(Response.Status status, AppErrorCodeEnu

@ServerExceptionMapper
public Response mapCompositeException(CompositeException exception) {
logger.error(exception.getMessage(), exception);
Exception composedException;
List<Throwable> causes = exception.getCauses();
composedException = (Exception) causes.get(causes.size() - 1);

if(composedException instanceof NotFoundException ex) {
if (composedException instanceof NotFoundException ex) {
return mapNotFoundException(ex);
} else if(composedException instanceof PaymentNoticeException paymentNoticeException) {
} else if (composedException instanceof PaymentNoticeException paymentNoticeException) {
return mapPaymentNoticeException(paymentNoticeException);
} else {
return mapGenericException(exception);
Expand All @@ -48,29 +54,56 @@ public Response mapCompositeException(CompositeException exception) {

@ServerExceptionMapper
public Response mapNotFoundException(NotFoundException exception) {
ErrorResponse errorResponse = buildErrorResponse(Response.Status.BAD_REQUEST, AppErrorCodeEnum.PPL_600,
"Invalid parameters on request");

MDC.put(FAULT_CODE, "400");
MDC.put(FAULT_DETAIL, getDetails(exception));
MDC.put(RESPONSE, mapToJSON(errorResponse));
MDC.put(STATUS, "KO");
logger.error(exception.getMessage(), exception);

return Response.status(BAD_REQUEST).entity(
buildErrorResponse(Response.Status.BAD_REQUEST, AppErrorCodeEnum.PPL_600,
"Invalid parameters on request")).build();
errorResponse).build();
}

@ServerExceptionMapper
public Response mapPaymentNoticeException(PaymentNoticeException exception) {
logger.error(exception.getMessage(), exception);
Response.Status status = getHttpStatus(exception);
return Response.status(status).entity(buildErrorResponse(status,
exception.getErrorCode(), exception.getMessage())).build();
ErrorResponse errorResponse = buildErrorResponse(status,
exception.getErrorCode(), exception.getMessage());
MDC.put(FAULT_CODE, String.valueOf(exception.getErrorCode()));
MDC.put(FAULT_DETAIL, getDetails(exception));
MDC.put(RESPONSE, mapToJSON(errorResponse));
MDC.put(STATUS, "KO");
logger.error(exception.getMessage(), exception);
return Response.status(status)
.entity(errorResponse)
.build();
}


@ServerExceptionMapper
public Response mapGenericException(Exception exception) {
ErrorResponse errorResponse = buildErrorResponse(
Response.Status.INTERNAL_SERVER_ERROR,
AppErrorCodeEnum.PPL_900,
"Unexpected Error");
logger.error(exception.getMessage(), exception);
MDC.put(FAULT_CODE, "500");
MDC.put(FAULT_DETAIL, getDetails(exception));
MDC.put(RESPONSE, mapToJSON(errorResponse));
MDC.put(STATUS, "KO");
return Response.status(INTERNAL_SERVER_ERROR)
.entity(buildErrorResponse(
Response.Status.INTERNAL_SERVER_ERROR,
AppErrorCodeEnum.PPL_900,
"Unexpected Error"))
.entity(errorResponse)
.build();
}

private String getDetails(Exception exception) {
String message = exception.getMessage();
var length = message.length();
var maxLength = Math.min(length, 200);
return message.substring(0, maxLength);
}

}
Loading

0 comments on commit 832161f

Please sign in to comment.