Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update http dependency name #1884

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ private void exportRemoteDependency(SpanData span, boolean inProc) {
private static String getDependencyName(SpanData span) {
String name = span.getName();

String method = span.getAttributes().get(SemanticAttributes.HTTP_METHOD);
if (method == null) {
return name;
}

if (!DEFAULT_HTTP_SPAN_NAMES.contains(name)) {
return name;
}
Expand All @@ -309,7 +314,10 @@ private static String getDependencyName(SpanData span) {
}

String path = UrlParser.getPathFromUrl(url);
return path != null ? path : name;
if (path == null) {
return name;
}
return path.isEmpty() ? method + " /" : method + " " + path;
}

private static void applySemanticConventions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,68 @@

package com.microsoft.applicationinsights.agent.internal.exporter;

import org.checkerframework.checker.nullness.qual.Nullable;

class UrlParser {

/**
* Returns the path portion of the url.
*
* <p>Returns {@code null} if the path cannot be extracted from url for any reason.
*/
@Nullable
static String getPathFromUrl(String url) {
Copy link
Contributor

@heyams heyams Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like URL.getPath() does the same thing? it handles case like ?query and #. Can we convert url string to an URL object and then use getPath directly, and we can get rid of UrlParser?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructing URL objects is slow: open-telemetry/opentelemetry-java-instrumentation#3699

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be nice to compare the perf side by side. was URLStreamHandler.parseURL slow or the constructor of URL?
inside the constructor it will invoke parseURL from the handler.. I am wondering if the newer version of java has improved it..

// TODO implement something efficient and add tests
return null;

int schemeEndIndex = url.indexOf(':');
if (schemeEndIndex == -1) {
// not a valid url
return null;
}

int len = url.length();
if (schemeEndIndex + 2 < len
&& url.charAt(schemeEndIndex + 1) == '/'
&& url.charAt(schemeEndIndex + 2) == '/') {
// has authority component
// look for
// '/' - start of path
// '?' or end of string - empty path
int pathStartIndex = -1;
for (int i = schemeEndIndex + 3; i < len; i++) {
char c = url.charAt(i);
if (c == '/') {
pathStartIndex = i;
break;
} else if (c == '?' || c == '#') {
// empty path
return "";
}
}
if (pathStartIndex == -1) {
// end of the url was reached while scanning for the beginning of the path
// which means the path is empty
return "";
}
int pathEndIndex = getPathEndIndex(url, pathStartIndex + 1);
return url.substring(pathStartIndex, pathEndIndex);
} else {
// has no authority, path starts right away
int pathStartIndex = schemeEndIndex + 1;
int pathEndIndex = getPathEndIndex(url, pathStartIndex);
return url.substring(pathStartIndex, pathEndIndex);
}
}

// returns the ending index of the path component (exclusive)
private static int getPathEndIndex(String url, int startIndex) {
int len = url.length();
for (int i = startIndex; i < len; i++) {
char c = url.charAt(i);
if (c == '?' || c == '#') {
return i;
}
}
return len;
}

private UrlParser() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent.internal.exporter;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class UrlParserTest {

@Test
public void testGetPathFromUrl() {
assertThat(UrlParser.getPathFromUrl("https://localhost")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost/")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost/path")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/path/")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path/")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost?")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost/?")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost/path?")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/path/?")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path?")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path/?")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost?query")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost/?query")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost/path?query")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/path/?query")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path?query"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path/?query"))
.isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost#")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost/#")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost/path#")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/path/#")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path#")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path/#")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost#fragment")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost/#fragment")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost/path#fragment")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/path/#fragment")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path#fragment"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost/more/path/#fragment"))
.isEqualTo("/more/path/");
}

@Test
public void testGetPathFromUrlWithPort() {
assertThat(UrlParser.getPathFromUrl("https://localhost:8080")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path/")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path/"))
.isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost:8080?")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/?")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path?")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path/?")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path?"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path/?"))
.isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost:8080?query")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/?query")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path?query")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path/?query")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path?query"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path/?query"))
.isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost:8080#")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/#")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path#")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path/#")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path#"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path/#"))
.isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https://localhost:8080#fragment")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/#fragment")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path#fragment")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/path/#fragment"))
.isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path#fragment"))
.isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https://localhost:8080/more/path/#fragment"))
.isEqualTo("/more/path/");
}

