Skip to content

Add a customizer for ProxyConnectionFactory.Builder from r2dbc-proxy #40555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -33,33 +33,53 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.r2dbc.ConnectionFactoryDecorator;
import org.springframework.boot.r2dbc.OptionsCapableConnectionFactory;
import org.springframework.boot.r2dbc.ProxyConnectionFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;

/**
* {@link EnableAutoConfiguration Auto-configuration} for R2DBC observability support.
*
* @author Moritz Halbritter
* @author Tadaya Tsuyukubo
* @since 3.2.0
*/
@AutoConfiguration(after = ObservationAutoConfiguration.class)
@ConditionalOnClass({ ConnectionFactory.class, ProxyConnectionFactory.class })
@EnableConfigurationProperties(R2dbcObservationProperties.class)
public class R2dbcObservationAutoConfiguration {

/**
* {@code @Order} value of observation customizer.
*/
public static final int R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER = 1000;

@Bean
ConnectionFactoryDecorator connectionFactoryDecorator(
ObjectProvider<ProxyConnectionFactoryCustomizer> customizers) {
return (connectionFactory) -> {
ProxyConnectionFactory.Builder builder = ProxyConnectionFactory.builder(connectionFactory);
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
};
}

@Bean
@Order(R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER)
@ConditionalOnBean(ObservationRegistry.class)
ConnectionFactoryDecorator connectionFactoryDecorator(R2dbcObservationProperties properties,
ProxyConnectionFactoryCustomizer proxyConnectionFactoryObservationCustomizer(R2dbcObservationProperties properties,
ObservationRegistry observationRegistry,
ObjectProvider<QueryObservationConvention> queryObservationConvention,
ObjectProvider<QueryParametersTagProvider> queryParametersTagProvider) {
return (connectionFactory) -> {
return (builder) -> {
ConnectionFactory connectionFactory = builder.getConnectionFactory();
HostAndPort hostAndPort = extractHostAndPort(connectionFactory);
ObservationProxyExecutionListener listener = new ObservationProxyExecutionListener(observationRegistry,
connectionFactory, hostAndPort.host(), hostAndPort.port());
listener.setIncludeParameterValues(properties.isIncludeParameterValues());
queryObservationConvention.ifAvailable(listener::setQueryObservationConvention);
queryParametersTagProvider.ifAvailable(listener::setQueryParametersTagProvider);
return ProxyConnectionFactory.builder(connectionFactory).listener(listener).build();
builder.listener(listener);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand All @@ -16,6 +16,8 @@

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

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -25,6 +27,7 @@
import io.r2dbc.spi.ConnectionFactory;
import org.awaitility.Awaitility;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

Expand All @@ -33,16 +36,20 @@
import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.boot.r2dbc.ConnectionFactoryBuilder;
import org.springframework.boot.r2dbc.ConnectionFactoryDecorator;
import org.springframework.boot.r2dbc.ProxyConnectionFactoryCustomizer;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

/**
* Tests for {@link R2dbcObservationAutoConfiguration}.
*
* @author Moritz Halbritter
* @author Tadaya Tsuyukubo
*/
class R2dbcObservationAutoConfigurationTests {

Expand Down Expand Up @@ -78,7 +85,20 @@ void shouldNotSupplyBeansIfR2dbcProxyIsNotOnClasspath() {
@Test
void shouldNotSupplyBeansIfObservationRegistryIsNotPresent() {
this.runnerWithoutObservationRegistry
.run((context) -> assertThat(context).doesNotHaveBean(ConnectionFactoryDecorator.class));
.run((context) -> assertThat(context).doesNotHaveBean(ProxyConnectionFactoryCustomizer.class));
}

@Test
void shouldApplyCustomizers() {
this.runner.withUserConfiguration(ProxyConnectionFactoryCustomizerConfig.class).run((context) -> {
ConnectionFactoryDecorator decorator = context.getBean(ConnectionFactoryDecorator.class);
ConnectionFactory connectionFactory = ConnectionFactoryBuilder
.withUrl("r2dbc:h2:mem:///" + UUID.randomUUID())
.build();
decorator.decorate(connectionFactory);
assertThat(context.getBean(ProxyConnectionFactoryCustomizerConfig.class).called).containsExactly("first",
"second");
});
}

@Test
Expand Down Expand Up @@ -128,4 +148,23 @@ Context awaitContext() {

}

@Configuration(proxyBeanMethods = false)
private static final class ProxyConnectionFactoryCustomizerConfig {

private final List<String> called = new ArrayList<>();

@Bean
@Order(R2dbcObservationAutoConfiguration.R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER - 1)
ProxyConnectionFactoryCustomizer first() {
return (builder) -> this.called.add("first");
}

@Bean
@Order(R2dbcObservationAutoConfiguration.R2DBC_PROXY_OBSERVATION_CUSTOMIZER_ORDER + 1)
ProxyConnectionFactoryCustomizer second() {
return (builder) -> this.called.add("second");
}

}

}
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ bom {
]
}
}
library("R2DBC Proxy", "1.1.4.RELEASE") {
library("R2DBC Proxy", "1.1.5.RELEASE") {
considerSnapshots()
group("io.r2dbc") {
modules = [
Expand Down
1 change: 1 addition & 0 deletions spring-boot-project/spring-boot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
optional("io.projectreactor:reactor-tools")
optional("io.projectreactor.netty:reactor-netty-http")
optional("io.r2dbc:r2dbc-pool")
optional("io.r2dbc:r2dbc-proxy")
optional("io.rsocket:rsocket-core")
optional("io.rsocket:rsocket-transport-netty")
optional("io.undertow:undertow-servlet")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2012-2024 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.r2dbc;

import io.r2dbc.proxy.ProxyConnectionFactory;

/**
* Callback interface that can be used to customize a
* {@link ProxyConnectionFactory.Builder}.
*
* @author Tadaya Tsuyukubo
* @since 3.3
*/
public interface ProxyConnectionFactoryCustomizer {

/**
* Callback to customize a {@link ProxyConnectionFactory.Builder} instance.
* @param builder the builder to customize
*/
void customize(ProxyConnectionFactory.Builder builder);

}