Skip to content

Commit

Permalink
enforce http.error to be present even in synchronouse mode in tracing…
Browse files Browse the repository at this point in the history
… valve
  • Loading branch information
rmannibucau committed May 22, 2023
1 parent aff5683 commit 45700f6
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.time.Clock;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

Expand Down Expand Up @@ -105,6 +106,9 @@ public void invoke(final Request request, final Response response) throws IOExce
request.setAttribute(Span.class.getName(), span);
try {
getNext().invoke(request, response);
} catch (final RuntimeException | IOException | ServletException re) {
addErrorTag(tags, re);
throw re;
} finally {
if (request.isAsyncStarted()) {
request.getAsyncContext().addListener(new AsyncListener() {
Expand All @@ -126,9 +130,7 @@ public void onTimeout(final AsyncEvent event) {

@Override
public void onError(final AsyncEvent event) {
tags.putIfAbsent("http.error", event.getThrowable() == null ?
"unknown" :
(event.getThrowable().getMessage() == null ? event.getThrowable().getClass().getName() : event.getThrowable().getMessage()));
addErrorTag(tags, event.getThrowable());
status(event);
}

Expand All @@ -144,6 +146,12 @@ public void onStartAsync(final AsyncEvent event) {
}
}

protected void addErrorTag(final Map<String, Object> tags, final Throwable throwable) {
tags.putIfAbsent("http.error", throwable == null ?
"unknown" :
(throwable.getMessage() == null ? throwable.getClass().getName() : throwable.getMessage()));
}

protected void finish(final HttpServletResponse response, final Span span, final Instant start) {
final var end = clock.instant();
span.getTags().putIfAbsent("http.status", response.getStatus());
Expand Down

0 comments on commit 45700f6

Please sign in to comment.