Skip to content

CAMEL-27134: camel-platform-http-starter - document and test that spring.task.execution.propagate-context joins the route to the request's trace - #2107

Merged
Croway merged 3 commits into
apache:mainfrom
Croway:CAMEL-27134-platform-http-context-propagation
Nov 18, 2026
Merged

Croway merged 3 commits into
apache:mainfrom
Croway:CAMEL-27134-platform-http-context-propagation

Conversation

@Croway

@Croway Croway commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

CAMEL-27134: SpringBootPlatformHttpConsumer runs the route on a thread of the task executor, not on the servlet thread that received the request. With Micrometer Tracing (for example spring-boot-starter-opentelemetry) and camel-opentelemetry2, the route spans therefore start a new trace instead of joining the http 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=true it decorates applicationTaskExecutor (the executor this consumer normally uses) with a ContextPropagatingTaskDecorator, 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:

  • a test that sets the property and checks that the route, on a 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);
  • a usage section on the starter page explaining the property, what else it carries over (Spring Security context, MDC, when their accessors are registered), and that an executor bean defined by the application replaces applicationTaskExecutor, so it needs the decorator itself (built with Spring Boot's executor builders, or with a ContextPropagatingTaskDecorator);
  • io.micrometer:context-propagation as a test dependency.

An earlier commit decorated the task in the consumer whenever context-propagation was 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.testLoad failed 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

…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.
@Croway
Croway requested review from davsclaus and oscerd November 17, 2026 21:36

@davsclaus davsclaus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Croway Croway changed the title CAMEL-27134: camel-platform-http-starter - run the route with the context of the HTTP request, so its spans join the request's trace CAMEL-27134: camel-platform-http-starter - document and test that spring.task.execution.propagate-context joins the route to the request's trace Nov 18, 2026
@Croway

Croway commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @davsclaus, both questions pointed the same way: with spring.task.execution.propagate-context=true, applicationTaskExecutor already restores the request's context on the route thread, so 9bd3949 drops the consumer's decorator and keeps propagation opt-in, as Spring Boot does. The PR now only has the test (with the property) and the usage section explaining it; no behaviour change, so no upgrade-guide note.

Claude Code on behalf of Croway

@Croway
Croway requested a review from davsclaus November 18, 2026 12:55

@davsclaus davsclaus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  • SpringBootPlatformHttpAutoConfiguration takes the first ThreadPoolTaskExecutor / SimpleAsyncTaskExecutor in the List<Executor> (or a DelegatingSecurityContextAsyncTaskExecutor first). This is applicationTaskExecutor only 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 a ContextPropagatingTaskDecorator"). 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).
@Croway
Croway merged commit 6bc1df8 into apache:main Nov 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants