Skip to content

Commit

Permalink
Filter HTTP URL for client spans
Browse files Browse the repository at this point in the history
- Fixes quarkusio#18250
- Remove UserInfo portion of HTTP URL, if present

(cherry picked from commit b4b7f84)
  • Loading branch information
kenfinnigan authored and gsmet committed Aug 3, 2021
1 parent aee7a90 commit 1f71024
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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:[email protected]";
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:[email protected]:9000";
String result = filter.filterUserInfo(url);

Assertions.assertNotNull(result);
Assertions.assertEquals("https://quarkus.io:9000", result);
}
}

0 comments on commit 1f71024

Please sign in to comment.