Skip to content

Commit 336e00d

Browse files
committed
Clear MDC so that no “leaked” MDC values stick around for the next request.
1 parent a378cf3 commit 336e00d

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/main/java/uk/gov/hmcts/cp/filters/tracing/TracingFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public TracingFilter(@Value("${spring.application.name}") final String applicati
2828

2929
@Override
3030
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
31+
try {
32+
populateMDC(request, response, filterChain);
33+
} finally {
34+
MDC.clear();
35+
}
36+
}
37+
38+
void populateMDC(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
3139
MDC.put(APPLICATION_NAME, applicationName);
3240
if (request.getHeader(TRACE_ID) != null) {
3341
MDC.put(TRACE_ID, request.getHeader(TRACE_ID));

src/test/java/uk/gov/hmcts/cp/filters/tracing/TracingFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void filter_should_use_incoming_traceId() throws ServletException, IOException {
3535
when(request.getHeader(TRACE_ID)).thenReturn("incoming-traceId");
3636
when(request.getHeader(SPAN_ID)).thenReturn("incoming-spanId");
3737

38-
tracingFilter.doFilterInternal(request, response, filterChain);
38+
tracingFilter.populateMDC(request, response, filterChain);
3939

4040
assertThat(MDC.get(APPLICATION_NAME)).isEqualTo("myAppName");
4141
verify(response).setHeader(TRACE_ID, "incoming-traceId");

0 commit comments

Comments
 (0)