Copying MDC Context Map in Web Clients: A Comprehensive Guide

Copying MDC Context Map in Web Clients: A Comprehensive Guide
Photo by Library of Congress / Unsplash

Introduction

In distributed systems, maintaining context across microservices is crucial for effective logging and tracing. The Mapped Diagnostic Context (MDC) in logging frameworks like Logback or Log4j allows developers to propagate contextual information such as request IDs, user IDs, or correlation IDs across threads and services. When using web clients to make outgoing requests, it's essential to propagate the MDC context map to ensure consistency and traceability. This guide will provide a comprehensive overview of how to copy the MDC context map in web clients, ensuring seamless context propagation in distributed environments.

  1. Configure MDC in Your Application: Before copying the MDC context map in web clients, ensure that MDC is properly configured in your application. This typically involves setting up MDC filters or interceptors to capture and propagate contextual information.
  2. Use Interceptors or Filters in Web Clients: In both synchronous and asynchronous web clients, interceptors or filters can be used to capture the MDC context map from the current thread and propagate it to outgoing requests.
  3. Example Implementation in Spring WebClient:
import org.slf4j.MDC;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class MdcPropagatingWebClient {

    private final WebClient webClient;

    public MdcPropagatingWebClient() {
        this.webClient = WebClient.builder()
                .filter(mdcPropagatingFilter())
                .build();
    }

    private ExchangeFilterFunction mdcPropagatingFilter() {
        return (request, next) -> Mono.deferContextual(contextView ->
                Mono.fromCallable(() -> MDC.getCopyOfContextMap())
                        .doOnNext(mdcContext -> {
                            if (mdcContext != null) {
                                MDC.setContextMap(mdcContext);
                            }
                        })
                        .flatMap(mdcContext -> next.exchange(request))
                        .doFinally(signalType -> MDC.clear())
        );
    }

    public WebClient getClient() {
        return webClient;
    }
}
  1. Integration with Other Web Clients: For other web clients, such as Apache HttpClient or OkHttp, similar approaches can be used to intercept outgoing requests and propagate the MDC context map.

Conclusion

Copying the MDC context map in web clients is essential for maintaining context and traceability in distributed systems. By following the guidelines outlined in this guide and implementing interceptors or filters in web clients, developers can ensure seamless context propagation across microservices. With consistent context propagation, logging and tracing become more effective, enabling easier debugging and monitoring of distributed applications.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe