@@ -63,8 +63,6 @@ public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(
6363 */
6464 public <REQUEST , RESPONSE > Mono <ClientHttpResponse <RESPONSE >> asyncHttpResponse (
6565 ClientHttpRequest <REQUEST , RESPONSE > httpRequest ) {
66- // --- MDC Propagation Step 1: Capture MDC from the calling thread ---
67- // This map holds the MDC context from the thread that initiates this request.
6866 final Map <String , String > mdcContextMap = MDC .getCopyOfContextMap ();
6967 log .info ("asyncHttpResponse initiated. Captured MDC from calling thread: {}" , mdcContextMap );
7068
@@ -85,42 +83,34 @@ public <REQUEST, RESPONSE> Mono<ClientHttpResponse<RESPONSE>> asyncHttpResponse(
8583 contextFromReactor .ifPresent (MDC ::setContextMap );
8684 })
8785 .onErrorResume (WebClientResponseException .class , ex -> {
88- // MDC should be available here due to the `doOnEach` operator
8986 final String errorMessage = String .format (
9087 "Error in WebClient call (ResponseException). Error=%s Headers=%s statusCode=%s" ,
9188 ex .getResponseBodyAsString (), ex .getHeaders (), ex .getStatusCode ());
9289 return Mono .just (handleExceptionInternal (ex , errorMessage , ex .getResponseBodyAsString (),
9390 HttpStatus .valueOf (ex .getStatusCode ().value ()), httpRequest ));
9491 })
9592 .onErrorResume (org .springframework .web .client .HttpStatusCodeException .class , ex -> {
96- // MDC should be available here
9793 final String errorMessage = String .format (
9894 "Error in WebClient call (HttpStatusCodeException). Error=%s Headers=%s statusCode=%s" ,
9995 ex .getResponseBodyAsString (), ex .getResponseHeaders (), ex .getStatusCode ());
10096 return Mono .just (handleExceptionInternal (ex , errorMessage , ex .getResponseBodyAsString (),
10197 HttpStatus .valueOf (ex .getStatusCode ().value ()), httpRequest ));
10298 })
10399 .onErrorResume (org .springframework .web .client .UnknownContentTypeException .class , ex -> {
104- // MDC should be available here
105100 final String errorMessage = String .format (
106101 "Error in WebClient call (UnknownContentTypeException). Error=%s Headers=%s" ,
107102 ex .getResponseBodyAsString (), ex .getResponseHeaders ());
108103 return Mono .just (handleExceptionInternal (ex , errorMessage , ex .getResponseBodyAsString (),
109104 HttpStatus .valueOf (ex .getRawStatusCode ()), httpRequest ));
110105 })
111106 .onErrorResume (Exception .class , ex -> { // Catch any other unexpected exceptions
112- // MDC should be available here
113107 final String errorMessage = String .format (
114108 "Unhandled exception in WebClient call. Error=%s Cause=%s" , ex .getMessage (),
115109 ex .getCause ());
116110 return Mono .just (
117111 handleExceptionInternal (ex , errorMessage , null , HttpStatus .INTERNAL_SERVER_ERROR ,
118112 httpRequest ));
119113 })
120- // --- MDC Propagation Step 4: Clear MDC after stream completion ---
121- // This `doFinally` operator ensures that MDC is cleared on the thread
122- // that processes the final signal (onComplete or onError), preventing
123- // MDC leakage to subsequent tasks on the same thread pool thread.
124114 .doFinally (signalType -> {
125115 MDC .clear ();
126116 log .info ("MDC cleared after reactive chain completion (signal type: {})." , signalType );
@@ -162,12 +152,8 @@ private <REQUEST, RESPONSE> Retry generateRetrySpec(
162152 .fixedDelay (httpRequest .getClientRetryConfig ().getMaxAttempts (),
163153 Duration .ofSeconds (httpRequest .getClientRetryConfig ().getBackOff ()))
164154 .doBeforeRetry (signal -> {
165- // --- MDC Logging in Retry Callback ---
166- // The `doOnEach` operator (placed earlier in the chain) ensures that
167- // MDC is already set on the current thread for this `doBeforeRetry` callback.
168- // So, we can directly access it for logging.
169- log .info ("Retrying for requestUrl={}, retryCount={}. Current MDC: {}" ,
170- httpRequest .getUrl (), signal .totalRetries (), MDC .getCopyOfContextMap ());
155+ log .info ("Retrying for requestUrl={}, retryCount={}" ,
156+ httpRequest .getUrl (), signal .totalRetries ());
171157 })
172158 .filter (httpRequest .getClientRetryConfig ().getRetryFilter ());
173159 }
@@ -204,12 +190,9 @@ private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleExceptionInternal
204190 final String responseBody ,
205191 final HttpStatus httpStatus ,
206192 final ClientHttpRequest <REQUEST , RESPONSE > httpRequest ) {
207- // --- MDC Logging in Error Handler ---
208- // MDC should be available here because `onErrorResume` is also within the scope
209- // of `doOnEach` that restores the context.
210193 log .error (
211- "Exception while executing http request for requestUrl={}, status={}, errorMessage={}. Current MDC: {} " ,
212- httpRequest .getUrl (), httpStatus , errorMessage , MDC . getCopyOfContextMap (),
194+ "Exception while executing http request for requestUrl={}, status={}, errorMessage={}" ,
195+ httpRequest .getUrl (), httpStatus , errorMessage ,
213196 exception ); // Include 'exception' for stack trace
214197 httpRequest .getRetryHandlers ()
215198 .forEach (handlerId -> RetryHandlerFactory .getHandler (handlerId .toString ())
0 commit comments