Skip to content

Commit

Permalink
Updated generateRemoteService to parse HTTP_URL as last resort (#750)
Browse files Browse the repository at this point in the history
* Updated generateRemoteService to parse HTTP_URL as last resort

* fixed violations

* updated comments

* applied comments
  • Loading branch information
AsakerMohd authored Feb 20, 2024
1 parent 61460af commit 34a2f17
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ private static String normalizeServiceName(SpanData span, String serviceName) {
* </ul>
*
* 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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34a2f17

Please sign in to comment.