This is an automated email from the ASF dual-hosted git repository.

Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 6bc1df8d47e CAMEL-27134: camel-platform-http-starter - document and 
test that spring.task.execution.propagate-context joins the route to the 
request's trace (#2107)
6bc1df8d47e is described below

commit 6bc1df8d47e4193f00c5d94faad22738d53adfb0
Author: Federico Mariani <[email protected]>
AuthorDate: Wed Nov 18 14:57:58 2026 +0100

    CAMEL-27134: camel-platform-http-starter - document and test that 
spring.task.execution.propagate-context joins the route to the request's trace 
(#2107)
    
    * CAMEL-27134: camel-platform-http-starter - run the route with the context 
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.
    
    * CAMEL-27134: camel-platform-http-starter - context propagation stays 
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.
    
    * CAMEL-27134: camel-platform-http-starter - test that propagation stays 
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).
---
 .../camel-platform-http-starter/pom.xml            |   6 ++
 .../src/main/doc/usage.adoc                        |  18 ++++
 ...ringBootPlatformHttpContextPropagationTest.java | 111 +++++++++++++++++++++
 ...ngBootPlatformHttpNoContextPropagationTest.java |  63 ++++++++++++
 .../modules/ROOT/pages/starters/platform-http.adoc |  21 ++++
 5 files changed, 219 insertions(+)

diff --git a/components-starter/camel-platform-http-starter/pom.xml 
b/components-starter/camel-platform-http-starter/pom.xml
index f5618d142ae..69a57311424 100644
--- a/components-starter/camel-platform-http-starter/pom.xml
+++ b/components-starter/camel-platform-http-starter/pom.xml
@@ -83,6 +83,12 @@
       <version>${micrometer-version}</version>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <!-- needed by spring.task.execution.propagate-context, see 
SpringBootPlatformHttpContextPropagationTest -->
+      <groupId>io.micrometer</groupId>
+      <artifactId>context-propagation</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>io.rest-assured</groupId>
       <artifactId>rest-assured</artifactId>
diff --git 
a/components-starter/camel-platform-http-starter/src/main/doc/usage.adoc 
b/components-starter/camel-platform-http-starter/src/main/doc/usage.adoc
new file mode 100644
index 00000000000..cb43ec8587c
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/main/doc/usage.adoc
@@ -0,0 +1,18 @@
+The routes run on a thread of a task executor (normally Spring Boot's 
`applicationTaskExecutor`), not on the servlet
+thread that received the request. To run them with the context of the request, 
for example so that with Micrometer
+Tracing the spans of the route belong to the trace of the HTTP request instead 
of starting a new one, enable Spring
+Boot's context propagation for its task executors:
+
+[source,properties]
+----
+spring.task.execution.propagate-context=true
+----
+
+It needs `io.micrometer:context-propagation`, which comes with Micrometer 
Tracing (for example with
+`spring-boot-starter-opentelemetry`). It carries over any context registered 
with context propagation, such as the
+Spring Security context or the MDC when their accessors are registered.
+
+The property applies to the executors that Spring Boot builds. If the 
application defines its own executor bean,
+Spring Boot does not create `applicationTaskExecutor` and the starter uses 
that bean instead: build it with Spring
+Boot's `ThreadPoolTaskExecutorBuilder` or `SimpleAsyncTaskExecutorBuilder`, 
which add the decorator, or set a
+`ContextPropagatingTaskDecorator` on it.
diff --git 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpContextPropagationTest.java
 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpContextPropagationTest.java
