Make sure span is opened and closed with scope in spymemcached
@@ -2,6 +2,7 @@ package datadog.trace.instrumentation.spymemcached;
import static datadog.trace.instrumentation.spymemcached.MemcacheClientDecorator.DECORATE;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import java.util.concurrent.CancellationException;
@@ -29,19 +30,23 @@ public abstract class CompletionListener<T> {
public CompletionListener(final MemcachedConnection connection, final String methodName) {
this.connection = connection;
span = GlobalTracer.get().buildSpan(OPERATION_NAME).start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onConnection(span, connection);
DECORATE.onOperation(span, methodName);
}
+ }
protected void closeAsyncSpan(final T future) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try {
processResult(span, future);
} catch (final CancellationException e) {
span.setTag(DB_COMMAND_CANCELLED, true);
} catch (final ExecutionException e) {
if (e.getCause() instanceof CancellationException) {
- // Looks like underlying OperationFuture wraps CancellationException into ExecutionException
+ // Looks like underlying OperationFuture wraps CancellationException into
+ // ExecutionException
span.setTag(DB_COMMAND_CANCELLED, true);
} else {
DECORATE.onError(span, e.getCause());
@@ -55,14 +60,15 @@ public abstract class CompletionListener<T> {
DECORATE.onError(span, e);
} finally {
DECORATE.beforeFinish(span);
- span.finish();
+ }
}
}
protected void closeSyncSpan(final Throwable thrown) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span);
- span.finish();
+ }
}
protected abstract void processResult(Span span, T future)
