From 34a2f176e194f325198ff05c468f153978f61653 Mon Sep 17 00:00:00 2001 From: Mohamed Asaker Date: Tue, 20 Feb 2024 11:27:18 -0800 Subject: [PATCH] Updated generateRemoteService to parse HTTP_URL as last resort (#750) * Updated generateRemoteService to parse HTTP_URL as last resort * fixed violations * updated comments * applied comments --- .../AwsMetricAttributeGenerator.java | 19 ++++++++++-- .../AwsMetricAttributeGeneratorTest.java | 30 ++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java b/awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java index b316a982f6..c783659af0 100644 --- a/awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java +++ b/awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java @@ -253,9 +253,9 @@ private static String normalizeServiceName(SpanData span, String serviceName) { * * * if the selected attributes are still producing the UnknownRemoteService or - * UnknownRemoteOperation, `net.peer.name`, `net.peer.port`, `net.peer.sock.addr` and - * `net.peer.sock.port` will be used to derive the RemoteService. And `http.method` and `http.url` - * will be used to derive the RemoteOperation. + * UnknownRemoteOperation, `net.peer.name`, `net.peer.port`, `net.peer.sock.addr`, + * `net.peer.sock.port` and `http.url` will be used to derive the RemoteService. And `http.method` + * and `http.url` will be used to derive the RemoteOperation. */ private static void setRemoteServiceAndOperation(SpanData span, AttributesBuilder builder) { String remoteService = UNKNOWN_REMOTE_SERVICE; @@ -339,6 +339,19 @@ private static String generateRemoteService(SpanData span) { Long port = span.getAttributes().get(NET_SOCK_PEER_PORT); remoteService += ":" + port; } + } else if (isKeyPresent(span, HTTP_URL)) { + String httpUrl = span.getAttributes().get(HTTP_URL); + try { + URL url = new URL(httpUrl); + if (!url.getHost().isEmpty()) { + remoteService = url.getHost(); + if (url.getPort() != -1) { + remoteService += ":" + url.getPort(); + } + } + } catch (MalformedURLException e) { + logger.log(Level.FINEST, "invalid http.url attribute: ", httpUrl); + } } else { logUnknownAttribute(AWS_REMOTE_SERVICE, span); } diff --git a/awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java b/awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java index 60a10b19b5..baca8bf193 100644 --- a/awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java +++ b/awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java @@ -499,14 +499,36 @@ public void testRemoteAttributesCombinations() { mockAttribute(NET_SOCK_PEER_ADDR, null); mockAttribute(NET_SOCK_PEER_PORT, null); - // Validate behavior of Remote Operation from HttpTarget - with 1st api part, then remove it + // Validate behavior of Remote Operation from HttpTarget - with 1st api part. Also validates + // that RemoteService is extracted from HttpUrl. mockAttribute(HTTP_URL, "http://www.example.com/payment/123"); - validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, "/payment"); + validateExpectedRemoteAttributes("www.example.com", "/payment"); mockAttribute(HTTP_URL, null); - // Validate behavior of Remote Operation from HttpTarget - without 1st api part, then remove it + // Validate behavior of Remote Operation from HttpTarget - with 1st api part. Also validates + // that RemoteService is extracted from HttpUrl. mockAttribute(HTTP_URL, "http://www.example.com"); - validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, "/"); + validateExpectedRemoteAttributes("www.example.com", "/"); + mockAttribute(HTTP_URL, null); + + // Validate behavior of Remote Service from HttpUrl + mockAttribute(HTTP_URL, "http://192.168.1.1:8000"); + validateExpectedRemoteAttributes("192.168.1.1:8000", "/"); + mockAttribute(HTTP_URL, null); + + // Validate behavior of Remote Service from HttpUrl + mockAttribute(HTTP_URL, "http://192.168.1.1"); + validateExpectedRemoteAttributes("192.168.1.1", "/"); + mockAttribute(HTTP_URL, null); + + // Validate behavior of Remote Service from HttpUrl + mockAttribute(HTTP_URL, ""); + validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, UNKNOWN_REMOTE_OPERATION); + mockAttribute(HTTP_URL, null); + + // Validate behavior of Remote Service from HttpUrl + mockAttribute(HTTP_URL, null); + validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, UNKNOWN_REMOTE_OPERATION); mockAttribute(HTTP_URL, null); // Validate behavior of Remote Operation from HttpTarget - invalid url, then remove it