Skip to content

Commit

Permalink
Allow null host param on ObservationProxyExecutionListener constructor
Browse files Browse the repository at this point in the history
Relates #135
  • Loading branch information
ttddyy committed Aug 2, 2023
1 parent 51089d6 commit 2182c4b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ObservationProxyExecutionListener(ObservationRegistry observationRegistry
* @since 1.1.2
*/
public ObservationProxyExecutionListener(ObservationRegistry observationRegistry,
ConnectionFactory connectionFactory, String host, @Nullable Integer port) {
ConnectionFactory connectionFactory, @Nullable String host, @Nullable Integer port) {
this.observationRegistry = observationRegistry;
this.connectionFactory = connectionFactory;
this.remoteServiceAddress = buildRemoteServiceAddress(host, port);
Expand All @@ -110,6 +110,9 @@ private String parseR2dbcConnectionUrl(String connectionUrl) {

@Nullable
private String buildRemoteServiceAddress(@Nullable String host, @Nullable Integer portNumber) {
if (host == null) {
return null;
}
int port = portNumber != null ? portNumber : -1;
try {
URI uri = new URI(null, null, host, port, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.micrometer.tracing.propagation.Propagator;
import io.micrometer.tracing.test.simple.SimpleSpan;
import io.micrometer.tracing.test.simple.SimpleTracer;
import io.micrometer.tracing.test.simple.SpanAssert;
import io.micrometer.tracing.test.simple.TracingAssertions;
import io.r2dbc.proxy.core.Bindings;
import io.r2dbc.proxy.core.BoundValue;
Expand All @@ -41,6 +42,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
import reactor.util.context.ContextView;

Expand Down Expand Up @@ -195,7 +197,7 @@ private static Stream<Arguments> remoteServiceAddressWithConnectionUrl() {

@ParameterizedTest
@MethodSource
void remoteServiceAddressWithConnectionHostAndPort(String host, Integer port, String expectedIp, int expectedPort) {
void remoteServiceAddressWithConnectionHostAndPort(String host, Integer port, String expectedIp, Integer expectedPort) {
runAndVerifyRemoteServiceAddress(() -> {
ConnectionFactory connectionFactory = createMockConnectionFactory();
return new ObservationProxyExecutionListener(this.registry, connectionFactory, host, port);
Expand All @@ -206,21 +208,33 @@ private static Stream<Arguments> remoteServiceAddressWithConnectionHostAndPort()
return Stream.of(
Arguments.of("192.168.1.1", 5432, "192.168.1.1", 5432),
Arguments.of("192.168.1.1", null, "192.168.1.1", -1),
Arguments.of("192.168.1.1", -1, "192.168.1.1", -1)
Arguments.of("192.168.1.1", -1, "192.168.1.1", -1),
Arguments.of(null, 5432, null, null),
Arguments.of(null, -1, null, null),
Arguments.of(null, null, null, null)
);
}

void runAndVerifyRemoteServiceAddress(Supplier<ObservationProxyExecutionListener> listenerSupplier, String expectedIp, int expectedPort) {
void runAndVerifyRemoteServiceAddress(Supplier<ObservationProxyExecutionListener> listenerSupplier, @Nullable String expectedIp, @Nullable Integer expectedPort) {
this.registry.observationConfig().observationHandler(new PropagatingSenderTracingObservationHandler<QueryContext>(this.tracer, NOOP_PROPAGATOR));
QueryExecutionInfo queryExecutionInfo = createQueryExecutionInfo();

ObservationProxyExecutionListener listener = listenerSupplier.get();
listener.beforeQuery(queryExecutionInfo);
listener.afterQuery(queryExecutionInfo);

TracingAssertions.assertThat(this.tracer).onlySpan()
.hasIpEqualTo(expectedIp)
.hasPortEqualTo(expectedPort);
TracingAssertions.assertThat(this.tracer).onlySpan();
SimpleSpan span = this.tracer.onlySpan();
if (expectedIp == null) {
SpanAssert.assertThat(span).hasIpThatIsBlank();
} else {
SpanAssert.assertThat(span).hasIpEqualTo(expectedIp);
}
if (expectedPort == null) {
SpanAssert.assertThat(span).hasPortThatIsNotSet();
} else {
SpanAssert.assertThat(span).hasPortEqualTo(expectedPort);
}
}

}

0 comments on commit 2182c4b

Please sign in to comment.