@Test
public void testGetPathFromUrlWithNoAuthority() {
assertThat(UrlParser.getPathFromUrl("https:")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https:/")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https:/path")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https:/path/")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https:/more/path")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https:/more/path/")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https:?")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https:/?")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https:/path?")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https:/path/?")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https:/more/path?")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https:/more/path/?")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https:?query")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https:/?query")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https:/path?query")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https:/path/?query")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https:/more/path?query")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https:/more/path/?query")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https:#")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https:/#")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https:/path#")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https:/path/#")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https:/more/path#")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https:/more/path/#")).isEqualTo("/more/path/");

assertThat(UrlParser.getPathFromUrl("https:#fragment")).isEqualTo("");
assertThat(UrlParser.getPathFromUrl("https:/#fragment")).isEqualTo("/");
assertThat(UrlParser.getPathFromUrl("https:/path#fragment")).isEqualTo("/path");
assertThat(UrlParser.getPathFromUrl("https:/path/#fragment")).isEqualTo("/path/");
assertThat(UrlParser.getPathFromUrl("https:/more/path#fragment")).isEqualTo("/more/path");
assertThat(UrlParser.getPathFromUrl("https:/more/path/#fragment")).isEqualTo("/more/path/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static void verify(String capturedUrl) throws Exception {
assertTrue(telemetry.rd.getProperties().isEmpty());
assertTrue(telemetry.rd.getSuccess());

assertEquals("HTTP GET", telemetry.rdd1.getName());
assertEquals("GET /search", telemetry.rdd1.getName());
assertEquals(capturedUrl, telemetry.rdd1.getData());
assertEquals("Http", telemetry.rdd1.getType());
assertEquals("www.bing.com", telemetry.rdd1.getTarget());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void doMostBasicTest() throws Exception {

Envelope rdEnvelope2 = getRequestEnvelope(rdList, "message process");
Envelope rddEnvelope1 = getDependencyEnvelope(rddList, "message send");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "HTTP GET");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "GET /");

