diff --git a/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/restclient/ClientTracingFilter.java b/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/restclient/ClientTracingFilter.java index db439d81c5a61..9d34efb149b28 100644 --- a/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/restclient/ClientTracingFilter.java +++ b/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/restclient/ClientTracingFilter.java @@ -1,6 +1,7 @@ package io.quarkus.opentelemetry.runtime.tracing.restclient; import java.io.IOException; +import java.net.URI; import javax.annotation.Priority; import javax.ws.rs.Priorities; @@ -44,7 +45,7 @@ public void filter(ClientRequestContext requestContext) throws IOException { // Add attributes builder.setAttribute(SemanticAttributes.HTTP_METHOD, requestContext.getMethod()); - builder.setAttribute(SemanticAttributes.HTTP_URL, requestContext.getUri().toString()); + builder.setAttribute(SemanticAttributes.HTTP_URL, filterUserInfo(requestContext.getUri().toString())); final Span clientSpan = builder.startSpan(); @@ -54,6 +55,14 @@ public void filter(ClientRequestContext requestContext) throws IOException { TEXT_MAP_PROPAGATOR.inject(Context.current(), requestContext.getHeaders(), SETTER); } + String filterUserInfo(String httpUrl) { + if (httpUrl.contains("@")) { + URI uri = URI.create(httpUrl); + httpUrl = httpUrl.replace(uri.getUserInfo() + "@", ""); + } + return httpUrl; + } + private String getSpanName(ClientRequestContext requestContext) { final String uriPath = requestContext.getUri().getPath(); if (uriPath.length() > 1) { diff --git a/extensions/opentelemetry/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/restclient/HttpUrlFilterTest.java b/extensions/opentelemetry/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/restclient/HttpUrlFilterTest.java new file mode 100644 index 0000000000000..7ce05e09cfa11 --- /dev/null +++ b/extensions/opentelemetry/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/restclient/HttpUrlFilterTest.java @@ -0,0 +1,83 @@ +package io.quarkus.opentelemetry.runtime.tracing.restclient; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class HttpUrlFilterTest { + @Test + void testUrl() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://quarkus.io"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals(url, result); + } + + @Test + void testUrlWithPort() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://quarkus.io:9091"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals(url, result); + } + + @Test + void testUrlWithPath() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://quarkus.io/guides/"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals(url, result); + } + + @Test + void testUserWithEmptyPassword() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://username:@quarkus.io"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals("https://quarkus.io", result); + } + + @Test + void testUserWithEmptyPasswordAndPort() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://username:@quarkus.io:9212"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals("https://quarkus.io:9212", result); + } + + @Test + void testUserWithPassword() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://username:password@quarkus.io"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals("https://quarkus.io", result); + } + + @Test + void testUserWithPasswordAndPort() { + ClientTracingFilter filter = new ClientTracingFilter(null); + + String url = "https://username:password@quarkus.io:9000"; + String result = filter.filterUserInfo(url); + + Assertions.assertNotNull(result); + Assertions.assertEquals("https://quarkus.io:9000", result); + } +}