Unlike WebClient
, the new RestClient
does not propagate tracing headers (e.g., traceId
, spanId
) automatically when using Micrometer Tracing.
Expected Behavior
When Spring Boot Observability (Micrometer Tracing
) is enabled, RestClient
should automatically inject the tracing headers, just like WebClient
.
Currently, developers must manually add tracing headers to every request, either by:
1. Manually adding headers to each request:
java
RestClient restClient = RestClient.create();
restClient.get()
.uri("https://api.example.com")
.header("traceparent", "12345")
.header("x-span-id", "67890")
.retrieve()
.body(String.class);
2. Using a custom ClientHttpRequestInterceptor
, which requires additional setup.
Motivation
Many Spring developers are migrating from RestTemplate
to RestClient
because it is its official replacement. However, RestTemplate
users often expect automatic observability/tracing, which is currently missing in RestClient
.
WebClient
provides this functionality out-of-the-box when Micrometer Tracing is enabled. RestClient
should offer similar behavior to maintain consistency across Spring's HTTP clients.
Proposal
- Automatically inject tracing headers (
traceparent
,x-span-id
, etc.) when Spring Boot Observability (Micrometer Tracing
) is enabled. - Provide a way to customize or disable this feature if needed.
Comment From: bclozel
This has been supported since https://github.com/spring-projects/spring-framework/issues/31114 so I'm surprised if this doesn't work for you. Maybe there is a problem with your setup?
If you want us to have a look please provide a minimal sample. Thanks.
Comment From: mouadELOmari
This functionality works when using the builder, but not when using RestClient.create(). It seems that tracing headers are only propagated when configuring RestClient through the builder. Could you confirm if this is the expected behavior or if RestClient.create() should also support automatic tracing header propagation?
Comment From: bclozel
This is the expected and documented behavior: if you want to produce metrics and traces, you need to configure an ObservationRegistry
to record those observations in the first place.
RestClient
must keep working without concrete observation registry implementations on the classpath. Also, Spring Framework cannot auto-detect the observation registry: Spring Boot will do that in its auto-configurations and will contribute a well suited RestClient.Builder
to the application context.