Stop using `scopeManager().activate(span, true)`

This commit is contained in:
Nikolay Martynov 2019-04-08 11:54:47 -04:00
parent f4791a17df
commit 6fd630831f
13 changed files with 59 additions and 35 deletions

View File

@ -139,8 +139,9 @@ public class CouchbaseBucketInstrumentation extends Instrumenter.Default {
final Span span = spanRef.getAndSet(null);
if (span != null) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.beforeFinish(span);
span.finish();
}
}
}
@ -157,9 +158,10 @@ public class CouchbaseBucketInstrumentation extends Instrumenter.Default {
public void call(final Throwable throwable) {
final Span span = spanRef.getAndSet(null);
if (span != null) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
}
}
}

View File

@ -133,8 +133,9 @@ public class CouchbaseClusterInstrumentation extends Instrumenter.Default {
final Span span = spanRef.getAndSet(null);
if (span != null) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.beforeFinish(span);
span.finish();
}
}
}
@ -151,9 +152,10 @@ public class CouchbaseClusterInstrumentation extends Instrumenter.Default {
public void call(final Throwable throwable) {
final Span span = spanRef.getAndSet(null);
if (span != null) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
}
}
}

View File

@ -60,7 +60,7 @@ public class TracingSession implements Session {
@Override
public ResultSet execute(final String query) {
try (final Scope scope = startSpanWithScope(query, true)) {
try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query);
beforeSpanFinish(scope.span(), resultSet);
@ -68,13 +68,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
} finally {
scope.span().finish();
}
}
}
@Override
public ResultSet execute(final String query, final Object... values) {
try (final Scope scope = startSpanWithScope(query, true)) {
try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query, values);
beforeSpanFinish(scope.span(), resultSet);
@ -82,13 +84,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
} finally {
scope.span().finish();
}
}
}
@Override
public ResultSet execute(final String query, final Map<String, Object> values) {
try (final Scope scope = startSpanWithScope(query, true)) {
try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query, values);
beforeSpanFinish(scope.span(), resultSet);
@ -96,6 +100,8 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
} finally {
scope.span().finish();
}
}
}
@ -103,7 +109,7 @@ public class TracingSession implements Session {
@Override
public ResultSet execute(final Statement statement) {
final String query = getQuery(statement);
try (final Scope scope = startSpanWithScope(query, true)) {
try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(statement);
beforeSpanFinish(scope.span(), resultSet);
@ -111,13 +117,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
} finally {
scope.span().finish();
}
}
}
@Override
public ResultSetFuture executeAsync(final String query) {
try (final Scope scope = startSpanWithScope(query, false)) {
try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query);
future.addListener(createListener(scope.span(), future), executorService);
@ -127,7 +135,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final String query, final Object... values) {
try (final Scope scope = startSpanWithScope(query, false)) {
try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query, values);
future.addListener(createListener(scope.span(), future), executorService);
@ -137,7 +145,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final String query, final Map<String, Object> values) {
try (final Scope scope = startSpanWithScope(query, false)) {
try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query, values);
future.addListener(createListener(scope.span(), future), executorService);
@ -148,7 +156,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final Statement statement) {
final String query = getQuery(statement);
try (final Scope scope = startSpanWithScope(query, false)) {
try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(statement);
future.addListener(createListener(scope.span(), future), executorService);
@ -216,18 +224,20 @@ public class TracingSession implements Session {
return new Runnable() {
@Override
public void run() {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
beforeSpanFinish(span, future.get());
} catch (final InterruptedException | ExecutionException e) {
beforeSpanFinish(span, e);
} finally {
span.finish();
}
}
};
}
private Scope startSpanWithScope(final String query, final boolean finishOnClose) {
private Scope startSpanWithScope(final String query) {
final Span span = tracer.buildSpan("cassandra.execute").start();
final Scope scope = tracer.scopeManager().activate(span, finishOnClose);
final Scope scope = tracer.scopeManager().activate(span, false);
DECORATE.afterStart(span);
DECORATE.onConnection(span, session);
DECORATE.onStatement(span, query);

View File

@ -131,14 +131,14 @@ public class TracingClientInterceptor implements ClientInterceptor {
public void onClose(final Status status, final Metadata trailers) {
DECORATE.onClose(span, status);
// Finishes span.
try (final Scope ignored = tracer.scopeManager().activate(span, true)) {
try (final Scope ignored = tracer.scopeManager().activate(span, false)) {
delegate().onClose(status, trailers);
DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
throw e;
} finally {
DECORATE.beforeFinish(span);
span.finish();
throw e;
}
}

View File

@ -140,7 +140,7 @@ public class TracingServerInterceptor implements ServerInterceptor {
@Override
public void onCancel() {
// Finishes span.
try (final Scope scope = tracer.scopeManager().activate(span, true)) {
try (final Scope scope = tracer.scopeManager().activate(span, false)) {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
@ -149,19 +149,19 @@ public class TracingServerInterceptor implements ServerInterceptor {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(false);
}
DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
throw e;
} finally {
DECORATE.beforeFinish(span);
span.finish();
throw e;
}
}
@Override
public void onComplete() {
// Finishes span.
try (final Scope scope = tracer.scopeManager().activate(span, true)) {
try (final Scope scope = tracer.scopeManager().activate(span, false)) {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
@ -169,12 +169,12 @@ public class TracingServerInterceptor implements ServerInterceptor {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(false);
}
DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
throw e;
} finally {
DECORATE.beforeFinish(span);
span.finish();
throw e;
}
}

View File

@ -199,9 +199,10 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
public void finishSpan(final Throwable throwable) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
span = null;
finished = true;
}
@ -214,9 +215,10 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
* (e.g. breaks getOutputStream).
*/
if (responseCode > 0) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onResponse(span, responseCode);
DECORATE.beforeFinish(span);
span.finish();
span = null;
finished = true;
}

View File

@ -66,7 +66,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
.withTag(Tags.COMPONENT.getKey(), COMPONENT)
.start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
Tags.HTTP_URL.set(span, url.toString());
Tags.PEER_PORT.set(span, url.getPort() == -1 ? 80 : url.getPort());
Tags.PEER_HOSTNAME.set(span, url.getHost());
@ -76,6 +76,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
Tags.ERROR.set(span, true);
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
span.finish();
}
}
}

View File

@ -93,7 +93,7 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
final Span span = spanBuilder.start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
CONSUMER_DECORATE.afterStart(span);
if (message == null) {
CONSUMER_DECORATE.onReceive(span, method);
@ -102,6 +102,7 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
CONSUMER_DECORATE.onError(span, throwable);
CONSUMER_DECORATE.beforeFinish(span);
span.finish();
}
}
}

View File

@ -90,9 +90,10 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, false)) {
NettyHttpServerDecorator.DECORATE.onError(errorSpan, cause);
NettyHttpServerDecorator.DECORATE.beforeFinish(errorSpan);
errorSpan.finish();
}
return parentScope;

