From 2d80b9d86c01b27fd8f03452d96dcc2947289ec2 Mon Sep 17 00:00:00 2001 From: "a.ebrahimi" Date: Mon, 8 Apr 2024 17:00:45 +0330 Subject: [PATCH 1/2] [feature]add X-Request-ID and X-User-IP fields to headers. the value of X-Request-ID is extracted from requestId field in MDC. the value of X-User-IP is extracted from clientIP field in MDC. --- .../java/com/tosan/client/http/core/Constants.java | 6 ++++++ .../configuration/AbstractFeignConfiguration.java | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java b/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java index f88ebd1..3af9c4e 100644 --- a/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java +++ b/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java @@ -10,4 +10,10 @@ public interface Constants { SecureParameter AUTHORIZATION_SECURE_PARAM = new SecureParameter("authorization", MaskType.COMPLETE); SecureParameter PROXY_AUTHORIZATION_SECURE_PARAM = new SecureParameter("proxy-authorization", MaskType.COMPLETE); + String X_USER_IP = "X-User-IP"; + String MDC_CLIENT_IP = "clientIP"; + String MDC_REQUEST_ID = "requestId"; + String X_REQUEST_ID = "X-Request-ID"; + String ACCEPT_HEADER = "Accept"; + String CONTENT_TYPE_HEADER = "Content-Type"; } diff --git a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java index 068c7df..2982a05 100644 --- a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java +++ b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java @@ -27,6 +27,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.core5.http.ContentType; +import org.slf4j.MDC; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.AnnotatedParameterProcessor; @@ -49,6 +50,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import static com.tosan.client.http.core.Constants.*; import static com.tosan.tools.mask.starter.configuration.MaskBeanConfiguration.SECURED_PARAMETERS; /** @@ -110,8 +112,14 @@ public Client feignClient(HttpClient httpClient) { public RequestInterceptor requestInterceptor() { return requestTemplate -> { - requestTemplate.header("Accept", ContentType.APPLICATION_JSON.getMimeType()); - requestTemplate.header("Content-Type", ContentType.APPLICATION_JSON.getMimeType()); + requestTemplate.header(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType()); + requestTemplate.header(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType()); + if (MDC.get(MDC_REQUEST_ID) != null) { + requestTemplate.header(X_REQUEST_ID, MDC.get(MDC_REQUEST_ID)); + } + if (MDC.get(MDC_CLIENT_IP) != null) { + requestTemplate.header(X_USER_IP, MDC.get(MDC_CLIENT_IP)); + } }; } From 1fab8c2660b369ba769ddf7585778ba9b7d2ba2c Mon Sep 17 00:00:00 2001 From: "a.ebrahimi" Date: Sun, 30 Jun 2024 23:10:19 +0330 Subject: [PATCH 2/2] [bug]adding duration filed for successful and unsuccessful service calls in rest template implementation --- .../starter/impl/interceptor/HttpLoggingInterceptor.java | 7 +++++-- .../starter/util/HttpLoggingInterceptorUtil.java | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/impl/interceptor/HttpLoggingInterceptor.java b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/impl/interceptor/HttpLoggingInterceptor.java index 9c5636c..fbf09f3 100644 --- a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/impl/interceptor/HttpLoggingInterceptor.java +++ b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/impl/interceptor/HttpLoggingInterceptor.java @@ -32,18 +32,21 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] requestBody, Cli log.info(httpLoggingInterceptorUtil.getRequestDetailContent(request, requestBody, webServiceName)); } ClientHttpResponse response; + long startTime = System.currentTimeMillis(); try { response = ex.execute(request, requestBody); if (log.isInfoEnabled()) { HttpResponseWrapper responseWrapper = new HttpResponseWrapper(response); - log.info(httpLoggingInterceptorUtil.getResponseDetailContent(responseWrapper, webServiceName)); + log.info(httpLoggingInterceptorUtil.getResponseDetailContent(responseWrapper, webServiceName, + System.currentTimeMillis() - startTime)); return responseWrapper; } else { return response; } } catch (IOException e) { if (log.isInfoEnabled()) { - log.info(httpLoggingInterceptorUtil.getExceptionDetailContent(e, webServiceName)); + log.info(httpLoggingInterceptorUtil.getExceptionDetailContent(e, webServiceName, + System.currentTimeMillis() - startTime)); } throw new HttpClientRequestExecuteException(e.getMessage(), e); } diff --git a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java index 9a54471..047422c 100644 --- a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java +++ b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java @@ -65,9 +65,10 @@ public String getRequestDetailContent(HttpRequest request, byte[] body, String w return toJson(requestData); } - public String getResponseDetailContent(ClientHttpResponse response, String webServiceName) throws IOException { + public String getResponseDetailContent(ClientHttpResponse response, String webServiceName, long elapsedTime) throws IOException { final Map responseData = new LinkedHashMap<>(); responseData.put("invoked", webServiceName); + responseData.put("duration", elapsedTime / 1000.0 + "s"); responseData.put("status", response.getStatusCode()); if (!response.getHeaders().isEmpty()) { responseData.put("headers", getMaskedHeaders(response.getHeaders())); @@ -80,9 +81,10 @@ public String getResponseDetailContent(ClientHttpResponse response, String webSe return toJson(responseData); } - public String getExceptionDetailContent(Exception exception, String webServiceName) { + public String getExceptionDetailContent(Exception exception, String webServiceName, long elapsedTime) { final Map exceptionData = new LinkedHashMap<>(); exceptionData.put("invoked", webServiceName); + exceptionData.put("duration", elapsedTime / 1000.0 + "s"); exceptionData.put("exception", exception.getClass().getSimpleName()); exceptionData.put("message", exception.getMessage()); return toJson(exceptionData);