Skip to content

Commit ee2a6df

Browse files
author
Samin Rahman
committed
Added optimized filter path for HTTPPathMetricFilter cases (like operator) without need for expensive path parameter resolution
1 parent daee856 commit ee2a6df

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

src/main/java/com/uid2/shared/util/HTTPPathMetricFilter.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
public class HTTPPathMetricFilter {
77
public static String filterPath(String actualPath, Set<String> pathSet) {
88
try {
9-
String normalized = HttpUtils.normalizePath(actualPath).split("\\?")[0];
9+
String normalized = HttpUtils.normalizePath(actualPath);
10+
/* Optimization 1: Split that avoids array and regex initialization */
11+
int splitIndex = normalized.indexOf('?');
12+
if (splitIndex != -1) {
13+
normalized = normalized.substring(0, splitIndex);
14+
}
15+
1016
if (normalized.charAt(normalized.length() - 1) == '/') {
1117
normalized = normalized.substring(0, normalized.length() - 1);
1218
}
@@ -25,4 +31,29 @@ public static String filterPath(String actualPath, Set<String> pathSet) {
2531
return "/parsing_error";
2632
}
2733
}
34+
35+
public static String filterPathWithoutPathParameters(String actualPath, Set<String> pathSet) {
36+
try {
37+
String normalized = HttpUtils.normalizePath(actualPath);
38+
/* Optimization 1: Split that avoids array and regex initialization */
39+
int splitIndex = normalized.indexOf('?');
40+
if (splitIndex != -1) {
41+
normalized = normalized.substring(0, splitIndex);
42+
}
43+
44+
if (normalized.charAt(normalized.length() - 1) == '/') {
45+
normalized = normalized.substring(0, normalized.length() - 1);
46+
}
47+
normalized = normalized.toLowerCase();
48+
49+
if (pathSet == null || pathSet.isEmpty()) { return normalized; }
50+
51+
/* Optimization 2: Remove for loop and regex matching */
52+
if (pathSet.contains(normalized)) { return normalized; }
53+
54+
return "/unknown";
55+
} catch (IllegalArgumentException e) {
56+
return "/parsing_error";
57+
}
58+
}
2859
}

src/test/java/com/uid2/shared/util/HTTPPathMetricFilterTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import static org.junit.jupiter.api.Assertions.assertEquals;
1010

11-
public class HTTPPathMetricFilterTest {
11+
public class HTTPPathMetricFilterTest {
12+
/* filterPathTests */
1213
final Set<String> pathSet = Set.of("/v1/identity/map", "/token/refresh", "/list", "/list/:siteId/:keyId");
1314

1415
@ParameterizedTest
@@ -54,4 +55,49 @@ void testPathFiltering_ValidPaths_KnownEndpoints(String actualPath, String expec
5455
String filteredPath = HTTPPathMetricFilter.filterPath(actualPath, pathSet);
5556
assertEquals(expectedFilteredPath, filteredPath);
5657
}
58+
59+
/* filterPathWithoutPathParameters tests */
60+
final Set<String> pathSetWithoutParams = Set.of("/v1/identity/map", "/token/refresh", "/list");
61+
62+
@ParameterizedTest
63+
@ValueSource(strings = {
64+
"",
65+
"/",
66+
"/unknown-path",
67+
"../",
68+
"/v1/identity/map%55",
69+
"/list/123",
70+
})
71+
void testPathFilteringWithoutPathParameters_InvalidPaths_Unknown(String actualPath) {
72+
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
73+
assertEquals("/unknown", filteredPath);
74+
}
75+
76+
@ParameterizedTest
77+
@ValueSource(strings = {
78+
"v1/identity/map?id=bad-escape-code%2",
79+
"token/refresh?refresh_token=SOME_TOKEN<%=7485*4353%>",
80+
"list/12%4/5435"
81+
})
82+
void testPathFilteringWithoutPathParameters_InvalidPaths_ParsingError(String actualPath) {
83+
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
84+
assertEquals("/parsing_error", filteredPath);
85+
}
86+
87+
@ParameterizedTest
88+
@CsvSource(value = {
89+
"/v1/identity/map, /v1/identity/map",
90+
"v1/identity/map, /v1/identity/map",
91+
"V1/IdenTity/mAp, /v1/identity/map",
92+
"./v1//identity//map/, /v1/identity/map",
93+
"../v1/identity/./map, /v1/identity/map",
94+
"/v1/identity/new/path/../../map, /v1/identity/map",
95+
"token/refresh?refresh_token=123%20%23, /token/refresh",
96+
"v1/identity/map?identity/../map/, /v1/identity/map",
97+
"/list, /list"
98+
})
99+
void testPathFilteringWithoutPathParameters_ValidPaths_KnownEndpoints(String actualPath, String expectedFilteredPath) {
100+
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
101+
assertEquals(expectedFilteredPath, filteredPath);
102+
}
57103
}

0 commit comments

Comments
 (0)