Skip to content

Commit 34a2f17

Browse files
authored
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
1 parent 61460af commit 34a2f17

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ private static String normalizeServiceName(SpanData span, String serviceName) {
253253
* </ul>
254254
*
255255
* if the selected attributes are still producing the UnknownRemoteService or
256-
* UnknownRemoteOperation, `net.peer.name`, `net.peer.port`, `net.peer.sock.addr` and
257-
* `net.peer.sock.port` will be used to derive the RemoteService. And `http.method` and `http.url`
258-
* will be used to derive the RemoteOperation.
256+
* UnknownRemoteOperation, `net.peer.name`, `net.peer.port`, `net.peer.sock.addr`,
257+
* `net.peer.sock.port` and `http.url` will be used to derive the RemoteService. And `http.method`
258+
* and `http.url` will be used to derive the RemoteOperation.
259259
*/
260260
private static void setRemoteServiceAndOperation(SpanData span, AttributesBuilder builder) {
261261
String remoteService = UNKNOWN_REMOTE_SERVICE;
@@ -339,6 +339,19 @@ private static String generateRemoteService(SpanData span) {
339339
Long port = span.getAttributes().get(NET_SOCK_PEER_PORT);
340340
remoteService += ":" + port;
341341
}
342+
} else if (isKeyPresent(span, HTTP_URL)) {
343+
String httpUrl = span.getAttributes().get(HTTP_URL);
344+
try {
345+
URL url = new URL(httpUrl);
346+
if (!url.getHost().isEmpty()) {
347+
remoteService = url.getHost();
348+
if (url.getPort() != -1) {
349+
remoteService += ":" + url.getPort();
350+
}
351+
}
352+
} catch (MalformedURLException e) {
353+
logger.log(Level.FINEST, "invalid http.url attribute: ", httpUrl);
354+
}
342355
} else {
343356
logUnknownAttribute(AWS_REMOTE_SERVICE, span);
344357
}

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,36 @@ public void testRemoteAttributesCombinations() {
499499
mockAttribute(NET_SOCK_PEER_ADDR, null);
500500
mockAttribute(NET_SOCK_PEER_PORT, null);
501501

502-
// Validate behavior of Remote Operation from HttpTarget - with 1st api part, then remove it
502+
// Validate behavior of Remote Operation from HttpTarget - with 1st api part. Also validates
503+
// that RemoteService is extracted from HttpUrl.
503504
mockAttribute(HTTP_URL, "http://www.example.com/payment/123");
504-
validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, "/payment");
505+
validateExpectedRemoteAttributes("www.example.com", "/payment");
505506
mockAttribute(HTTP_URL, null);
506507

507-
// Validate behavior of Remote Operation from HttpTarget - without 1st api part, then remove it
508+
// Validate behavior of Remote Operation from HttpTarget - with 1st api part. Also validates
509+
// that RemoteService is extracted from HttpUrl.
508510
mockAttribute(HTTP_URL, "http://www.example.com");
509-
validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, "/");
511+
validateExpectedRemoteAttributes("www.example.com", "/");
512+
mockAttribute(HTTP_URL, null);
513+
514+
// Validate behavior of Remote Service from HttpUrl
515+
mockAttribute(HTTP_URL, "http://192.168.1.1:8000");
516+
validateExpectedRemoteAttributes("192.168.1.1:8000", "/");
517+
mockAttribute(HTTP_URL, null);
518+
519+
// Validate behavior of Remote Service from HttpUrl
520+
mockAttribute(HTTP_URL, "http://192.168.1.1");
521+
validateExpectedRemoteAttributes("192.168.1.1", "/");
522+
mockAttribute(HTTP_URL, null);
523+
524+
// Validate behavior of Remote Service from HttpUrl
525+
mockAttribute(HTTP_URL, "");
526+
validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, UNKNOWN_REMOTE_OPERATION);
527+
mockAttribute(HTTP_URL, null);
528+
529+
// Validate behavior of Remote Service from HttpUrl
530+
mockAttribute(HTTP_URL, null);
531+
validateExpectedRemoteAttributes(UNKNOWN_REMOTE_SERVICE, UNKNOWN_REMOTE_OPERATION);
510532
mockAttribute(HTTP_URL, null);
511533

512534
// Validate behavior of Remote Operation from HttpTarget - invalid url, then remove it

0 commit comments

Comments
 (0)