Skip to content

Commit

Permalink
Support userContext for additional info in access log
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster committed Aug 8, 2024
1 parent 9655952 commit ed3ac3f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,10 +23,13 @@ public class AccessLogContext {
Integer contentLength;
Long responseTime;
Integer responseStatus;
String method;
String protocol;
Supplier<Map<String,String>> userContext;

@Builder.Default
Map<String,String> 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.
Expand All @@ -43,6 +48,8 @@ private Map<String,Object> getValueMap() {
params.put("headers." + entry.getKey(), entry.getValue());
}
params.put("thread", Thread.currentThread().getName());
if (userContext != null)
params.putAll(userContext.get());
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class RequestParams<M> {
// 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public void doProcessRequest(ServletRequest req, RequestParams<Map<String,String
accessLogContextBuilder = AccessLogContext.builder()
.clientIp(requestParamsInput.getClientIp())
.resourcePath(requestParamsInput.getResourcePath())
.protocol(req.getProtocol())
.method(requestParamsInput.getMethod())
.headers(requestParamsInput.getMetadata());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class AccessLogContextTest {
public void setUp() {
}

private String template = "Formatted {clientIp} {resourcePath} {contentLength} {responseTime} {headers.x-header}";

@Test
public void testSimpleRender() {
String template = "Formatted {clientIp} {resourcePath} {contentLength} {responseTime} {headers.x-header}";
AccessLogContext context =
AccessLogContext.builder()
.clientIp("127.0.0.1")
Expand All @@ -28,4 +28,20 @@ public void testSimpleRender() {
String result = context.format(template);
Assert.assertEquals("Formatted 127.0.0.1 /path 100 1000 value", result);
}

@Test
public void testRenderUserContext() {
String template = "Formatted {clientIp} - {user} {resourcePath} {contentLength} {responseTime} {headers.x-header}";
AccessLogContext context =
AccessLogContext.builder()
.clientIp("127.0.0.1")
.resourcePath("/path")
.responseTime(1000L)
.contentLength(100)
.headers(Collections.singletonMap("x-header", "value"))
.userContext(() -> Collections.singletonMap("user", "username"))
.build();
String result = context.format(template);
Assert.assertEquals("Formatted 127.0.0.1 - username /path 100 1000 value", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, String>> requestParams = requestParamsBuilder.build();
Expand Down

0 comments on commit ed3ac3f

Please sign in to comment.