From 20d264150ba5fb78a9ecb17057922dad0fa18364 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 2 Aug 2023 11:41:30 +0200 Subject: [PATCH] Polish R2dbcObservationAutoConfiguration --- .../R2dbcObservationAutoConfiguration.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfiguration.java index 6065ca6765..0634770640 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/r2dbc/R2dbcObservationAutoConfiguration.java @@ -53,8 +53,9 @@ public class R2dbcObservationAutoConfiguration { ObjectProvider queryObservationConvention, ObjectProvider queryParametersTagProvider) { return (connectionFactory) -> { + HostAndPort hostAndPort = extractHostAndPort(connectionFactory); ObservationProxyExecutionListener listener = new ObservationProxyExecutionListener(observationRegistry, - connectionFactory, extractUrl(connectionFactory)); + connectionFactory, hostAndPort.host(), hostAndPort.port()); listener.setIncludeParameterValues(properties.isIncludeParameterValues()); queryObservationConvention.ifAvailable(listener::setQueryObservationConvention); queryParametersTagProvider.ifAvailable(listener::setQueryParametersTagProvider); @@ -62,20 +63,25 @@ public class R2dbcObservationAutoConfiguration { }; } - private String extractUrl(ConnectionFactory connectionFactory) { + private HostAndPort extractHostAndPort(ConnectionFactory connectionFactory) { OptionsCapableConnectionFactory optionsCapableConnectionFactory = OptionsCapableConnectionFactory .unwrapFrom(connectionFactory); if (optionsCapableConnectionFactory == null) { - return null; + return HostAndPort.empty(); } ConnectionFactoryOptions options = optionsCapableConnectionFactory.getOptions(); Object host = options.getValue(ConnectionFactoryOptions.HOST); Object port = options.getValue(ConnectionFactoryOptions.PORT); - if (host == null || !(port instanceof Integer portAsInt)) { - return null; + if ((!(host instanceof String hostAsString) || !(port instanceof Integer portAsInt))) { + return HostAndPort.empty(); + } + return new HostAndPort(hostAsString, portAsInt); + } + + private record HostAndPort(String host, Integer port) { + static HostAndPort empty() { + return new HostAndPort(null, null); } - // See https://github.com/r2dbc/r2dbc-proxy/issues/135 - return "r2dbc:dummy://%s:%d/".formatted(host, portAsInt); } }