fix span error tagging in grpc server interceptor

This commit is contained in:
Marco Ferrer 2019-09-06 12:25:31 -04:00
parent df017e044a
commit 6ac07abe2b
2 changed files with 38 additions and 0 deletions

View File

@ -2,6 +2,9 @@ package datadog.trace.instrumentation.grpc.server;
import datadog.trace.agent.decorator.ServerDecorator;
import datadog.trace.api.DDSpanTypes;
import io.grpc.Status;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
public class GrpcServerDecorator extends ServerDecorator {
public static final GrpcServerDecorator DECORATE = new GrpcServerDecorator();
@ -20,4 +23,17 @@ public class GrpcServerDecorator extends ServerDecorator {
protected String component() {
return "grpc-server";
}
public Span onClose(final Span span, final Status status) {
span.setTag("status.code", status.getCode().name());
span.setTag("status.description", status.getDescription());
onError(span, status.getCause());
if (!status.isOk()) {
Tags.ERROR.set(span, true);
}
return span;
}
}

View File

@ -90,6 +90,27 @@ public class TracingServerInterceptor implements ServerInterceptor {
this.span = span;
}
@Override
public void onClose(final Status status, final Metadata trailers) {
DECORATE.onClose(span, status);
// Finishes span.
try (final Scope scope = tracer.scopeManager().activate(span, false)) {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
delegate().onClose(status, trailers);
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(false);
}
} catch (final Throwable e) {
DECORATE.onError(span, e);
throw e;
} finally {
DECORATE.beforeFinish(span);
span.finish();
}
}
@Override
public void onMessage(final ReqT message) {
final Scope scope =
@ -178,6 +199,7 @@ public class TracingServerInterceptor implements ServerInterceptor {
}
}
@Override
public void onReady() {
try (final Scope scope = tracer.scopeManager().activate(span, false)) {