Skip to content

Commit f2b9ccb

Browse files
committed
Created a more optimized version of HTTPPathMetricFilter
1 parent 4e733bf commit f2b9ccb

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

src/main/java/com/uid2/operator/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.uid2.operator.service.*;
1313
import com.uid2.operator.store.*;
1414
import com.uid2.operator.store.BootstrapConfigStore;
15+
import com.uid2.operator.util.HTTPPathMetricFilterOptimized;
1516
import com.uid2.operator.vertx.Endpoints;
1617
import com.uid2.operator.vertx.OperatorShutdownHandler;
1718
import com.uid2.operator.vertx.UIDOperatorVerticle;
@@ -517,7 +518,7 @@ private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
517518
// providing common renaming for prometheus metric, e.g. "hello.world" to "hello_world"
518519
.meterFilter(new PrometheusRenameFilter())
519520
.meterFilter(MeterFilter.replaceTagValues(Label.HTTP_PATH.toString(),
520-
actualPath -> HTTPPathMetricFilter.filterPath(actualPath, Endpoints.pathSet())))
521+
actualPath -> HTTPPathMetricFilterOptimized.filterPath(actualPath, Endpoints.pathSet())))
521522
// Don't record metrics for 404s.
522523
.meterFilter(MeterFilter.deny(id ->
523524
id.getName().startsWith(MetricsDomain.HTTP_SERVER.getPrefix()) &&
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.uid2.operator.util;
2+
3+
import io.vertx.core.http.impl.HttpUtils;
4+
5+
import java.util.Set;
6+
7+
public class HTTPPathMetricFilterOptimized {
8+
public static String filterPath(String actualPath, Set<String> pathSet) {
9+
try {
10+
String normalized = HttpUtils.normalizePath(actualPath);
11+
/* Optimization 1: Split that avoids array and regex initialization */
12+
int splitIndex = normalized.indexOf('?');
13+
if (splitIndex != -1) {
14+
normalized = normalized.substring(0, splitIndex);
15+
}
16+
17+
if (normalized.charAt(normalized.length() - 1) == '/') {
18+
normalized = normalized.substring(0, normalized.length() - 1);
19+
}
20+
normalized = normalized.toLowerCase();
21+
22+
if (pathSet == null || pathSet.isEmpty()) { return normalized; }
23+
24+
/* Optimization 2: Remove for loop and regex matching */
25+
if (pathSet.contains(normalized)) { return normalized; }
26+
27+
return "/unknown";
28+
} catch (IllegalArgumentException e) {
29+
return "/parsing_error";
30+
}
31+
}
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.uid2.operator.util;
2+
3+
import com.uid2.operator.vertx.Endpoints;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
import org.junit.jupiter.params.provider.ValueSource;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class HTTPPathMetricFilterOptimizedTest {
11+
@ParameterizedTest
12+
@ValueSource(strings = {
13+
"",
14+
"/",
15+
"/unknown-path",
16+
"../",
17+
"/v1/identity/map%55",
18+
"/list/123",
19+
})
20+
void testPathFiltering_InvalidPaths_Unknown(String actualPath) {
21+
String filteredPath = HTTPPathMetricFilterOptimized.filterPath(actualPath, Endpoints.pathSet());
22+
assertEquals("/unknown", filteredPath);
23+
}
24+
25+
@ParameterizedTest
26+
@ValueSource(strings = {
27+
"v1/identity/map?id=bad-escape-code%2",
28+
"token/refresh?refresh_token=SOME_TOKEN<%=7485*4353%>",
29+
"list/12%4/5435"
30+
})
31+
void testPathFiltering_InvalidPaths_ParsingError(String actualPath) {
32+
String filteredPath = HTTPPathMetricFilterOptimized.filterPath(actualPath, Endpoints.pathSet());
33+
assertEquals("/parsing_error", filteredPath);
34+
}
35+
36+
@ParameterizedTest
37+
@CsvSource(value = {
38+
"/v2/identity/map, /v2/identity/map",
39+
"v2/identity/map, /v2/identity/map",
40+
"V3/IdenTity/mAp, /v3/identity/map",
41+
"v2/token/refresh?refresh_token=123%20%23, /v2/token/refresh",
42+
"v2/identity/map?identity/../map/, /v2/identity/map"
43+
})
44+
void testPathFiltering_ValidPaths_KnownEndpoints(String actualPath, String expectedFilteredPath) {
45+
String filteredPath = HTTPPathMetricFilterOptimized.filterPath(actualPath, Endpoints.pathSet());
46+
assertEquals(expectedFilteredPath, filteredPath);
47+
}
48+
}

0 commit comments

Comments
 (0)