diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListener.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListener.java index 6652b8caa4..70fa344992 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListener.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListener.java @@ -73,7 +73,7 @@ public void onEvent(RequestEvent event) { switch (event.getType()) { case ON_EXCEPTION: - if (!isNotFoundException(event)) { + if (!isClientError(event)) { break; } case REQUEST_MATCHED: @@ -109,13 +109,14 @@ public void onEvent(RequestEvent event) { } } - private boolean isNotFoundException(RequestEvent event) { + private boolean isClientError(RequestEvent event) { Throwable t = event.getException(); if (t == null) { return false; } - String className = t.getClass().getCanonicalName(); - return className.equals("jakarta.ws.rs.NotFoundException") || className.equals("javax.ws.rs.NotFoundException"); + String className = t.getClass().getSuperclass().getCanonicalName(); + return className.equals("jakarta.ws.rs.ClientErrorException") + || className.equals("javax.ws.rs.ClientErrorException"); } private Set shortTimers(Set timed, RequestEvent event) { 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 44243c6e8c..0a28e08659 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 @@ -27,6 +27,7 @@ import javax.ws.rs.NotFoundException; import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; import java.util.logging.Level; import java.util.logging.Logger; @@ -148,6 +149,11 @@ void exceptionsAreMappedCorrectly() { } catch (Exception ignored) { } + try { + target("produces-text-plain").request(MediaType.APPLICATION_JSON).get(); + } + catch (Exception ignored) { + } assertThat(registry.get(METRIC_NAME) .tags(tagsFrom("/throws-exception", "500", "SERVER_ERROR", "IllegalArgumentException")) @@ -163,6 +169,11 @@ void exceptionsAreMappedCorrectly() { .tags(tagsFrom("/throws-mappable-exception", "410", "CLIENT_ERROR", "ResourceGoneException")) .timer() .count()).isEqualTo(1); + + assertThat(registry.get(METRIC_NAME) + .tags(tagsFrom("root", "406", "CLIENT_ERROR", "NotAcceptableException")) + .timer() + .count()).isEqualTo(1); } private static Iterable tagsFrom(String uri, String status, String outcome, String exception) { diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/resources/TestResource.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/resources/TestResource.java index bb6e299a73..19ef41a156 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/resources/TestResource.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/resources/TestResource.java @@ -82,6 +82,13 @@ public String throwsMappableException() { throw new ResourceGoneException("Resource has been permanently removed."); } + @GET + @Path("produces-text-plain") + @Produces(MediaType.TEXT_PLAIN) + public String producesTextPlain() { + return "hello"; + } + @GET @Path("redirect/{status}") public Response redirect(@PathParam("status") int status) {