From e58c8975f68a0730baad4c0be1910ff18f90ea33 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 11 Nov 2025 12:25:31 +0100 Subject: [PATCH] Document how to use ContextPropagatingTaskDecorator for propagating trace context over thread boundaries Closes gh-47893 --- .../pages/actuator/observability.adoc | 5 +++ .../ContextPropagationConfiguration.java | 31 +++++++++++++++++++ .../ContextPropagationConfiguration.kt | 31 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.kt diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/observability.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/observability.adoc index 349ae05782e..a021cdb0da0 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/observability.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/observability.adoc @@ -29,6 +29,11 @@ Observability support relies on the https://github.com/micrometer-metrics/contex By default, javadoc:java.lang.ThreadLocal[] values are not automatically reinstated in reactive operators. This behavior is controlled with the configprop:spring.reactor.context-propagation[] property, which can be set to `auto` to enable automatic propagation. +If you're working with javadoc:org.springframework.scheduling.annotation.Async[format=annotation] methods or use an javadoc:org.springframework.core.task.AsyncTaskExecutor[], you have to register the javadoc:org.springframework.core.task.support.ContextPropagatingTaskDecorator[] on the executor, otherwise the observability context is lost when switching threads. +This can be done using this configuration: + +include-code::ContextPropagationConfiguration[] + For more details about observations please see the {url-micrometer-docs}/observation[Micrometer Observation documentation]. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.java new file mode 100644 index 00000000000..3656f534a6f --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.boot.docs.actuator.observability.contextpropagation; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.support.ContextPropagatingTaskDecorator; + +@Configuration(proxyBeanMethods = false) +class ContextPropagationConfiguration { + + @Bean + ContextPropagatingTaskDecorator contextPropagatingTaskDecorator() { + return new ContextPropagatingTaskDecorator(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.kt new file mode 100644 index 00000000000..0c1f76e006f --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/contextpropagation/ContextPropagationConfiguration.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.boot.docs.actuator.observability.contextpropagation + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.task.support.ContextPropagatingTaskDecorator + +@Configuration(proxyBeanMethods = false) +class ContextPropagationConfiguration { + + @Bean + fun contextPropagatingTaskDecorator(): ContextPropagatingTaskDecorator { + return ContextPropagatingTaskDecorator() + } + +}