Skip to content

Commit 30f29de

Browse files
committed
Revert "Temporarily remove auto-config for Reactor context propagation"
This reverts commit 88de3cc. See gh-34201
1 parent c6096e2 commit 30f29de

File tree

9 files changed

+200
-0
lines changed

9 files changed

+200
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ dependencies {
220220
testImplementation("com.mysql:mysql-connector-j")
221221
testImplementation("com.squareup.okhttp3:mockwebserver")
222222
testImplementation("com.sun.xml.messaging.saaj:saaj-impl")
223+
testImplementation("io.micrometer:context-propagation")
223224
testImplementation("io.projectreactor:reactor-test")
224225
testImplementation("io.r2dbc:r2dbc-h2")
225226
testImplementation("jakarta.json:jakarta.json-api")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.reactor;
18+
19+
import reactor.core.publisher.Hooks;
20+
21+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
/**
27+
* {@link EnableAutoConfiguration Auto-configuration} for Reactor.
28+
*
29+
* @author Brian Clozel
30+
* @since 3.0.2
31+
*/
32+
@Configuration(proxyBeanMethods = false)
33+
@ConditionalOnClass(Hooks.class)
34+
@EnableConfigurationProperties(ReactorProperties.class)
35+
public class ReactorAutoConfiguration {
36+
37+
public ReactorAutoConfiguration(ReactorProperties properties) {
38+
if (properties.getContextPropagation() == ReactorProperties.ContextPropagationMode.AUTO) {
39+
Hooks.enableAutomaticContextPropagation();
40+
}
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.reactor;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for Reactor.
23+
*
24+
* @author Brian Clozel
25+
* @since 3.0.3
26+
*/
27+
@ConfigurationProperties(prefix = "spring.reactor")
28+
public class ReactorProperties {
29+
30+
/**
31+
* Context Propagation support mode for Reactor operators.
32+
*/
33+
private ContextPropagationMode contextPropagation = ContextPropagationMode.AUTO;
34+
35+
public ContextPropagationMode getContextPropagation() {
36+
return this.contextPropagation;
37+
}
38+
39+
public void setContextPropagation(ContextPropagationMode contextPropagation) {
40+
this.contextPropagation = contextPropagation;
41+
}
42+
43+
public enum ContextPropagationMode {
44+
45+
/**
46+
* Context Propagation is applied to all Reactor operators.
47+
*/
48+
AUTO,
49+
50+
/**
51+
* Context Propagation is only applied to "tap" and "handle" Reactor operators.
52+
*/
53+
LIMITED
54+
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for Reactor.
19+
*/
20+
package org.springframework.boot.autoconfigure.reactor;

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+4
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,10 @@
20902090
"level": "error"
20912091
}
20922092
},
2093+
{
2094+
"name": "spring.reactor.context-propagation",
2095+
"defaultValue": "auto"
2096+
},
20932097
{
20942098
"name": "spring.reactor.stacktrace-mode.enabled",
20952099
"description": "Whether Reactor should collect stacktrace information at runtime.",

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
9797
org.springframework.boot.autoconfigure.pulsar.PulsarAutoConfiguration
9898
org.springframework.boot.autoconfigure.pulsar.PulsarReactiveAutoConfiguration
9999
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration
100+
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration
100101
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration
101102
org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration
102103
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.reactor;
18+
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
import io.micrometer.context.ContextRegistry;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
import reactor.core.publisher.Mono;
25+
import reactor.util.context.Context;
26+
27+
import org.springframework.boot.autoconfigure.AutoConfigurations;
28+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link ReactorAutoConfiguration}.
34+
*
35+
* @author Brian Clozel
36+
*/
37+
class ReactorAutoConfigurationTests {
38+
39+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
40+
.withConfiguration(AutoConfigurations.of(ReactorAutoConfiguration.class));
41+
42+
private static final String THREADLOCAL_KEY = "ReactorAutoConfigurationTests";
43+
44+
private static final ThreadLocal<String> THREADLOCAL_VALUE = ThreadLocal.withInitial(() -> "failure");
45+
46+
@BeforeAll
47+
static void initializeThreadLocalAccessors() {
48+
ContextRegistry globalRegistry = ContextRegistry.getInstance();
49+
globalRegistry.registerThreadLocalAccessor(THREADLOCAL_KEY, THREADLOCAL_VALUE);
50+
}
51+
52+
@Test
53+
void shouldConfigureAutomaticContextPropagation() {
54+
AtomicReference<String> threadLocalValue = new AtomicReference<>();
55+
this.contextRunner.run((applicationContext) -> {
56+
Mono.just("test")
57+
.doOnNext((element) -> threadLocalValue.set(THREADLOCAL_VALUE.get()))
58+
.contextWrite(Context.of(THREADLOCAL_KEY, "success"))
59+
.block();
60+
assertThat(threadLocalValue.get()).isEqualTo("success");
61+
});
62+
}
63+
64+
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/observability.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@ The attributes of the `Resource` can be configured via the configprop:management
8080
NOTE: Spring Boot does not provide auto-configuration for OpenTelemetry metrics or logging.
8181
OpenTelemetry tracing is only auto-configured when used together with <<actuator#actuator.micrometer-tracing, Micrometer Tracing>>.
8282

83+
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.
84+
`ThreadLocal` values are automatically reinstated in reactive operators, this behavior is controlled with the configprop:spring.reactor.context-propagation[] property.
85+
8386
The next sections will provide more details about logging, metrics and traces.

spring-boot-project/spring-boot-parent/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ bom {
138138
]
139139
}
140140
}
141+
library("Micrometer Context Propagation", "1.0.2") {
142+
group("io.micrometer") {
143+
modules = [
144+
"context-propagation"
145+
]
146+
}
147+
}
141148
library("MockK", "1.13.5") {
142149
group("io.mockk") {
143150
modules = [

0 commit comments

Comments
 (0)