Would it be possible to make it easy to apply customizers to R2DBC observability?

For example, consider this fuctional interface:

@FunctionalInterface
public interface ProxyConnectionFactoryBuilderCustomizer {
    void customize(ConnectionFactory connectionFactory, ProxyConnectionFactory.Builder builder);
}

The current listener org.springframework.boot.actuate.autoconfigure.r2dbc.R2dbcObservationAutoConfiguration could be re-written:

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

@Bean
@ConditionalOnBean(ObservationRegistry.class)
ProxyConnectionFactoryBuilderCustomizer connectionObserver(R2dbcObservationProperties properties,
                                                           ObservationRegistry observationRegistry,
                                                           ObjectProvider<QueryObservationConvention> queryObservationConvention,
                                                           ObjectProvider<QueryParametersTagProvider> queryParametersTagProvider) {
    return (connectionFactory, builder) -> {
        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);
        builder.listener(listener);
    };
}

With this pattern, it is very easy for other applications to apply their own listeners and customizations to the ProxyConnectionFactory initalization.

Comment From: philwebb

Thanks for the suggestion @pauljohe-dnb. We generally like the idea of a customizer, although having one that accepts two parameters isn't that common.

@ttddyy Is there anything we could get changed in ProxyConnectionFactory.Builder to help remove the need to pass in the ConnectionFactory? Perhaps a getter on the builder for connectionFactory or an alternative listener method that takes a Factory<ConnectionFactory, ProxyExecutionListener>?

Comment From: ttddyy

Perhaps a getter on the builder for connectionFactory

@philwebb Yeah, usually builder doesn't have getters to expose the inside objects, but I do see a value here; so, I can add getters to the builder.

Comment From: ttddyy

@philwebb I have released r2dbc-proxy 1.1.5.RELEASE which now has getters on the builder.

Comment From: philwebb

Closing in favor of PR #40555. Thanks @ttddyy!