Make sure span is opened and closed with scope in spymemcached

This commit is contained in:
Nikolay Martynov 2019-04-03 16:48:15 -04:00
parent bfb48f31c1
commit 2bba4c5591
1 changed files with 31 additions and 25 deletions

View File

@ -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,40 +30,45 @@ public abstract class CompletionListener<T> {
public CompletionListener(final MemcachedConnection connection, final String methodName) {
this.connection = connection;
span = GlobalTracer.get().buildSpan(OPERATION_NAME).start();
DECORATE.afterStart(span);
DECORATE.onConnection(span, connection);
DECORATE.onOperation(span, methodName);
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 {
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
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try {
processResult(span, future);
} catch (final CancellationException e) {
span.setTag(DB_COMMAND_CANCELLED, true);
} else {
DECORATE.onError(span, e.getCause());
} catch (final ExecutionException e) {
if (e.getCause() instanceof CancellationException) {
// Looks like underlying OperationFuture wraps CancellationException into
// ExecutionException
span.setTag(DB_COMMAND_CANCELLED, true);
} else {
DECORATE.onError(span, e.getCause());
}
} catch (final InterruptedException e) {
// Avoid swallowing InterruptedException
DECORATE.onError(span, e);
Thread.currentThread().interrupt();
} catch (final Exception e) {
// This should never happen, just in case to make sure we cover all unexpected exceptions
DECORATE.onError(span, e);
} finally {
DECORATE.beforeFinish(span);
}
} catch (final InterruptedException e) {
// Avoid swallowing InterruptedException
DECORATE.onError(span, e);
Thread.currentThread().interrupt();
} catch (final Exception e) {
// This should never happen, just in case to make sure we cover all unexpected exceptions
DECORATE.onError(span, e);
} finally {
DECORATE.beforeFinish(span);
span.finish();
}
}
protected void closeSyncSpan(final Throwable thrown) {
DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span);
span.finish();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span);
}
}
protected abstract void processResult(Span span, T future)