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 @@ ${micrometer-version} true + + + io.micrometer + context-propagation + test + io.rest-assured rest-assured 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. ---- +== 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.