Skip to content

Commit

Permalink
Resolve UNKNOWN URI instead of root for URLs that don't match any pat…
Browse files Browse the repository at this point in the history
…terns.
  • Loading branch information
adpaste committed Mar 1, 2024
1 parent c4ed2dd commit 66bffd1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -108,7 +112,9 @@ private static boolean isRedirection(int status) {
private static String getMatchingPattern(RequestEvent event) {
ExtendedUriInfo uriInfo = event.getUriInfo();
List<UriTemplate> 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--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 66bffd1

Please sign in to comment.