Skip to content

Commit

Permalink
Add missing metrics for cases of client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaste committed Jan 3, 2024
1 parent 9b56fe2 commit c4ed2dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
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 @@ -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("root", "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

0 comments on commit c4ed2dd

Please sign in to comment.