Skip to content
Permalink
Browse files
GERONIMO-6657 avoid to loose error in async mode
  • Loading branch information
rmannibucau committed Nov 22, 2018
1 parent ac2bb11 commit 466bae2e367cf1f97885f8df3d1f700b222473df
Showing 1 changed file with 21 additions and 15 deletions.
@@ -12,6 +12,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@@ -155,19 +156,7 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
try {
chain.doFilter(request, response);
} catch (final Exception ex) {
getCurrentScope(request).ifPresent(scope -> {
final int status = HttpServletResponse.class.cast(response).getStatus();
final Span span = scope.span();
Tags.HTTP_STATUS.set(span,
status == HttpServletResponse.SC_OK ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : status);
if (forceStackLog) {
Tags.ERROR.set(span, true);
final Map<String, Object> logs = new LinkedHashMap<>();
logs.put("event", Tags.ERROR.getKey());
logs.put("error.object", ex);
span.log(logs);
}
});
getCurrentScope(request).ifPresent(scope -> onError(response, ex, scope));
throw ex;
} finally {
getCurrentScope(request).ifPresent(scope -> {
@@ -181,12 +170,15 @@ public void onComplete(final AsyncEvent event) {

@Override
public void onTimeout(final AsyncEvent event) {
// no-op
OpenTracingFilter.this.onError(
event.getSuppliedResponse(),
ofNullable(event.getThrowable()).orElseGet(TimeoutException::new),
scope);
}

@Override
public void onError(final AsyncEvent event) {
// no-op
OpenTracingFilter.this.onError(event.getSuppliedResponse(), event.getThrowable(), scope);
}

@Override
@@ -212,6 +204,20 @@ public void onStartAsync(final AsyncEvent event) {
}
}

private void onError(final ServletResponse response, final Throwable ex, final Scope scope) {
final int status = HttpServletResponse.class.cast(response).getStatus();
final Span span = scope.span();
Tags.HTTP_STATUS.set(span,
status == HttpServletResponse.SC_OK ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : status);
if (forceStackLog) {
Tags.ERROR.set(span, true);
final Map<String, Object> logs = new LinkedHashMap<>();
logs.put("event", Tags.ERROR.getKey());
logs.put("error.object", ex);
span.log(logs);
}
}

private Optional<Scope> getCurrentScope(final ServletRequest request) {
return ofNullable(ofNullable(request.getAttribute(OpenTracingFilter.class.getName()))
.orElseGet(() -> tracer.scopeManager().active())).map(Scope.class::cast);

0 comments on commit 466bae2

Please sign in to comment.