new file mode 100644
index 00000000000..00bcc05de67
--- /dev/null
+++ 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpContextPropagationTest.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.platform.http.springboot;
+
+import io.micrometer.observation.Observation;
+import io.micrometer.observation.ObservationRegistry;
+import io.micrometer.observation.ObservationView;
+import io.restassured.RestAssured;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.test.spring.junit6.CamelSpringBootTest;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import 
org.springframework.http.server.observation.ServerRequestObservationContext;
+import 
org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.web.SecurityFilterChain;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
+
+/**
+ * The route runs on an executor thread. With Spring Boot's 
spring.task.execution.propagate-context, that thread gets the
+ * context of the request, so the route sees the Micrometer Observation that 
Spring MVC opened for it and the route spans
+ * belong to the trace of the request.
+ */
+@EnableAutoConfiguration
+@CamelSpringBootTest
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+                properties = "spring.task.execution.propagate-context=true", 
classes = { CamelAutoConfiguration.class,
+        SpringBootPlatformHttpContextPropagationTest.class,
+        SpringBootPlatformHttpContextPropagationTest.TestConfiguration.class,
+        PlatformHttpComponentAutoConfiguration.class, 
SpringBootPlatformHttpAutoConfiguration.class })
+public class SpringBootPlatformHttpContextPropagationTest {
+
+    @Autowired
+    private Environment env;
+
+    @BeforeEach
+    void setUp() {
+        RestAssured.port = env.getRequiredProperty("local.server.port", 
Integer.class);
+    }
+
+    @Test
+    void routeSeesTheObservationOfTheRequest() {
+        given()
+                .get("/observed")
+                .then()
+                .statusCode(200)
+                .header("RouteThread", startsWith("task-"))
+                .body(equalTo("http.server.requests GET /observed"));
+    }
+
+    @Configuration
+    public static class TestConfiguration {
+
+        @Bean
+        public SecurityFilterChain securityFilterChain(HttpSecurity http) 
throws Exception {
+            http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
+                    .csrf(csrf -> csrf.disable());
+            return http.build();
+        }
+
+        @Bean
+        public RouteBuilder routeBuilder(ObservationRegistry 
observationRegistry) {
+            return new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("platform-http:/observed")
+                            .process(exchange -> {
+                                exchange.getMessage().setHeader("RouteThread", 
Thread.currentThread().getName());
+                                // the current observation may be a child of 
the request one (Spring Security adds
+                                // its own), so look for the request up the 
chain. Its key values are only computed
+                                // when it stops, so read the request it 
carries.
+                                String found = "no observation of the request";
+                                for (ObservationView view = 
observationRegistry.getCurrentObservation(); view != null;
+                                     view = view.getContextView() instanceof 
Observation.Context ctx
+                                             ? ctx.getParentObservation() : 
null) {
+                                    if (view.getContextView() instanceof 
ServerRequestObservationContext ctx) {
+                                        found = ctx.getName() + " " + 
ctx.getCarrier().getMethod() + " "
+                                                + 
ctx.getCarrier().getRequestURI();
+                                        break;
+                                    }
+                                }
+                                exchange.getMessage().setBody(found);
+                            });
+                }
+            };
+        }
+    }
+}
diff --git 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpNoContextPropagationTest.java
 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpNoContextPropagationTest.java
new file mode 100644
index 00000000000..8c937967b57
--- /dev/null
+++ 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpNoContextPropagationTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.platform.http.springboot;
+
+import io.restassured.RestAssured;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.test.spring.junit6.CamelSpringBootTest;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.env.Environment;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
+
+/**
+ * Without spring.task.execution.propagate-context, the executor thread that 
runs the route does not get the context of
+ * the request: context propagation stays opt-in, as for Spring Boot's own 
task executors. The same route as
+ * {@link SpringBootPlatformHttpContextPropagationTest}, which sets the 
property.
+ */
+@EnableAutoConfiguration
+@CamelSpringBootTest
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
classes = { CamelAutoConfiguration.class,
+        SpringBootPlatformHttpNoContextPropagationTest.class,
+        SpringBootPlatformHttpContextPropagationTest.TestConfiguration.class,
+        PlatformHttpComponentAutoConfiguration.class, 
SpringBootPlatformHttpAutoConfiguration.class })
+public class SpringBootPlatformHttpNoContextPropagationTest {
+
+    @Autowired
+    private Environment env;
+
+    @BeforeEach
+    void setUp() {
+        RestAssured.port = env.getRequiredProperty("local.server.port", 
Integer.class);
+    }
+
+    @Test
+    void routeDoesNotSeeTheObservationOfTheRequestByDefault() {
+        given()
+                .get("/observed")
+                .then()
+                .statusCode(200)
+                .header("RouteThread", startsWith("task-"))
+                .body(equalTo("no observation of the request"));
+    }
+}
diff --git a/docs/spring-boot/modules/ROOT/pages/starters/platform-http.adoc 
b/docs/spring-boot/modules/ROOT/pages/starters/platform-http.adoc
index 4e0cdc8faa6..453a9bd770a 100644
--- a/docs/spring-boot/modules/ROOT/pages/starters/platform-http.adoc
+++ b/docs/spring-boot/modules/ROOT/pages/starters/platform-http.adoc
@@ -21,6 +21,27 @@ Please refer to the above links for usage and configuration 
details.
 </dependency>
 ----
 
+== Usage
+
+The routes run on a thread of a task executor (normally Spring Boot's 
`applicationTaskExecutor`), not on the servlet
+thread that received the request. To run them with the context of the request, 
for example so that with Micrometer
+Tracing the spans of the route belong to the trace of the HTTP request instead 
of starting a new one, enable Spring
+Boot's context propagation for its task executors:
+
+[source,properties]
+----
+spring.task.execution.propagate-context=true
+----
+
+It needs `io.micrometer:context-propagation`, which comes with Micrometer 
Tracing (for example with
+`spring-boot-starter-opentelemetry`). It carries over any context registered 
with context propagation, such as the
+Spring Security context or the MDC when their accessors are registered.
+
+The property applies to the executors that Spring Boot builds. If the 
application defines its own executor bean,
+Spring Boot does not create `applicationTaskExecutor` and the starter uses 
that bean instead: build it with Spring
+Boot's `ThreadPoolTaskExecutorBuilder` or `SimpleAsyncTaskExecutorBuilder`, 
which add the decorator, or set a
+`ContextPropagatingTaskDecorator` on it.
+
 == Spring Boot Auto-Configuration
 
 The starter supports 10 options, which are listed below.

Reply via email to