Repository navigation
CAMEL-27134: camel-platform-http-starter - document and test that spring.task.execution.propagate-context joins the route to the request's trace - #2107
Conversation
…text of the HTTP request, so its spans join the request's trace SpringBootPlatformHttpConsumer runs the route on an executor thread without the context of the servlet thread, so with Micrometer Tracing the route spans were a separate trace from the span Spring MVC records for the request. When io.micrometer:context-propagation is on the classpath, the consumer now wraps the task with Spring's ContextPropagatingTaskDecorator, which captures the context on the servlet thread and restores it on the executor thread.
davsclaus
left a comment
There was a problem hiding this comment.
Thanks, joining the route spans to the request's trace is a real gain, and capturing in service() on the servlet thread (the decorator snapshots in decorate(), before runAsync hands the task over) is the right place. The test that walks up the Observation chain to the ServerRequestObservationContext is a nice way to prove it without a tracing backend.
A few points:
[Question] Double decoration with spring.task.execution.propagate-context=true. Spring Boot 4.1 registers a ContextPropagatingTaskDecorator bean when that property is set (TaskExecutorConfigurations$TaskExecutorContextPropagationConfiguration), and the ThreadPoolTaskExecutorBuilder puts it on the applicationTaskExecutor, which is the executor this consumer usually gets. Then each request is captured and restored twice (once by the consumer, once by the executor's execute(), which also runs on the servlet thread here). It should be harmless (nested scopes of the same values), just redundant work per request. Would it be worth skipping the consumer's decorator when that property is true, for example by passing a flag from SpringBootPlatformHttpAutoConfiguration?
[Question] On by default. Spring Boot keeps context propagation opt-in for its own executors, while here it switches on whenever io.micrometer:context-propagation is on the classpath, and then everything registered in the ContextRegistry (Observation, and for example the Spring Security context or MDC when their accessors are registered) shows up on the route thread. I think on-by-default is the right call for platform-http, as the route is the handling of that request, but it is new auto-detection, so it deserves a short note in the 4.24 upgrade guide (in apache/camel), and maybe a way to turn it off if someone relies on the route thread starting clean.
[Nit] The new usage section could mention the spring.task.execution.propagate-context relation once the first point is settled, so users don't wonder whether they need to set it as well.
Reviewed with Claude Code on behalf of davsclaus. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
…opt-in, through spring.task.execution.propagate-context Following the review: Spring Boot keeps context propagation opt-in for its task executors, and with spring.task.execution.propagate-context=true the executor that the platform-http consumer uses already restores the context of the request on the route thread. So the consumer no longer decorates the task itself (that did the work twice when the property was set, and turned propagation on for everyone). The test sets the property and checks that the route sees the request's Observation; the usage section of the starter explains the property.
|
Thanks @davsclaus, both questions pointed the same way: with Claude Code on behalf of Croway |
davsclaus
left a comment
There was a problem hiding this comment.
Thanks for reworking this. Keeping propagation opt-in through Spring Boot's own spring.task.execution.propagate-context, with no code in the consumer, is the right call, and the test shows it end to end: the route runs on a task-* thread and still finds the request's http.server.requests observation.
Two optional notes, neither blocks the merge:
SpringBootPlatformHttpAutoConfigurationtakes the firstThreadPoolTaskExecutor/SimpleAsyncTaskExecutorin theList<Executor>(or aDelegatingSecurityContextAsyncTaskExecutorfirst). This isapplicationTaskExecutoronly when the app defines no other executor bean, because Boot backs off when it finds one. The last sentence of the usage section already covers this ("an executor bean that you build yourself needs aContextPropagatingTaskDecorator"). You could also say there that the starter then uses that bean, so users know which executor needs the decorator.- A negative test without the property (the route gets
no observation of the request) would show that the property is what makes the difference, not the classpath. It is optional, because the positive test already fails without the decoration.
Reviewed with Claude Code on behalf of davsclaus. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
…s off without the property, and say which executor needs the decorator - SpringBootPlatformHttpNoContextPropagationTest runs the same route without spring.task.execution.propagate-context: the route does not see the request's Observation. - The usage section says that an application executor bean replaces applicationTaskExecutor, and how to give it the decorator (Spring Boot's executor builders, or a ContextPropagatingTaskDecorator).
Description
CAMEL-27134:
SpringBootPlatformHttpConsumerruns the route on a thread of the task executor, not on the servlet thread that received the request. With Micrometer Tracing (for examplespring-boot-starter-opentelemetry) andcamel-opentelemetry2, the route spans therefore start a new trace instead of joining thehttp post ...span that Spring MVC records for the request.Spring Boot already solves this for its task executors, opt-in: with
spring.task.execution.propagate-context=trueit decoratesapplicationTaskExecutor(the executor this consumer normally uses) with aContextPropagatingTaskDecorator, which restores the context of the request on the route thread. So, after the review, this PR changes no code and keeps propagation opt-in, as Spring Boot does:task-*thread, sees the Observation that Spring MVC opened for the request (http.server.requests GET /observed), and one without the property, where the route does not see it (propagation stays opt-in);applicationTaskExecutor, so it needs the decorator itself (built with Spring Boot's executor builders, or with aContextPropagatingTaskDecorator);io.micrometer:context-propagationas a test dependency.An earlier commit decorated the task in the consumer whenever
context-propagationwas on the classpath; it is reverted, as it duplicated the executor's decoration when the property was set and turned propagation on by default.Tests
All tests of the starter pass: 145 unit tests (2 skipped) and 11 integration tests. In an earlier run,
SpringBootPlatformHttpCertificationTest.testLoadfailed once with "Invalid header" and passed on the rerun; it does not use the property, and the production code is unchanged.Claude Code on behalf of Croway