Skip to content

Commit 854305b

Browse files
committed
Add missing metrics for cases of client errors
Changes ported from micrometer-metrics: micrometer-metrics/micrometer#4326
1 parent 9c5b688 commit 854305b

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

ext/micrometer/src/main/java/org/glassfish/jersey/micrometer/server/JerseyTags.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -43,6 +43,8 @@ public final class JerseyTags {
4343

4444
private static final Tag URI_ROOT = Tag.of("uri", "root");
4545

46+
private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");
47+
4648
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
4749

4850
private static final Tag STATUS_SERVER_ERROR = Tag.of("status", "500");
@@ -95,7 +97,10 @@ public static Tag uri(RequestEvent event) {
9597
}
9698
}
9799
String matchingPattern = getMatchingPattern(event);
98-
if (matchingPattern.equals("/")) {
100+
if (matchingPattern == null) {
101+
return URI_UNKNOWN;
102+
}
103+
else if (matchingPattern.equals("/")) {
99104
return URI_ROOT;
100105
}
101106
return Tag.of("uri", matchingPattern);
@@ -108,7 +113,9 @@ static boolean isRedirection(int status) {
108113
static String getMatchingPattern(RequestEvent event) {
109114
ExtendedUriInfo uriInfo = event.getUriInfo();
110115
List<UriTemplate> templates = uriInfo.getMatchedTemplates();
111-
116+
if (templates.isEmpty()) {
117+
return null;
118+
}
112119
StringBuilder sb = new StringBuilder();
113120
sb.append(uriInfo.getBaseUri().getPath());
114121
for (int i = templates.size() - 1; i >= 0; i--) {

ext/micrometer/src/main/java/org/glassfish/jersey/micrometer/server/MetricsRequestEventListener.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -79,7 +79,7 @@ public void onEvent(RequestEvent event) {
7979

8080
switch (event.getType()) {
8181
case ON_EXCEPTION:
82-
if (!isNotFoundException(event)) {
82+
if (!isClientError(event)) {
8383
break;
8484
}
8585
time(event, containerRequest);
@@ -122,13 +122,14 @@ private void time(RequestEvent event, ContainerRequest containerRequest) {
122122
}
123123
}
124124

125-
private boolean isNotFoundException(RequestEvent event) {
125+
private boolean isClientError(RequestEvent event) {
126126
Throwable t = event.getException();
127127
if (t == null) {
128128
return false;
129129
}
130-
String className = t.getClass().getCanonicalName();
131-
return className.equals("jakarta.ws.rs.NotFoundException") || className.equals("javax.ws.rs.NotFoundException");
130+
String className = t.getClass().getSuperclass().getCanonicalName();
131+
return className.equals("jakarta.ws.rs.ClientErrorException")
132+
|| className.equals("javax.ws.rs.ClientErrorException");
132133
}
133134

134135
private Set<Timer> shortTimers(Set<Timed> timed, RequestEvent event) {

ext/micrometer/src/test/java/org/glassfish/jersey/micrometer/server/DefaultJerseyTagsProviderTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -51,7 +51,7 @@ class DefaultJerseyTagsProviderTest {
5151

5252
@Test
5353
void testRootPath() {
54-
assertThat(tagsProvider.httpRequestTags(event(200, null, "/", (String[]) null)))
54+
assertThat(tagsProvider.httpRequestTags(event(200, null, "/", "/")))
5555
.containsExactlyInAnyOrder(tagsFrom("root", 200, null, "SUCCESS"));
5656
}
5757

@@ -86,21 +86,21 @@ void redirectsAreShunted() {
8686
@Test
8787
@SuppressWarnings("serial")
8888
void exceptionsAreMappedCorrectly() {
89-
assertThat(tagsProvider.httpRequestTags(event(500, new IllegalArgumentException(), "/app", (String[]) null)))
89+
assertThat(tagsProvider.httpRequestTags(event(500, new IllegalArgumentException(), "/app", "/")))
9090
.containsExactlyInAnyOrder(tagsFrom("/app", 500, "IllegalArgumentException", "SERVER_ERROR"));
9191
assertThat(tagsProvider.httpRequestTags(
92-
event(500, new IllegalArgumentException(new NullPointerException()), "/app", (String[]) null)))
92+
event(500, new IllegalArgumentException(new NullPointerException()), "/app", "/")))
9393
.containsExactlyInAnyOrder(tagsFrom("/app", 500, "NullPointerException", "SERVER_ERROR"));
94-
assertThat(tagsProvider.httpRequestTags(event(406, new NotAcceptableException(), "/app", (String[]) null)))
94+
assertThat(tagsProvider.httpRequestTags(event(406, new NotAcceptableException(), "/app", "/")))
9595
.containsExactlyInAnyOrder(tagsFrom("/app", 406, "NotAcceptableException", "CLIENT_ERROR"));
9696
assertThat(tagsProvider.httpRequestTags(event(500, new Exception("anonymous") {
97-
}, "/app", (String[]) null))).containsExactlyInAnyOrder(tagsFrom("/app", 500,
97+
}, "/app", "/"))).containsExactlyInAnyOrder(tagsFrom("/app", 500,
9898
"org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProviderTest$1", "SERVER_ERROR"));
9999
}
100100

101101
@Test
102102
void longRequestTags() {
103-
assertThat(tagsProvider.httpLongRequestTags(event(0, null, "/app", (String[]) null)))
103+
assertThat(tagsProvider.httpLongRequestTags(event(0, null, "/app", "/")))
104104
.containsExactlyInAnyOrder(Tag.of("method", "GET"), Tag.of("uri", "/app"));
105105
}
106106

ext/micrometer/src/test/java/org/glassfish/jersey/micrometer/server/MetricsRequestEventListenerTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -20,6 +20,7 @@
2020

2121
import javax.ws.rs.NotFoundException;
2222
import javax.ws.rs.core.Application;
23+
import javax.ws.rs.core.MediaType;
2324

2425
import io.micrometer.core.instrument.MeterRegistry;
2526
import io.micrometer.core.instrument.Tag;
@@ -149,6 +150,11 @@ void exceptionsAreMappedCorrectly() {
149150
}
150151
catch (Exception ignored) {
151152
}
153+
try {
154+
target("produces-text-plain").request(MediaType.APPLICATION_JSON).get();
155+
}
156+
catch (Exception ignored) {
157+
}
152158

153159
assertThat(registry.get(METRIC_NAME)
154160
.tags(tagsFrom("/throws-exception", "500", "SERVER_ERROR", "IllegalArgumentException"))
@@ -164,6 +170,11 @@ void exceptionsAreMappedCorrectly() {
164170
.tags(tagsFrom("/throws-mappable-exception", "410", "CLIENT_ERROR", "ResourceGoneException"))
165171
.timer()
166172
.count()).isEqualTo(1);
173+
174+
assertThat(registry.get(METRIC_NAME)
175+
.tags(tagsFrom("UNKNOWN", "406", "CLIENT_ERROR", "NotAcceptableException"))
176+
.timer()
177+
.count()).isEqualTo(1);
167178
}
168179

169180
private static Iterable<Tag> tagsFrom(String uri, String status, String outcome, String exception) {

ext/micrometer/src/test/java/org/glassfish/jersey/micrometer/server/resources/TestResource.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -89,6 +89,13 @@ public String throwsMappableException() {
8989
throw new ResourceGoneException("Resource has been permanently removed.");
9090
}
9191

92+
@GET
93+
@Path("produces-text-plain")
94+
@Produces(MediaType.TEXT_PLAIN)
95+
public String producesTextPlain() {
96+
return "hello";
97+
}
98+
9299
@GET
93100
@Path("redirect/{status}")
94101
public Response redirect(@PathParam("status") int status) {

0 commit comments

Comments
 (0)