Skip to content

Commit 287a6d8

Browse files
ahus1gsmet
authored andcommitted
Keep the URIs in the metrics tag if they match a client or server pattern
Closes quarkusio#39581 Co-authored-by: Erin Schnabel <[email protected]> (cherry picked from commit 2b0b1f2)
1 parent 10e49ce commit 287a6d8

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/HttpCommonTags.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkus.micrometer.runtime.binder;
22

3+
import java.util.Objects;
4+
35
import io.micrometer.core.instrument.Tag;
46
import io.micrometer.core.instrument.binder.http.Outcome;
57

@@ -46,30 +48,47 @@ public static Tag outcome(int statusCode) {
4648

4749
/**
4850
* Creates a {@code uri} tag based on the URI of the given {@code request}.
49-
* Falling back to {@code REDIRECTION} for 3xx responses, {@code NOT_FOUND}
50-
* for 404 responses, {@code root} for requests with no path info, and {@code UNKNOWN}
51+
* Falling back to {@code REDIRECTION} for 3xx responses if there wasn't a matched path pattern, {@code NOT_FOUND}
52+
* for 404 responses if there wasn't a matched path pattern, {@code root} for requests with no path info, and
53+
* {@code UNKNOWN}
5154
* for all other requests.
5255
*
5356
* @param pathInfo request path
57+
* @param initialPath initial path before request pattern matching took place. Pass in null if there is pattern matching
58+
* done in the caller.
5459
* @param code status code of the response
5560
* @return the uri tag derived from the request
5661
*/
57-
public static Tag uri(String pathInfo, int code) {
58-
if (code > 0) {
59-
if (code / 100 == 3) {
60-
return URI_REDIRECTION;
61-
} else if (code == 404) {
62-
return URI_NOT_FOUND;
63-
}
64-
}
62+
public static Tag uri(String pathInfo, String initialPath, int code) {
6563
if (pathInfo == null) {
6664
return URI_UNKNOWN;
6765
}
6866
if (pathInfo.isEmpty() || "/".equals(pathInfo)) {
6967
return URI_ROOT;
7068
}
7169

70+
if (code > 0) {
71+
if (code / 100 == 3) {
72+
if (isTemplatedPath(pathInfo, initialPath)) {
73+
return Tag.of("uri", pathInfo);
74+
} else {
75+
return URI_REDIRECTION;
76+
}
77+
} else if (code == 404) {
78+
if (isTemplatedPath(pathInfo, initialPath)) {
79+
return Tag.of("uri", pathInfo);
80+
} else {
81+
return URI_NOT_FOUND;
82+
}
83+
}
84+
}
85+
7286
// Use first segment of request path
7387
return Tag.of("uri", pathInfo);
7488
}
89+
90+
private static boolean isTemplatedPath(String pathInfo, String initialPath) {
91+
// only include the path info if it has been matched to a template (initialPath != pathInfo) to avoid a metrics explosion with lots of entries
92+
return initialPath != null && !Objects.equals(initialPath, pathInfo);
93+
}
7594
}

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/RestClientMetricsFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void filter(final ClientRequestContext requestContext, final ClientRespon
7272
Timer.Builder builder = Timer.builder(httpMetricsConfig.getHttpClientRequestsName())
7373
.tags(Tags.of(
7474
HttpCommonTags.method(requestContext.getMethod()),
75-
HttpCommonTags.uri(requestPath, statusCode),
75+
HttpCommonTags.uri(requestPath, requestContext.getUri().getPath(), statusCode),
7676
HttpCommonTags.outcome(statusCode),
7777
HttpCommonTags.status(statusCode),
7878
clientName(requestContext)));

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/vertx/VertxHttpClientMetrics.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static class RequestTracker extends RequestMetricInfo {
183183
this.tags = origin.and(
184184
Tag.of("address", address),
185185
HttpCommonTags.method(method),
186-
HttpCommonTags.uri(path, -1));
186+
HttpCommonTags.uri(path, null, -1));
187187
}
188188

189189
void requestReset() {

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/vertx/VertxHttpServerMetrics.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public HttpRequestMetric responsePushed(LongTaskTimer.Sample socketMetric, HttpM
9999
config.getServerIgnorePatterns());
100100
if (path != null) {
101101
registry.counter(nameHttpServerPush, Tags.of(
102-
HttpCommonTags.uri(path, response.statusCode()),
102+
HttpCommonTags.uri(path, requestMetric.initialPath, response.statusCode()),
103103
VertxMetricsTags.method(method),
104104
VertxMetricsTags.outcome(response),
105105
HttpCommonTags.status(response.statusCode())))
@@ -153,7 +153,7 @@ public void requestReset(HttpRequestMetric requestMetric) {
153153
Timer.Builder builder = Timer.builder(nameHttpServerRequests)
154154
.tags(Tags.of(
155155
VertxMetricsTags.method(requestMetric.request().method()),
156-
HttpCommonTags.uri(path, 0),
156+
HttpCommonTags.uri(path, requestMetric.initialPath, 0),
157157
Outcome.CLIENT_ERROR.asTag(),
158158
HttpCommonTags.STATUS_RESET));
159159

@@ -180,7 +180,7 @@ public void responseEnd(HttpRequestMetric requestMetric, HttpResponse response,
180180
Timer.Sample sample = requestMetric.getSample();
181181
Tags allTags = Tags.of(
182182
VertxMetricsTags.method(requestMetric.request().method()),
183-
HttpCommonTags.uri(path, response.statusCode()),
183+
HttpCommonTags.uri(path, requestMetric.initialPath, response.statusCode()),
184184
VertxMetricsTags.outcome(response),
185185
HttpCommonTags.status(response.statusCode()));
186186
if (!httpServerMetricsTagsContributors.isEmpty()) {
@@ -217,7 +217,7 @@ public LongTaskTimer.Sample connected(LongTaskTimer.Sample sample, HttpRequestMe
217217
config.getServerIgnorePatterns());
218218
if (path != null) {
219219
return LongTaskTimer.builder(nameWebsocketConnections)
220-
.tags(Tags.of(HttpCommonTags.uri(path, 0)))
220+
.tags(Tags.of(HttpCommonTags.uri(path, requestMetric.initialPath, 0)))
221221
.register(registry)
222222
.start();
223223
}

extensions/micrometer/runtime/src/test/java/io/quarkus/micrometer/runtime/binder/HttpCommonTagsTest.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ public void testStatus() {
2121

2222
@Test
2323
public void testUriRedirect() {
24-
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", 301));
25-
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", 302));
26-
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", 304));
24+
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", null, 301));
25+
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", null, 302));
26+
Assertions.assertEquals(HttpCommonTags.URI_REDIRECTION, HttpCommonTags.uri("/moved", null, 304));
27+
Assertions.assertEquals(Tag.of("uri", "/moved/{id}"), HttpCommonTags.uri("/moved/{id}", "/moved/111", 304));
28+
Assertions.assertEquals(HttpCommonTags.URI_ROOT, HttpCommonTags.uri("/", null, 304));
2729
}
2830

2931
@Test
3032
public void testUriDefaults() {
31-
Assertions.assertEquals(HttpCommonTags.URI_ROOT, HttpCommonTags.uri("/", 200));
32-
Assertions.assertEquals(Tag.of("uri", "/known/ok"), HttpCommonTags.uri("/known/ok", 200));
33-
Assertions.assertEquals(HttpCommonTags.URI_NOT_FOUND, HttpCommonTags.uri("/invalid", 404));
34-
Assertions.assertEquals(Tag.of("uri", "/known/bad/request"), HttpCommonTags.uri("/known/bad/request", 400));
35-
Assertions.assertEquals(Tag.of("uri", "/known/server/error"), HttpCommonTags.uri("/known/server/error", 500));
33+
Assertions.assertEquals(HttpCommonTags.URI_ROOT, HttpCommonTags.uri("/", null, 200));
34+
Assertions.assertEquals(HttpCommonTags.URI_ROOT, HttpCommonTags.uri("/", null, 404));
35+
Assertions.assertEquals(Tag.of("uri", "/known/ok"), HttpCommonTags.uri("/known/ok", null, 200));
36+
Assertions.assertEquals(HttpCommonTags.URI_NOT_FOUND, HttpCommonTags.uri("/invalid", null, 404));
37+
Assertions.assertEquals(Tag.of("uri", "/invalid/{id}"), HttpCommonTags.uri("/invalid/{id}", "/invalid/111", 404));
38+
Assertions.assertEquals(Tag.of("uri", "/known/bad/request"), HttpCommonTags.uri("/known/bad/request", null, 400));
39+
Assertions.assertEquals(Tag.of("uri", "/known/server/error"), HttpCommonTags.uri("/known/server/error", null, 500));
3640
}
3741
}

0 commit comments

Comments
 (0)