Skip to content

Commit

Permalink
Remove construction of URL fields that are filled on APM Server
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Jan 25, 2021
1 parent a7899d9 commit 56594eb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;

import javax.annotation.Nonnull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -126,19 +124,6 @@ private void mapTransactionAttributes(Transaction t, AttributeKey<?> key, Object
StringBuilder fullURl = request.getUrl().getFull();
fullURl.setLength(0);
fullURl.append((String) value);
try {
URL url = new URL((String) value);
request.getUrl().withSearch(url.getQuery());
request.getUrl().withProtocol(url.getProtocol());
request.getUrl().withPathname(url.getPath());
request.getUrl().withHostname(url.getHost());
int port = url.getPort();
port = port > 0 ? port : url.getDefaultPort();
if (port > 0) {
request.getUrl().withPort(port);
}
} catch (MalformedURLException ignore) {
}
} else if (key.equals(SemanticAttributes.HTTP_TARGET)) {
StringBuilder fullURl = request.getUrl().getFull();
if (fullURl.length() == 0) {
Expand Down Expand Up @@ -205,18 +190,6 @@ private void onTransactionEnd() {
request.getSocket().withRemoteAddress((remoteAddress == null ? "" : remoteAddress) + ":" + entry.getValue());
}
}
// if the URL starts with / we have only captured the http.target and have to construct the full url
if (requestUrl.getFull().length() > 0 && requestUrl.getFull().charAt(0) == '/') {
String httpTarget = requestUrl.getFull().toString();
requestUrl.getFull().setLength(0);
requestUrl
.appendToFull(requestUrl.getProtocol())
.appendToFull("://")
.appendToFull(requestUrl.getHostname())
.appendToFull(requestUrl.getPort().length() > 0 ? ":" + requestUrl.getPort() : "")
.appendToFull(httpTarget);
}

} else {
t.withType("unknown");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void testTransactionSemanticConventionMappingHttpHost() {
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getProtocol()).isEqualTo("http");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getHostname()).isEqualTo("www.example.com");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getPort().toString()).isEqualTo("8080");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("http://www.example.com:8080/foo?bar");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("/foo?bar");
}

@Test
Expand All @@ -236,7 +236,7 @@ void testTransactionSemanticConventionMappingHttpNetHostName() {
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getHostname()).isEqualTo("example.com");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getPort().toString()).isEqualTo("8080");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getSocket().getRemoteAddress()).isEqualTo("192.168.178.1:123456");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("http://example.com:8080/foo?bar");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("/foo?bar");
}

@Test
Expand All @@ -260,14 +260,15 @@ void testTransactionSemanticConventionMappingHttpNetHostIP() {
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getHostname()).isEqualTo("127.0.0.1");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getPort().toString()).isEqualTo("8080");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getSocket().getRemoteAddress()).isEqualTo("192.168.178.1:123456");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("http://127.0.0.1:8080/foo?bar");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("/foo?bar");
}

@Test
void testTransactionSemanticConventionMappingHttpUrl() {
otelTracer.spanBuilder("transaction")
.startSpan()
.setAttribute(SemanticAttributes.HTTP_METHOD, "GET")
.setAttribute(SemanticAttributes.HTTP_SCHEME, "http")
.setAttribute(SemanticAttributes.HTTP_URL, "http://example.com:8080/foo?bar")
.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, 200L)
.setAttribute(SemanticAttributes.NET_PEER_PORT, 123456)
Expand All @@ -278,8 +279,8 @@ void testTransactionSemanticConventionMappingHttpUrl() {
assertThat(reporter.getFirstTransaction().getContext().getResponse().getStatusCode()).isEqualTo(200);
assertThat(reporter.getFirstTransaction().getContext().getRequest().getMethod()).isEqualTo("GET");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getProtocol()).isEqualTo("http");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getHostname()).isEqualTo("example.com");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getPort().toString()).isEqualTo("8080");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getHostname()).isNull();
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getPort().toString()).isEmpty();
assertThat(reporter.getFirstTransaction().getContext().getRequest().getSocket().getRemoteAddress()).isEqualTo("192.168.178.1:123456");
assertThat(reporter.getFirstTransaction().getContext().getRequest().getUrl().getFull().toString()).isEqualTo("http://example.com:8080/foo?bar");
}
Expand Down

0 comments on commit 56594eb

Please sign in to comment.