Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-5208 Enable request tracing through logs using new associated request id #5209

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
*******************************************************************************/
package org.eclipse.rdf4j.http.server;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

/**
Expand All @@ -24,13 +28,23 @@
*/
public abstract class ServerInterceptor implements HandlerInterceptor {

private static final String REQUEST_ID_KEY = "org.eclipse.rdf4j.requestId";
private static final String PROCESS_ID = "process:" + UUID.randomUUID();

private static final AtomicLong requestNumber = new AtomicLong(0L);

private volatile String origThreadName;

private static String createRequestId() {
return PROCESS_ID + ":request:" + requestNumber.getAndIncrement();
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
origThreadName = Thread.currentThread().getName();
Thread.currentThread().setName(getThreadName());
MDC.put(REQUEST_ID_KEY, createRequestId());

setRequestAttributes(request);

Expand All @@ -43,6 +57,7 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp
try {
cleanUpResources();
} finally {
MDC.remove(REQUEST_ID_KEY);
Thread.currentThread().setName(origThreadName);
}
}
Expand Down
Loading