Skip to content

Commit 75ae7c9

Browse files
ttddyymhalbritter
authored andcommitted
Add ProxyConnectionFactoryCustomizer
See gh-40555
1 parent a2378e1 commit 75ae7c9

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfiguration.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,33 +33,53 @@
3333
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3434
import org.springframework.boot.r2dbc.ConnectionFactoryDecorator;
3535
import org.springframework.boot.r2dbc.OptionsCapableConnectionFactory;
36+
import org.springframework.boot.r2dbc.ProxyConnectionFactoryCustomizer;
3637
import org.springframework.context.annotation.Bean;
38+
import org.springframework.core.annotation.Order;
3739

3840
/**
3941
* {@link EnableAutoConfiguration Auto-configuration} for R2DBC observability support.
4042
*
4143
* @author Moritz Halbritter
44+
* @author Tadaya Tsuyukubo
4245
* @since 3.2.0
4346
*/
4447
@AutoConfiguration(after = ObservationAutoConfiguration.class)
4548
@ConditionalOnClass({ ConnectionFactory.class, ProxyConnectionFactory.class })
4649
@EnableConfigurationProperties(R2dbcObservationProperties.class)
4750
public class R2dbcObservationAutoConfiguration {
4851

52+
/**
53+
* {@code @Order} value of observation customizer.
54+
*/
55+
public static final int R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER = 1000;
56+
4957
@Bean
58+
ConnectionFactoryDecorator connectionFactoryDecorator(
59+
ObjectProvider<ProxyConnectionFactoryCustomizer> customizers) {
60+
return (connectionFactory) -> {
61+
ProxyConnectionFactory.Builder builder = ProxyConnectionFactory.builder(connectionFactory);
62+
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
63+
return builder.build();
64+
};
65+
}
66+
67+
@Bean
68+
@Order(R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER)
5069
@ConditionalOnBean(ObservationRegistry.class)
51-
ConnectionFactoryDecorator connectionFactoryDecorator(R2dbcObservationProperties properties,
70+
ProxyConnectionFactoryCustomizer proxyConnectionFactoryObservationCustomizer(R2dbcObservationProperties properties,
5271
ObservationRegistry observationRegistry,
5372
ObjectProvider<QueryObservationConvention> queryObservationConvention,
5473
ObjectProvider<QueryParametersTagProvider> queryParametersTagProvider) {
55-
return (connectionFactory) -> {
74+
return (builder) -> {
75+
ConnectionFactory connectionFactory = builder.getConnectionFactory();
5676
HostAndPort hostAndPort = extractHostAndPort(connectionFactory);
5777
ObservationProxyExecutionListener listener = new ObservationProxyExecutionListener(observationRegistry,
5878
connectionFactory, hostAndPort.host(), hostAndPort.port());
5979
listener.setIncludeParameterValues(properties.isIncludeParameterValues());
6080
queryObservationConvention.ifAvailable(listener::setQueryObservationConvention);
6181
queryParametersTagProvider.ifAvailable(listener::setQueryParametersTagProvider);
62-
return ProxyConnectionFactory.builder(connectionFactory).listener(listener).build();
82+
builder.listener(listener);
6383
};
6484
}
6585

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfigurationTests.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.r2dbc;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.UUID;
2022
import java.util.concurrent.atomic.AtomicReference;
2123

@@ -25,6 +27,7 @@
2527
import io.r2dbc.spi.ConnectionFactory;
2628
import org.awaitility.Awaitility;
2729
import org.hamcrest.Matchers;
30+
import org.junit.jupiter.api.Order;
2831
import org.junit.jupiter.api.Test;
2932
import reactor.core.publisher.Mono;
3033

@@ -33,16 +36,20 @@
3336
import org.springframework.boot.context.annotation.ImportCandidates;
3437
import org.springframework.boot.r2dbc.ConnectionFactoryBuilder;
3538
import org.springframework.boot.r2dbc.ConnectionFactoryDecorator;
39+
import org.springframework.boot.r2dbc.ProxyConnectionFactoryCustomizer;
3640
import org.springframework.boot.test.context.FilteredClassLoader;
3741
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3842
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
43+
import org.springframework.context.annotation.Bean;
44+
import org.springframework.context.annotation.Configuration;
3945

4046
import static org.assertj.core.api.Assertions.assertThat;
4147

4248
/**
4349
* Tests for {@link R2dbcObservationAutoConfiguration}.
4450
*
4551
* @author Moritz Halbritter
52+
* @author Tadaya Tsuyukubo
4653
*/
4754
class R2dbcObservationAutoConfigurationTests {
4855

@@ -78,7 +85,20 @@ void shouldNotSupplyBeansIfR2dbcProxyIsNotOnClasspath() {
7885
@Test
7986
void shouldNotSupplyBeansIfObservationRegistryIsNotPresent() {
8087
this.runnerWithoutObservationRegistry
81-
.run((context) -> assertThat(context).doesNotHaveBean(ConnectionFactoryDecorator.class));
88+
.run((context) -> assertThat(context).doesNotHaveBean(ProxyConnectionFactoryCustomizer.class));
89+
}
90+
91+
@Test
92+
void shouldApplyCustomizers() {
93+
this.runner.withUserConfiguration(ProxyConnectionFactoryCustomizerConfig.class).run((context) -> {
94+
ConnectionFactoryDecorator decorator = context.getBean(ConnectionFactoryDecorator.class);
95+
ConnectionFactory connectionFactory = ConnectionFactoryBuilder
96+
.withUrl("r2dbc:h2:mem:///" + UUID.randomUUID())
97+
.build();
98+
decorator.decorate(connectionFactory);
99+
assertThat(context.getBean(ProxyConnectionFactoryCustomizerConfig.class).called).containsExactly("first",
100+
"second");
101+
});
82102
}
83103

84104
@Test
@@ -128,4 +148,23 @@ Context awaitContext() {
128148

129149
}
130150

151+
@Configuration(proxyBeanMethods = false)
152+
private static final class ProxyConnectionFactoryCustomizerConfig {
153+
154+
private final List<String> called = new ArrayList<>();
155+
156+
@Bean
157+
@Order(R2dbcObservationAutoConfiguration.R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER - 1)
158+
ProxyConnectionFactoryCustomizer first() {
159+
return (builder) -> this.called.add("first");
160+
}
161+
162+
@Bean
163+
@Order(R2dbcObservationAutoConfiguration.R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER + 1)
164+
ProxyConnectionFactoryCustomizer second() {
165+
return (builder) -> this.called.add("second");
166+
}
167+
168+
}
169+
131170
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies {
3535
optional("io.projectreactor:reactor-tools")
3636
optional("io.projectreactor.netty:reactor-netty-http")
3737
optional("io.r2dbc:r2dbc-pool")
38+
optional("io.r2dbc:r2dbc-proxy")
3839
optional("io.rsocket:rsocket-core")
3940
optional("io.rsocket:rsocket-transport-netty")
4041
optional("io.undertow:undertow-servlet")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2024 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.r2dbc;
18+
19+
import io.r2dbc.proxy.ProxyConnectionFactory;
20+
21+
/**
22+
* Callback interface that can be used to customize a
23+
* {@link ProxyConnectionFactory.Builder}.
24+
*
25+
* @author Tadaya Tsuyukubo
26+
* @since 3.3
27+
*/
28+
public interface ProxyConnectionFactoryCustomizer {
29+
30+
/**
31+
* Callback to customize a {@link ProxyConnectionFactory.Builder} instance.
32+
* @param builder the builder to customize
33+
*/
34+
void customize(ProxyConnectionFactory.Builder builder);
35+
36+
}

0 commit comments

Comments
 (0)