Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing metrics for Jersey in cases of client errors #4326

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 @@ 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 +113,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 @@ -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:
Expand Down Expand Up @@ -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<Timer> shortTimers(Set<Timed> timed, RequestEvent event) {
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)))
assertThat(tagsProvider
.httpRequestTags(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 @@ -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;

Expand Down Expand Up @@ -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"))
Expand All @@ -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("UNKNOWN", "406", "CLIENT_ERROR", "NotAcceptableException"))
.timer()
.count()).isEqualTo(1);
}

private static Iterable<Tag> tagsFrom(String uri, String status, String outcome, String exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down