From 66bffd1b1f145c3027f2f4d78be62d864eb948dc Mon Sep 17 00:00:00 2001 From: apasternak Date: Fri, 1 Mar 2024 12:28:52 +0100 Subject: [PATCH] Resolve UNKNOWN URI instead of root for URLs that don't match any patterns. --- .../instrument/binder/jersey/server/JerseyTags.java | 10 ++++++++-- .../jersey/server/DefaultJerseyTagsProviderTest.java | 12 ++++++------ .../server/MetricsRequestEventListenerTest.java | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/JerseyTags.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/JerseyTags.java index 1dc06c0ba..b3e6f562f 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/JerseyTags.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/JerseyTags.java @@ -43,6 +43,8 @@ public final class JerseyTags { private static final Tag URI_ROOT = Tag.of("uri", "root"); + private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN"); + private static final Tag EXCEPTION_NONE = Tag.of("exception", "None"); private static final Tag STATUS_SERVER_ERROR = Tag.of("status", "500"); @@ -95,7 +97,9 @@ public static Tag uri(RequestEvent event) { } } String matchingPattern = getMatchingPattern(event); - if (matchingPattern.equals("/")) { + if (matchingPattern == null) { + return URI_UNKNOWN; + } else if (matchingPattern.equals("/")) { return URI_ROOT; } return Tag.of("uri", matchingPattern); @@ -108,7 +112,9 @@ private static boolean isRedirection(int status) { private static String getMatchingPattern(RequestEvent event) { ExtendedUriInfo uriInfo = event.getUriInfo(); List templates = uriInfo.getMatchedTemplates(); - + if (templates.isEmpty()) { + return null; + } StringBuilder sb = new StringBuilder(); sb.append(uriInfo.getBaseUri().getPath()); for (int i = templates.size() - 1; i >= 0; i--) { diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/DefaultJerseyTagsProviderTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/DefaultJerseyTagsProviderTest.java index 5b6a4349e..a4fbafd1a 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/DefaultJerseyTagsProviderTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/DefaultJerseyTagsProviderTest.java @@ -50,7 +50,7 @@ class DefaultJerseyTagsProviderTest { @Test void testRootPath() { - assertThat(tagsProvider.httpRequestTags(event(200, null, "/", (String[]) null))) + assertThat(tagsProvider.httpRequestTags(event(200, null, "/", "/"))) .containsExactlyInAnyOrder(tagsFrom("root", 200, null, "SUCCESS")); } @@ -85,21 +85,21 @@ void redirectsAreShunted() { @Test @SuppressWarnings("serial") void exceptionsAreMappedCorrectly() { - assertThat(tagsProvider.httpRequestTags(event(500, new IllegalArgumentException(), "/app", (String[]) null))) + assertThat(tagsProvider.httpRequestTags(event(500, new IllegalArgumentException(), "/app", "/"))) .containsExactlyInAnyOrder(tagsFrom("/app", 500, "IllegalArgumentException", "SERVER_ERROR")); assertThat(tagsProvider.httpRequestTags( - event(500, new IllegalArgumentException(new NullPointerException()), "/app", (String[]) null))) + event(500, new IllegalArgumentException(new NullPointerException()), "/app", "/"))) .containsExactlyInAnyOrder(tagsFrom("/app", 500, "NullPointerException", "SERVER_ERROR")); - assertThat(tagsProvider.httpRequestTags(event(406, new NotAcceptableException(), "/app", (String[]) null))) + assertThat(tagsProvider.httpRequestTags(event(406, new NotAcceptableException(), "/app", "/"))) .containsExactlyInAnyOrder(tagsFrom("/app", 406, "NotAcceptableException", "CLIENT_ERROR")); assertThat(tagsProvider.httpRequestTags(event(500, new Exception("anonymous") { - }, "/app", (String[]) null))).containsExactlyInAnyOrder(tagsFrom("/app", 500, + }, "/app", "/"))).containsExactlyInAnyOrder(tagsFrom("/app", 500, "io.micrometer.core.instrument.binder.jersey.server.DefaultJerseyTagsProviderTest$1", "SERVER_ERROR")); } @Test void longRequestTags() { - assertThat(tagsProvider.httpLongRequestTags(event(0, null, "/app", (String[]) null))) + assertThat(tagsProvider.httpLongRequestTags(event(0, null, "/app", "/"))) .containsExactlyInAnyOrder(Tag.of("method", "GET"), Tag.of("uri", "/app")); } diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTest.java index 0a28e0865..05d99033e 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTest.java @@ -171,7 +171,7 @@ void exceptionsAreMappedCorrectly() { .count()).isEqualTo(1); assertThat(registry.get(METRIC_NAME) - .tags(tagsFrom("root", "406", "CLIENT_ERROR", "NotAcceptableException")) + .tags(tagsFrom("UNKNOWN", "406", "CLIENT_ERROR", "NotAcceptableException")) .timer() .count()).isEqualTo(1); }