Skip to content
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 @@ -438,7 +438,7 @@ public static boolean isValidUrl(String url) {
}

public static URL getNormalizedUrl(String url) throws MalformedURLException, URISyntaxException {
String sanitizedUrl = StringUtils.stripStart(url, SLASH);
String sanitizedUrl = removeInitialSlashes(url);
URL absoluteUrl = new URL(new URL(RELATIVE_URL_CONTEXT), sanitizedUrl);
return absoluteUrl.toURI().normalize().toURL();
}
Expand Down Expand Up @@ -510,7 +510,7 @@ public static Optional<String> getHttpPath(Event event) {
if (StringUtils.isBlank(pathVal)) {
pathVal = SLASH;
}
return Optional.of(removeTrailingSlash(pathVal));
return Optional.of(removeTrailingSlashes(pathVal));
} catch (MalformedURLException | URISyntaxException e) {
LOGGER.debug(
"On extracting httpPath, received an invalid URL: {}, {}", url.get(), e.getMessage());
Expand All @@ -530,7 +530,7 @@ private static Optional<String> getHttpPathFromRawAttributes(Event event) {
if (attributeValueMap.get(path) != null) {
String s = attributeValueMap.get(path).getValue();
if (StringUtils.isNotBlank(s) && s.startsWith(SLASH)) {
return getPathFromUrlObject(s).map(HttpSemanticConventionUtils::removeTrailingSlash);
return getPathFromUrlObject(s).map(HttpSemanticConventionUtils::removeTrailingSlashes);
}
}
}
Expand Down Expand Up @@ -897,9 +897,22 @@ static Optional<String> getPathFromUrlObject(String urlPath) {
return Optional.empty();
}

private static String removeTrailingSlash(String s) {
// Ends with "/" and it's not home page path
return s.endsWith(SLASH) && s.length() > 1 ? s.substring(0, s.length() - 1) : s;
private static String removeTrailingSlashes(String url) {
// if it's home page path, then return /
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// if it's home page path, then return /
// if it's root URL, then return /

String updatedUrl = StringUtils.stripEnd(url, SLASH);
if (updatedUrl.isEmpty()) {
return SLASH;
}
return updatedUrl;
}

private static String removeInitialSlashes(String url) {
// if it's home page path, then return /
String updatedUrl = StringUtils.stripStart(url, SLASH);
if (updatedUrl.isEmpty()) {
return SLASH;
}
return updatedUrl;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,20 @@ public void testGetPrimaryDomain() {

Assertions.assertEquals(HttpSemanticConventionUtils.getPrimaryDomain("10.0.0.0"), "10.0.0.0");
}

@Test
void testGetPathFromUrlObject() {
Optional<String> path =
HttpSemanticConventionUtils.getPathFromUrlObject("http://app.test.com:9191/");
Assertions.assertTrue(path.isPresent());
Assertions.assertEquals("/", path.get());

path = HttpSemanticConventionUtils.getPathFromUrlObject("http://app.test.com:9191//");
Assertions.assertTrue(path.isPresent());
Assertions.assertEquals("/", path.get());

path = HttpSemanticConventionUtils.getPathFromUrlObject("http://app.test.com:9191//abc/def");
Assertions.assertTrue(path.isPresent());
Assertions.assertEquals("/abc/def", path.get());
}
}