From ed3ac3f561b4f304ef924dd0f73d48661a83d760 Mon Sep 17 00:00:00 2001 From: Kinshuk Bairagi Date: Thu, 8 Aug 2024 10:45:40 +0530 Subject: [PATCH] Support userContext for additional info in access log --- .../gjex/core/context/AccessLogContext.java | 9 ++++++++- .../gjex/core/filter/RequestParams.java | 3 +++ .../core/filter/http/AccessLogHttpFilter.java | 2 ++ .../core/context/AccessLogContextTest.java | 18 +++++++++++++++++- .../interceptor/HttpFilterInterceptor.java | 1 + 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java b/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java index 6873357d..306ac43a 100644 --- a/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java +++ b/core/src/main/java/com/flipkart/gjex/core/context/AccessLogContext.java @@ -8,6 +8,8 @@ import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; /** * Represents the context for access logs, encapsulating details such as client IP, resource path, @@ -21,10 +23,13 @@ public class AccessLogContext { Integer contentLength; Long responseTime; Integer responseStatus; + String method; + String protocol; + Supplier> userContext; + @Builder.Default Map headers = new HashMap<>(); - /** * Retrieves a map of field names to their values for the current instance. * This includes the fields of the class and the headers map, as well as the current thread name. @@ -43,6 +48,8 @@ private Map getValueMap() { params.put("headers." + entry.getKey(), entry.getValue()); } params.put("thread", Thread.currentThread().getName()); + if (userContext != null) + params.putAll(userContext.get()); return params; } diff --git a/core/src/main/java/com/flipkart/gjex/core/filter/RequestParams.java b/core/src/main/java/com/flipkart/gjex/core/filter/RequestParams.java index e81bbba0..fa2d4615 100644 --- a/core/src/main/java/com/flipkart/gjex/core/filter/RequestParams.java +++ b/core/src/main/java/com/flipkart/gjex/core/filter/RequestParams.java @@ -38,6 +38,9 @@ public class RequestParams { // Path of the resource being requested. String resourcePath; + // method of the request. + String method; + // Metadata associated with the request, of generic type M. M metadata; } diff --git a/core/src/main/java/com/flipkart/gjex/core/filter/http/AccessLogHttpFilter.java b/core/src/main/java/com/flipkart/gjex/core/filter/http/AccessLogHttpFilter.java index c4da84b9..569593a2 100644 --- a/core/src/main/java/com/flipkart/gjex/core/filter/http/AccessLogHttpFilter.java +++ b/core/src/main/java/com/flipkart/gjex/core/filter/http/AccessLogHttpFilter.java @@ -63,6 +63,8 @@ public void doProcessRequest(ServletRequest req, RequestParams Collections.singletonMap("user", "username")) + .build(); + String result = context.format(template); + Assert.assertEquals("Formatted 127.0.0.1 - username /path 100 1000 value", result); + } } diff --git a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java index c0ea37c7..a82043ba 100644 --- a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java +++ b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java @@ -73,6 +73,7 @@ public final void doFilter(ServletRequest request, ServletResponse response, .stream().collect(Collectors.toMap(h -> h, httpServletRequest::getHeader)); requestParamsBuilder.metadata(headers); requestParamsBuilder.clientIp(getClientIp(request)); + requestParamsBuilder.method(httpServletRequest.getMethod()); requestParamsBuilder.resourcePath(httpServletRequest.getRequestURI()); } RequestParams> requestParams = requestParamsBuilder.build();