RequestData rd1 = (RequestData) ((Data<?>) rdEnvelope1.getData()).getBaseData();
RequestData rd2 = (RequestData) ((Data<?>) rdEnvelope2.getData()).getBaseData();
Expand All @@ -74,7 +74,7 @@ public void doMostBasicTest() throws Exception {
assertTrue(rd2.getProperties().isEmpty());
assertTrue(rd2.getSuccess());

assertEquals("HTTP GET", rdd2.getName());
assertEquals("GET /", rdd2.getName());
assertEquals("https://www.bing.com", rdd2.getData());
assertEquals("Http", rdd2.getType());
assertEquals("www.bing.com", rdd2.getTarget());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void doMostBasicTest() throws Exception {
}
rdd = (RemoteDependencyData) ((Data<?>) rddEnvelope.getData()).getBaseData();

assertEquals("HTTP GET", rdd.getName());
assertEquals("GET /", rdd.getName());
assertEquals("https://www.bing.com", rdd.getData());
assertTrue(rdd.getProperties().isEmpty());
assertTrue(rdd.getSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void doMostBasicTest() throws Exception {
Envelope rdEnvelope2 = getRequestEnvelope(rdList, "message process");
Envelope rddEnvelope1 = getDependencyEnvelope(rddList, "HelloController.sendMessage");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "message send");
Envelope rddEnvelope3 = getDependencyEnvelope(rddList, "HTTP GET");
Envelope rddEnvelope3 = getDependencyEnvelope(rddList, "GET /");

RequestData rd1 = (RequestData) ((Data<?>) rdEnvelope1.getData()).getBaseData();
RequestData rd2 = (RequestData) ((Data<?>) rdEnvelope2.getData()).getBaseData();
Expand Down Expand Up @@ -84,7 +84,7 @@ public void doMostBasicTest() throws Exception {
assertTrue(rd2.getProperties().isEmpty());
assertTrue(rd2.getSuccess());

assertEquals("HTTP GET", rdd3.getName());
assertEquals("GET /", rdd3.getName());
assertEquals("https://www.bing.com", rdd3.getData());
assertEquals("Http", rdd3.getType());
assertEquals("www.bing.com", rdd3.getTarget());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void doMostBasicTest() throws Exception {

Envelope rdEnvelope2 = getRequestEnvelope(rdList, "mytopic process");
Envelope rddEnvelope1 = getDependencyEnvelope(rddList, "mytopic send");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "HTTP GET");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "GET /");

RequestData rd1 = (RequestData) ((Data<?>) rdEnvelope1.getData()).getBaseData();
RequestData rd2 = (RequestData) ((Data<?>) rdEnvelope2.getData()).getBaseData();
Expand All @@ -90,7 +90,7 @@ public void doMostBasicTest() throws Exception {
assertTrue(rd2.getProperties().isEmpty());
assertTrue(rd2.getSuccess());

assertEquals("HTTP GET", rdd2.getName());
assertEquals("GET /", rdd2.getName());
assertEquals("https://www.bing.com", rdd2.getData());
assertEquals("Http", rdd2.getType());
assertEquals("www.bing.com", rdd2.getTarget());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void doMostBasicTest() throws Exception {
}
rdd = (RemoteDependencyData) ((Data<?>) rddEnvelope.getData()).getBaseData();

assertEquals("HTTP GET", rdd.getName());
assertEquals("GET /", rdd.getName());
assertEquals("https://www.bing.com", rdd.getData());
assertTrue(rdd.getProperties().isEmpty());
assertTrue(rdd.getSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void doMostBasicTest() throws Exception {
Envelope rdEnvelope2 = getRequestEnvelope(rdList, "mytopic process");
Envelope rddEnvelope1 = getDependencyEnvelope(rddList, "HelloController.sendMessage");
Envelope rddEnvelope2 = getDependencyEnvelope(rddList, "mytopic send");
Envelope rddEnvelope3 = getDependencyEnvelope(rddList, "HTTP GET");
Envelope rddEnvelope3 = getDependencyEnvelope(rddList, "GET /");

RequestData rd1 = (RequestData) ((Data<?>) rdEnvelope1.getData()).getBaseData();
RequestData rd2 = (RequestData) ((Data<?>) rdEnvelope2.getData()).getBaseData();
Expand Down Expand Up @@ -101,7 +101,7 @@ public void doMostBasicTest() throws Exception {
assertTrue(rd2.getSuccess());
assertTrue(rd2.getMeasurements().containsKey("timeSinceEnqueued"));

assertEquals("HTTP GET", rdd3.getName());
assertEquals("GET /", rdd3.getName());
assertEquals("https://www.bing.com", rdd3.getData());
assertEquals("Http", rdd3.getType());
assertEquals("www.bing.com", rdd3.getTarget());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public void spawnAnotherJavaProcess() throws Exception {
RemoteDependencyData rdd =
(RemoteDependencyData) ((Data<?>) rddEnvelope.getData()).getBaseData();

if (!rdd.getName().equals("HTTP GET")) {
if (!rdd.getName().equals("GET /search")) {
rddEnvelope = rddList.get(0);
rdd = (RemoteDependencyData) ((Data<?>) rddEnvelope.getData()).getBaseData();
}

assertTrue(rd.getProperties().isEmpty());
assertTrue(rd.getSuccess());

assertEquals("HTTP GET", rdd.getName());
assertEquals("GET /search", rdd.getName());
assertEquals("Http", rdd.getType());
assertEquals("www.bing.com", rdd.getTarget());
assertEquals("https://www.bing.com/search?q=test", rdd.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void testAsyncDependencyCall() throws Exception {
assertTrue(rd.getProperties().isEmpty());
assertTrue(rd.getSuccess());

assertEquals("HTTP GET", rdd1.getName());
assertEquals("GET /", rdd1.getName());
assertEquals("https://www.bing.com", rdd1.getData());
assertEquals("www.bing.com", rdd1.getTarget());
assertTrue(rdd1.getProperties().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void testAsyncDependencyCall() throws Exception {
assertTrue(rdd1.getProperties().isEmpty());
assertTrue(rdd1.getSuccess());

assertEquals("HTTP GET", rdd2.getName());
assertEquals("GET /", rdd2.getName());
assertEquals("https://www.bing.com", rdd2.getData());
assertEquals("www.bing.com", rdd2.getTarget());
assertTrue(rdd2.getProperties().isEmpty());
Expand Down