View File

@ -90,9 +90,10 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, false)) {
NettyHttpServerDecorator.DECORATE.onError(errorSpan, cause);
NettyHttpServerDecorator.DECORATE.beforeFinish(errorSpan);
errorSpan.finish();
}
return parentScope;

View File

@ -42,13 +42,14 @@ public class TracingCallFactory implements Call.Factory {
@Override
public Response intercept(final Chain chain) throws IOException {
try (final Scope interceptorScope =
GlobalTracer.get().scopeManager().activate(span, true)) {
GlobalTracer.get().scopeManager().activate(span, false)) {
return chain.proceed(chain.request());
} catch (final Exception ex) {
DECORATE.onError(scope, ex);
throw ex;
} finally {
DECORATE.beforeFinish(span);
span.finish();
}
}
});

View File

@ -193,7 +193,7 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
@Advice.Local("callDepth") int callDepth) {
callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class);
// Don't want RabbitCommandInstrumentation to mess up our actual parent span.
placeholderScope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, true);
placeholderScope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, false);
return System.currentTimeMillis();
}
@ -247,13 +247,14 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
.withTag("message.size", length)
.withTag(Tags.PEER_PORT.getKey(), connection.getPort())
.start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
CONSUMER_DECORATE.afterStart(span);
CONSUMER_DECORATE.onGet(span, queue);
CONSUMER_DECORATE.onPeerConnection(span, connection.getAddress());
CONSUMER_DECORATE.onError(span, throwable);
CONSUMER_DECORATE.beforeFinish(span);
} finally {
span.finish();
CallDepthThreadLocalMap.reset(Channel.class);
}
}

View File

@ -38,7 +38,7 @@ public abstract class CompletionListener<T> {
}
protected void closeAsyncSpan(final T future) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
try {
processResult(span, future);
} catch (final CancellationException e) {
@ -60,14 +60,16 @@ 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)) {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span);
span.finish();
}
}