Switch reactor context propagation from auto to limited

- Polish
- Fix @since tags
- Add a test case
- Update context-propagation version

Closes gh-34201
This commit is contained in:
Moritz Halbritter 2023-10-04 09:11:40 +02:00
parent 30f29dead8
commit 5b129ceb09
6 changed files with 35 additions and 14 deletions

View File

@ -27,14 +27,14 @@ import org.springframework.context.annotation.Configuration;
* {@link EnableAutoConfiguration Auto-configuration} for Reactor.
*
* @author Brian Clozel
* @since 3.0.2
* @since 3.2.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Hooks.class)
@EnableConfigurationProperties(ReactorProperties.class)
public class ReactorAutoConfiguration {
public ReactorAutoConfiguration(ReactorProperties properties) {
ReactorAutoConfiguration(ReactorProperties properties) {
if (properties.getContextPropagation() == ReactorProperties.ContextPropagationMode.AUTO) {
Hooks.enableAutomaticContextPropagation();
}

View File

@ -22,7 +22,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Configuration properties for Reactor.
*
* @author Brian Clozel
* @since 3.0.3
* @since 3.2.0
*/
@ConfigurationProperties(prefix = "spring.reactor")
public class ReactorProperties {
@ -30,7 +30,7 @@ public class ReactorProperties {
/**
* Context Propagation support mode for Reactor operators.
*/
private ContextPropagationMode contextPropagation = ContextPropagationMode.AUTO;
private ContextPropagationMode contextPropagation = ContextPropagationMode.LIMITED;
public ContextPropagationMode getContextPropagation() {
return this.contextPropagation;

View File

@ -2092,7 +2092,7 @@
},
{
"name": "spring.reactor.context-propagation",
"defaultValue": "auto"
"defaultValue": "limited"
},
{
"name": "spring.reactor.stacktrace-mode.enabled",

View File

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.reactor;
import java.util.concurrent.atomic.AtomicReference;
import io.micrometer.context.ContextRegistry;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ReactorAutoConfiguration}.
*
* @author Brian Clozel
* @author Moritz Halbritter
*/
class ReactorAutoConfigurationTests {
@ -41,7 +43,7 @@ class ReactorAutoConfigurationTests {
private static final String THREADLOCAL_KEY = "ReactorAutoConfigurationTests";
private static final ThreadLocal<String> THREADLOCAL_VALUE = ThreadLocal.withInitial(() -> "failure");
private static final ThreadLocal<String> THREADLOCAL_VALUE = ThreadLocal.withInitial(() -> "initial");
@BeforeAll
static void initializeThreadLocalAccessors() {
@ -49,15 +51,33 @@ class ReactorAutoConfigurationTests {
globalRegistry.registerThreadLocalAccessor(THREADLOCAL_KEY, THREADLOCAL_VALUE);
}
@AfterAll
static void removeThreadLocalAccessors() {
ContextRegistry globalRegistry = ContextRegistry.getInstance();
globalRegistry.removeThreadLocalAccessor(THREADLOCAL_KEY);
}
@Test
void shouldConfigureAutomaticContextPropagation() {
void shouldNotConfigurePropagationByDefault() {
AtomicReference<String> threadLocalValue = new AtomicReference<>();
this.contextRunner.run((applicationContext) -> {
Mono.just("test")
.doOnNext((element) -> threadLocalValue.set(THREADLOCAL_VALUE.get()))
.contextWrite(Context.of(THREADLOCAL_KEY, "success"))
.contextWrite(Context.of(THREADLOCAL_KEY, "updated"))
.block();
assertThat(threadLocalValue.get()).isEqualTo("success");
assertThat(threadLocalValue.get()).isEqualTo("initial");
});
}
@Test
void shouldConfigurePropagationIfSetToAuto() {
AtomicReference<String> threadLocalValue = new AtomicReference<>();
this.contextRunner.withPropertyValues("spring.reactor.context-propagation=auto").run((applicationContext) -> {
Mono.just("test")
.doOnNext((element) -> threadLocalValue.set(THREADLOCAL_VALUE.get()))
.contextWrite(Context.of(THREADLOCAL_KEY, "updated"))
.block();
assertThat(threadLocalValue.get()).isEqualTo("updated");
});
}

View File

@ -14,7 +14,11 @@ NOTE: Low cardinality key-values will be added to metrics and traces, while high
Beans of type `ObservationPredicate`, `GlobalObservationConvention`, `ObservationFilter` and `ObservationHandler` will be automatically registered on the `ObservationRegistry`.
You can additionally register any number of `ObservationRegistryCustomizer` beans to further configure the registry.
For more details please see the https://micrometer.io/docs/observation[Micrometer Observation documentation].
Observability support relies on the https://github.com/micrometer-metrics/context-propagation[Context Propagation library] for forwarding the current observation across threads and reactive pipelines.
By default, `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.
For more details about observations please see the https://micrometer.io/docs/observation[Micrometer Observation documentation].
TIP: Observability for JDBC can be configured using a separate project.
The https://github.com/jdbc-observations/datasource-micrometer[Datasource Micrometer project] provides a Spring Boot starter which automatically creates observations when JDBC operations are invoked.
@ -80,7 +84,4 @@ The attributes of the `Resource` can be configured via the configprop:management
NOTE: Spring Boot does not provide auto-configuration for OpenTelemetry metrics or logging.
OpenTelemetry tracing is only auto-configured when used together with <<actuator#actuator.micrometer-tracing, Micrometer Tracing>>.
Observability support relies on the https://github.com/micrometer-metrics/context-propagation[Context Propagation library] for forwarding the current observation across threads and reactive pipelines.
`ThreadLocal` values are automatically reinstated in reactive operators, this behavior is controlled with the configprop:spring.reactor.context-propagation[] property.
The next sections will provide more details about logging, metrics and traces.

View File

@ -138,7 +138,7 @@ bom {
]
}
}
library("Micrometer Context Propagation", "1.0.2") {
library("Micrometer Context Propagation", "1.0.5") {
group("io.micrometer") {
modules = [
"context-propagation"