Skip to content

Commit

Permalink
NOD-784 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsca committed Apr 4, 2024
1 parent a1f911b commit 82055c4
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package it.gov.pagopa.wispconverter.config.client;

import it.gov.pagopa.wispconverter.service.ReService;
import it.gov.pagopa.wispconverter.util.client.checkout.CheckoutClientLogging;
import it.gov.pagopa.wispconverter.util.client.MDCInterceptor;
import it.gov.pagopa.wispconverter.util.client.ReInterceptor;
import it.gov.pagopa.wispconverter.util.client.checkout.CheckoutClientLoggingInterceptor;
import it.gov.pagopa.wispconverter.util.client.checkout.CheckoutClientResponseErrorHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,8 +21,11 @@

@Configuration
@Slf4j
@RequiredArgsConstructor
public class CheckoutClientConfig {

private final ReService reService;

@Value("${client.checkout.read-timeout}")
private Integer readTimeout;

Expand Down Expand Up @@ -56,7 +62,7 @@ public class CheckoutClientConfig {

@Bean
public it.gov.pagopa.checkoutclient.client.ApiClient checkoutClient() {
CheckoutClientLogging clientLogging = new CheckoutClientLogging();
CheckoutClientLoggingInterceptor clientLogging = new CheckoutClientLoggingInterceptor();
clientLogging.setRequestIncludeHeaders(clientRequestIncludeHeaders);
clientLogging.setRequestIncludePayload(clientRequestIncludePayload);
clientLogging.setRequestMaxPayloadLength(clientRequestMaxLength);
Expand All @@ -71,14 +77,16 @@ public it.gov.pagopa.checkoutclient.client.ApiClient checkoutClient() {
RestTemplate restTemplate = restTemplate();

List<ClientHttpRequestInterceptor> currentInterceptors = restTemplate.getInterceptors();
currentInterceptors.add(new MDCInterceptor());
currentInterceptors.add(new ReInterceptor(reService));
currentInterceptors.add(clientLogging);
restTemplate.setInterceptors(currentInterceptors);

restTemplate.setErrorHandler(new CheckoutClientResponseErrorHandler());

it.gov.pagopa.checkoutclient.client.ApiClient client = new it.gov.pagopa.checkoutclient.client.ApiClient(restTemplate);
client.setBasePath(basePath);
client.setBasePath(apiKey);
// client.setApiKey(apiKey);

return client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package it.gov.pagopa.wispconverter.config.client;

import it.gov.pagopa.wispconverter.service.ReService;
import it.gov.pagopa.wispconverter.util.client.decouplercaching.DecouplerCachingClientLogging;
import it.gov.pagopa.wispconverter.util.client.MDCInterceptor;
import it.gov.pagopa.wispconverter.util.client.ReInterceptor;
import it.gov.pagopa.wispconverter.util.client.decouplercaching.DecouplerCachingClientLoggingInterceptor;
import it.gov.pagopa.wispconverter.util.client.decouplercaching.DecouplerCachingClientResponseErrorHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,7 +21,9 @@

@Configuration
@Slf4j
@RequiredArgsConstructor
public class DecouplerCachingClientConfig {
private final ReService reService;

@Value("${client.decoupler-caching.read-timeout}")
private Integer readTimeout;
Expand Down Expand Up @@ -56,7 +61,7 @@ public class DecouplerCachingClientConfig {

@Bean
public it.gov.pagopa.decouplercachingclient.client.ApiClient decouplerCachingClient() {
DecouplerCachingClientLogging clientLogging = new DecouplerCachingClientLogging();
DecouplerCachingClientLoggingInterceptor clientLogging = new DecouplerCachingClientLoggingInterceptor();
clientLogging.setRequestIncludeHeaders(clientRequestIncludeHeaders);
clientLogging.setRequestIncludePayload(clientRequestIncludePayload);
clientLogging.setRequestMaxPayloadLength(clientRequestMaxLength);
Expand All @@ -71,6 +76,8 @@ public it.gov.pagopa.decouplercachingclient.client.ApiClient decouplerCachingCli
RestTemplate restTemplate = restTemplate();

List<ClientHttpRequestInterceptor> currentInterceptors = restTemplate.getInterceptors();
currentInterceptors.add(new MDCInterceptor());
currentInterceptors.add(new ReInterceptor(reService));
currentInterceptors.add(clientLogging);
restTemplate.setInterceptors(currentInterceptors);

Expand All @@ -79,7 +86,7 @@ public it.gov.pagopa.decouplercachingclient.client.ApiClient decouplerCachingCli
it.gov.pagopa.decouplercachingclient.client.ApiClient client = new it.gov.pagopa.decouplercachingclient.client.ApiClient(restTemplate);

client.setBasePath(basePath);
client.setBasePath(apiKey);
client.setApiKey(apiKey);

return client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package it.gov.pagopa.wispconverter.config.client;

import it.gov.pagopa.wispconverter.service.ReService;
import it.gov.pagopa.wispconverter.util.client.gpd.GpdClientLogging;
import it.gov.pagopa.wispconverter.util.client.MDCInterceptor;
import it.gov.pagopa.wispconverter.util.client.ReInterceptor;
import it.gov.pagopa.wispconverter.util.client.gpd.GpdClientLoggingInterceptor;
import it.gov.pagopa.wispconverter.util.client.gpd.GpdClientResponseErrorHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,7 +21,9 @@

@Configuration
@Slf4j
@RequiredArgsConstructor
public class GpdClientConfig {
private final ReService reService;

@Value("${client.gpd.read-timeout}")
private Integer readTimeout;
Expand Down Expand Up @@ -56,7 +61,7 @@ public class GpdClientConfig {

@Bean
public it.gov.pagopa.gpdclient.client.ApiClient gpdClient() {
GpdClientLogging clientLogging = new GpdClientLogging();
GpdClientLoggingInterceptor clientLogging = new GpdClientLoggingInterceptor();
clientLogging.setRequestIncludeHeaders(clientRequestIncludeHeaders);
clientLogging.setRequestIncludePayload(clientRequestIncludePayload);
clientLogging.setRequestMaxPayloadLength(clientRequestMaxLength);
Expand All @@ -71,6 +76,8 @@ public it.gov.pagopa.gpdclient.client.ApiClient gpdClient() {
RestTemplate restTemplate = restTemplate();

List<ClientHttpRequestInterceptor> currentInterceptors = restTemplate.getInterceptors();
currentInterceptors.add(new MDCInterceptor());
currentInterceptors.add(new ReInterceptor(reService));
currentInterceptors.add(clientLogging);
restTemplate.setInterceptors(currentInterceptors);

Expand All @@ -79,7 +86,7 @@ public it.gov.pagopa.gpdclient.client.ApiClient gpdClient() {
it.gov.pagopa.gpdclient.client.ApiClient client = new it.gov.pagopa.gpdclient.client.ApiClient(restTemplate);

client.setBasePath(basePath);
client.setBasePath(apiKey);
client.setApiKey(apiKey);

return client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package it.gov.pagopa.wispconverter.config.client;

import it.gov.pagopa.wispconverter.service.ReService;
import it.gov.pagopa.wispconverter.util.client.iuvgenerator.IuvGeneratorClientLogging;
import it.gov.pagopa.wispconverter.util.client.MDCInterceptor;
import it.gov.pagopa.wispconverter.util.client.iuvgenerator.IuvGeneratorClientLoggingInterceptor;
import it.gov.pagopa.wispconverter.util.client.iuvgenerator.IuvGeneratorClientResponseErrorHandler;
import it.gov.pagopa.wispconverter.util.client.ReInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,7 +21,9 @@

@Configuration
@Slf4j
@RequiredArgsConstructor
public class IuvGeneratorClientConfig {
private final ReService reService;

@Value("${client.iuvgenerator.read-timeout}")
private Integer readTimeout;
Expand Down Expand Up @@ -57,7 +62,7 @@ public class IuvGeneratorClientConfig {

@Bean
public it.gov.pagopa.iuvgeneratorclient.client.ApiClient iuvGeneratorClient() {
IuvGeneratorClientLogging clientLogging = new IuvGeneratorClientLogging();
IuvGeneratorClientLoggingInterceptor clientLogging = new IuvGeneratorClientLoggingInterceptor();
clientLogging.setRequestIncludeHeaders(clientRequestIncludeHeaders);
clientLogging.setRequestIncludePayload(clientRequestIncludePayload);
clientLogging.setRequestMaxPayloadLength(clientRequestMaxLength);
Expand All @@ -72,14 +77,16 @@ public it.gov.pagopa.iuvgeneratorclient.client.ApiClient iuvGeneratorClient() {
RestTemplate restTemplate = restTemplate();

List<ClientHttpRequestInterceptor> currentInterceptors = restTemplate.getInterceptors();
currentInterceptors.add(new MDCInterceptor());
currentInterceptors.add(new ReInterceptor(reService));
currentInterceptors.add(clientLogging);
restTemplate.setInterceptors(currentInterceptors);

restTemplate.setErrorHandler(new IuvGeneratorClientResponseErrorHandler());

it.gov.pagopa.iuvgeneratorclient.client.ApiClient client = new it.gov.pagopa.iuvgeneratorclient.client.ApiClient(restTemplate);
client.setBasePath(basePath);
client.setBasePath(apiKey);
// client.setApiKey(apiKey);

return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public enum AppErrorCodeMessageEnum {
PARSE_ERROR (1000, "Parse error", "Error while parsing: {0}", HttpStatus.BAD_REQUEST),
PRIMITIVE_NOT_VALID (1001, "Primitive not valid", "Primitive [{0}] not valid", HttpStatus.NOT_ACCEPTABLE),
RPT_NOT_FOUND (1002, "RPT not found", "RPT with sessionId [{0}] not found", HttpStatus.NOT_FOUND),
CLIENT_IUV_GENERATOR (1003, "IUVGeneratorClient error", "IUVGeneratorClient status [{0}] - {1}", HttpStatus.EXPECTATION_FAILED),
CLIENT_GPD (1004, "GPDClient error", "GPDClient status [{0}] - {1}", HttpStatus.EXPECTATION_FAILED),
CLIENT_DECOUPLER_CACHING(1005, "DecouplerCachingClient error", "DecouplerCachingClient status [{0}] - {1}",HttpStatus.EXPECTATION_FAILED),
CLIENT_CHECKOUT (1006, "CheckoutClient error", "CheckoutClient status [{0}] - {1}", HttpStatus.EXPECTATION_FAILED),
CLIENT_IUV_GENERATOR (1003, "IUVGeneratorClient error", "IUVGeneratorClient - {0}", HttpStatus.EXPECTATION_FAILED),
CLIENT_GPD (1004, "GPDClient error", "GPDClient - {0}", HttpStatus.EXPECTATION_FAILED),
CLIENT_DECOUPLER_CACHING(1005, "DecouplerCachingClient error", "DecouplerCachingClient - {0}", HttpStatus.EXPECTATION_FAILED),
CLIENT_CHECKOUT (1006, "CheckoutClient error", "CheckoutClient - {0}", HttpStatus.EXPECTATION_FAILED),
UNZIP (1007, "Unzip error", "{0}", HttpStatus.INTERNAL_SERVER_ERROR),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public class Constants {
public static final String MDC_OPERATION_ID = "operationId";

public static final String MDC_CLIENT_OPERATION_ID = "clientOperationId";
public static final String MDC_CLIENT_EXECUTION_TIME = "clientExecutionTime";

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

@Getter
@Setter
public abstract class AbstractAppClientLogging implements ClientHttpRequestInterceptor {
public abstract class AbstractAppClientLoggingInterceptor implements ClientHttpRequestInterceptor {

public static final String REQUEST_DEFAULT_MESSAGE_PREFIX = "===> CLIENT Request CLIENT_OPERATION_ID=%s of OPERATION_ID=%s - ";
public static final String RESPONSE_DEFAULT_MESSAGE_PREFIX = "<=== CLIENT Response CLIENT_OPERATION_ID=%s of OPERATION_ID=%s -";
public static final String REQUEST_DEFAULT_MESSAGE_PREFIX = "===> CLIENT Request OPERATION_ID=%s, CLIENT_OPERATION_ID=%s - ";
public static final String RESPONSE_DEFAULT_MESSAGE_PREFIX = "<=== CLIENT Response OPERATION_ID=%s, CLIENT_OPERATION_ID=%s -";
private static final int REQUEST_DEFAULT_MAX_PAYLOAD_LENGTH = 50;
private static final int RESPONSE_DEFAULT_MAX_PAYLOAD_LENGTH = 50;

Expand Down Expand Up @@ -57,26 +57,27 @@ public abstract class AbstractAppClientLogging implements ClientHttpRequestInter
private boolean responsePretty = false;

@Override
public ClientHttpResponse intercept(HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex) throws IOException {
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

String startClient = String.valueOf(System.currentTimeMillis());
String clientOperationId = UUID.randomUUID().toString();
MDC.put(Constants.MDC_CLIENT_OPERATION_ID, clientOperationId);
String operationId = MDC.get(Constants.MDC_OPERATION_ID);

request(clientOperationId, operationId, req, reqBody);
ClientHttpResponse response = ex.execute(req, reqBody);
request(clientOperationId, operationId, request, body);
ClientHttpResponse response = execution.execute(request, body);

String executionClientTime = CommonUtility.getExecutionTime(startClient);
response(clientOperationId, operationId, executionClientTime, req, response);
MDC.put(Constants.MDC_CLIENT_EXECUTION_TIME, executionClientTime);

response(clientOperationId, operationId, executionClientTime, request, response);
return response;
}


public String createRequestMessage(String clientOperationId, String operationId, HttpRequest request, byte[] reqBody) {
StringBuilder msg = new StringBuilder();
msg.append(String.format(REQUEST_DEFAULT_MESSAGE_PREFIX, clientOperationId, operationId));
msg.append(String.format(REQUEST_DEFAULT_MESSAGE_PREFIX, operationId, clientOperationId));
if(isRequestPretty()){
msg.append(PRETTY_OUT).append(SPACE);
}
Expand Down Expand Up @@ -125,7 +126,7 @@ public String createRequestMessage(String clientOperationId, String operationId,
public String createResponseMessage(String clientOperationId, String operationId, String clientExecutionTime, HttpRequest request, ClientHttpResponse response)
throws IOException {
StringBuilder msg = new StringBuilder();
msg.append(String.format(RESPONSE_DEFAULT_MESSAGE_PREFIX, clientOperationId, operationId));
msg.append(String.format(RESPONSE_DEFAULT_MESSAGE_PREFIX, operationId, clientOperationId));
if(isResponsePretty()){
msg.append(PRETTY_IN).append(SPACE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package it.gov.pagopa.wispconverter.util.client;

import it.gov.pagopa.wispconverter.util.Constants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
public class MDCInterceptor implements ClientHttpRequestInterceptor {


@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

ClientHttpResponse response = execution.execute(request, body);

MDC.remove(Constants.MDC_CLIENT_OPERATION_ID);
MDC.remove(Constants.MDC_CLIENT_EXECUTION_TIME);

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package it.gov.pagopa.wispconverter.util.client;

import it.gov.pagopa.wispconverter.service.ReService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
public class ReInterceptor implements ClientHttpRequestInterceptor {


private final ReService reService;

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

MDC.getCopyOfContextMap().forEach((k, v) -> {
log.debug(String.format("BEFORE MDC %s=%s",k, v));
});
reService.addRe("CLIENT IN");
ClientHttpResponse response = execution.execute(request, body);

MDC.getCopyOfContextMap().forEach((k,v) -> {
log.debug(String.format("AFTER MDC %s=%s",k, v));
});
reService.addRe("CLIENT OUT");

return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.gov.pagopa.wispconverter.util.client.checkout;

import it.gov.pagopa.wispconverter.util.client.AbstractAppClientLogging;
import it.gov.pagopa.wispconverter.util.client.AbstractAppClientLoggingInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -9,7 +9,7 @@

@Slf4j
@RequiredArgsConstructor
public class CheckoutClientLogging extends AbstractAppClientLogging {
public class CheckoutClientLoggingInterceptor extends AbstractAppClientLoggingInterceptor {


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ else if (statusCode.is5xxServerError()) {
ex = new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
}

throw new AppException(ex, AppErrorCodeMessageEnum.CLIENT_CHECKOUT, statusCode.value(), message) ;
throw new AppException(ex, AppErrorCodeMessageEnum.CLIENT_CHECKOUT, message) ;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.gov.pagopa.wispconverter.util.client.decouplercaching;

import it.gov.pagopa.wispconverter.util.client.AbstractAppClientLogging;
import it.gov.pagopa.wispconverter.util.client.AbstractAppClientLoggingInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -9,7 +9,7 @@

@Slf4j
@RequiredArgsConstructor
public class DecouplerCachingClientLogging extends AbstractAppClientLogging {
public class DecouplerCachingClientLoggingInterceptor extends AbstractAppClientLoggingInterceptor {


@Override
Expand Down
Loading

0 comments on commit 82055c4

Please sign in to comment.