Skip to content

Commit

Permalink
:logging Extracting request_query_string to a separate MDC field
Browse files Browse the repository at this point in the history
  • Loading branch information
iakunin committed Mar 11, 2024
1 parent 088ee97 commit e88cc15
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/logging/logging-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jar.enabled = true

archivesBaseName = "library-logging-common"

project.version = "1.0.0"
project.version = "1.0.1"

apply from: "$rootDir/gradle/publishing.gradle"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public final class Properties {
private String requestIdHeader = "x-server-request-id";
private MdcKeys mdcKeys = new MdcKeys();
private Integer fieldMaxLength = 20_000;
private boolean logQueryString = false;
private List<MediaType> contentTypeWhitelist = List.of(
new MediaType("text", "*"),
MediaType.APPLICATION_JSON,
Expand Down Expand Up @@ -53,6 +52,7 @@ public static class Fingerprint {
public static class Request {
private String method = "request_method";
private String path = "request_path";
private String queryString = "request_query_string";
private String body = "request_body";
private String headers = "request_headers";
private String clientIp = "client_ip";
Expand Down
2 changes: 1 addition & 1 deletion src/logging/logging-reactive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jar.enabled = true

archivesBaseName = "library-logging-reactive"

project.version = "1.0.0"
project.version = "1.0.1"

apply from: "$rootDir/gradle/publishing.gradle"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.iakunin.library.logging.reactive.handler.HttpLoggingHandler;
import dev.iakunin.library.logging.reactive.handler.RequestIdHandler;
import dev.iakunin.library.logging.reactive.handler.RequestPathHandler;
import dev.iakunin.library.logging.reactive.handler.RequestQueryStringHandler;
import dev.iakunin.library.logging.reactive.wrapper.ContextWrapper;
import dev.iakunin.library.logging.reactive.wrapper.LoggerWrapper;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -56,24 +57,28 @@ public HttpHandlerDecoratorFactory requestPathHandlerDecorator(
FieldTrimmer fieldTrimmer,
ContextWrapper contextWrapper
) {
return decorated -> new RequestPathHandler(
decorated,
properties,
fieldTrimmer,
contextWrapper
);
return decorated -> new RequestPathHandler(decorated, fieldTrimmer, contextWrapper);
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE + 2)
public HttpHandlerDecoratorFactory requestQueryStringHandlerDecorator(
FieldTrimmer fieldTrimmer,
ContextWrapper contextWrapper
) {
return decorated -> new RequestQueryStringHandler(decorated, fieldTrimmer, contextWrapper);
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE + 3)
public HttpHandlerDecoratorFactory requestIdHandlerDecorator(
ContextWrapper contextWrapper
) {
return decorated -> new RequestIdHandler(decorated, properties, contextWrapper);
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE + 3)
@Order(Ordered.HIGHEST_PRECEDENCE + 4)
public HttpHandlerDecoratorFactory httpLoggingHandlerDecorator(
LoggerWrapper loggerWrapper
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.iakunin.library.logging.reactive.handler;

import dev.iakunin.library.logging.common.configuration.Properties;
import dev.iakunin.library.logging.common.service.FieldTrimmer;
import dev.iakunin.library.logging.reactive.wrapper.ContextWrapper;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,7 +14,6 @@
public final class RequestPathHandler implements HttpHandler {

private final HttpHandler decorated;
private final Properties properties;
private final FieldTrimmer fieldTrimmer;
private final ContextWrapper contextWrapper;

Expand All @@ -24,20 +22,13 @@ public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response)
return decorated.handle(request, response)
.contextWrite(
context -> contextWrapper
.putRequestPath(context, fieldTrimmer.trim(buildPath(request)))
.putRequestPath(
context,
fieldTrimmer.trim(
request.getURI().getPath()
)
)
);
}

private String buildPath(ServerHttpRequest request) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(request.getURI().getPath());

final String queryString = request.getURI().getQuery();
if (queryString != null && properties.isLogQueryString()) {
stringBuilder.append('?').append(queryString);
}

return stringBuilder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.iakunin.library.logging.reactive.handler;

import dev.iakunin.library.logging.common.service.FieldTrimmer;
import dev.iakunin.library.logging.reactive.wrapper.ContextWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

@Slf4j
@RequiredArgsConstructor
public final class RequestQueryStringHandler implements HttpHandler {

private final HttpHandler decorated;
private final FieldTrimmer fieldTrimmer;
private final ContextWrapper contextWrapper;

@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
return decorated.handle(request, response)
.contextWrite(
context -> contextWrapper
.putRequestQueryString(
context,
fieldTrimmer.trim(
request.getURI().getQuery()
)
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
public final class ContextWrapper {

private final String requestPathKey;
private final String requestQueryStringKey;
private final String requestIdKey;
private final String sessionFingerprintKey;
private final String processFingerprintKey;

public ContextWrapper(Properties properties) {
this.requestPathKey = properties.getMdcKeys().getRequest().getPath();
this.requestQueryStringKey = properties.getMdcKeys().getRequest().getQueryString();
this.requestIdKey = properties.getMdcKeys().getRequest().getId();
this.sessionFingerprintKey = properties.getMdcKeys().getFingerprint().getSession();
this.processFingerprintKey = properties.getMdcKeys().getFingerprint().getProcess();
Expand All @@ -23,6 +25,7 @@ public ContextWrapper(Properties properties) {
public Map<String, String> getAll(ContextView context) {
final Map<String, String> map = new ConcurrentHashMap<>();
map.put(requestPathKey, getRequestPath(context));
map.put(requestQueryStringKey, getRequestQueryString(context));
map.put(requestIdKey, getRequestId(context));
map.put(sessionFingerprintKey, getSessionFingerprint(context));
map.put(processFingerprintKey, getProcessFingerprint(context));
Expand All @@ -38,6 +41,14 @@ public String getRequestPath(ContextView context) {
return context.get(requestPathKey);
}

public Context putRequestQueryString(Context context, String requestQueryString) {
return context.put(requestQueryStringKey, requestQueryString);
}

public String getRequestQueryString(ContextView context) {
return context.get(requestQueryStringKey);
}

public Context putRequestId(Context context, String requestId) {
return context.put(requestIdKey, requestId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/logging/logging-servlet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jar.enabled = true

archivesBaseName = "library-logging-servlet"

project.version = "1.0.0"
project.version = "1.0.1"

apply from: "$rootDir/gradle/publishing.gradle"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
public final class RequestPathFilter extends OncePerRequestFilter {

private final Properties.MdcKeys.Request requestMdcKeys;
private final Boolean logQueryString;
private final FieldTrimmer fieldTrimmer;

public RequestPathFilter(Properties properties, FieldTrimmer fieldTrimmer) {
super();
this.requestMdcKeys = properties.getMdcKeys().getRequest();
this.logQueryString = properties.isLogQueryString();
this.fieldTrimmer = fieldTrimmer;
}

Expand All @@ -30,23 +28,14 @@ protected void doFilterInternal(
FilterChain chain
) throws IOException, ServletException {
try {
MDC.put(requestMdcKeys.getPath(), fieldTrimmer.trim(buildPath(request)));
MDC.put(requestMdcKeys.getPath(), fieldTrimmer.trim(request.getRequestURI()));
MDC.put(requestMdcKeys.getQueryString(), fieldTrimmer.trim(request.getQueryString()));

chain.doFilter(request, response);
} finally {
MDC.remove(requestMdcKeys.getPath());
MDC.remove(requestMdcKeys.getQueryString());
}
}

private String buildPath(HttpServletRequest request) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(request.getRequestURI());

final String queryString = request.getQueryString();
if (queryString != null && logQueryString) {
stringBuilder.append('?').append(queryString);
}

return stringBuilder.toString();
}
}

0 comments on commit e88cc15

Please sign in to comment.