Skip to content

Commit

Permalink
GH-5208 Enable request tracing through logs using new associated requ…
Browse files Browse the repository at this point in the history
…est id
  • Loading branch information
bherber1 committed Dec 2, 2024
1 parent 6e64a12 commit 222760e
Showing 1 changed file with 15 additions and 0 deletions.
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

0 comments on commit 222760e

Please sign in to comment.