From 56594eb5b68b4b5010e1271e16f6086d4366b228 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Mon, 25 Jan 2021 08:40:17 +0100 Subject: [PATCH] Remove construction of URL fields that are filled on APM Server --- .../opentelemetry/sdk/ElasticOTelSpan.java | 27 ------------------- .../sdk/ElasticOpenTelemetryTest.java | 11 ++++---- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/apm-agent-plugins/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOTelSpan.java b/apm-agent-plugins/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOTelSpan.java index fd23287679..cd0e5a1218 100644 --- a/apm-agent-plugins/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOTelSpan.java +++ b/apm-agent-plugins/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOTelSpan.java @@ -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; @@ -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) { @@ -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"); } diff --git a/apm-agent-plugins/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOpenTelemetryTest.java b/apm-agent-plugins/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOpenTelemetryTest.java index 52d2940a4c..4cc9d70be0 100644 --- a/apm-agent-plugins/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOpenTelemetryTest.java +++ b/apm-agent-plugins/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/sdk/ElasticOpenTelemetryTest.java @@ -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 @@ -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 @@ -260,7 +260,7 @@ 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 @@ -268,6 +268,7 @@ 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) @@ -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"); }