Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components-starter/camel-platform-http-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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);
});
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
21 changes: 21 additions & 0 deletions docs/spring-boot/modules/ROOT/pages/starters/platform-http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading