Wrap Rabbitmq channel span into scope

This commit is contained in:
Nikolay Martynov 2019-04-04 15:38:52 -04:00
parent 743ab327b0
commit 1af7487201
1 changed files with 17 additions and 12 deletions

View File

@ -189,10 +189,11 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
public static class ChannelGetAdvice { public static class ChannelGetAdvice {
@Advice.OnMethodEnter @Advice.OnMethodEnter
public static long takeTimestamp( public static long takeTimestamp(
@Advice.Local("placeholderScope") Scope scope, @Advice.Local("callDepth") int callDepth) { @Advice.Local("placeholderScope") Scope placeholderScope,
@Advice.Local("callDepth") int callDepth) {
callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class); callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class);
// Don't want RabbitCommandInstrumentation to mess up our actual parent span. // Don't want RabbitCommandInstrumentation to mess up our actual parent span.
scope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, true); placeholderScope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, true);
return System.currentTimeMillis(); return System.currentTimeMillis();
} }
@ -201,13 +202,13 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
@Advice.This final Channel channel, @Advice.This final Channel channel,
@Advice.Argument(0) final String queue, @Advice.Argument(0) final String queue,
@Advice.Enter final long startTime, @Advice.Enter final long startTime,
@Advice.Local("placeholderScope") final Scope scope, @Advice.Local("placeholderScope") final Scope placeholderScope,
@Advice.Local("callDepth") final int callDepth, @Advice.Local("callDepth") final int callDepth,
@Advice.Return final GetResponse response, @Advice.Return final GetResponse response,
@Advice.Thrown final Throwable throwable) { @Advice.Thrown final Throwable throwable) {
if (scope.span() instanceof NoopSpan) { if (placeholderScope.span() instanceof NoopSpan) {
scope.close(); placeholderScope.close();
} }
if (callDepth > 0) { if (callDepth > 0) {
@ -236,6 +237,8 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
final Integer length = response == null ? null : response.getBody().length; final Integer length = response == null ? null : response.getBody().length;
// TODO: it would be better if we could actually have span wrapped into the scope started in
// OnMethodEnter
final Span span = final Span span =
GlobalTracer.get() GlobalTracer.get()
.buildSpan("amqp.command") .buildSpan("amqp.command")
@ -244,15 +247,17 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
.withTag("message.size", length) .withTag("message.size", length)
.withTag(Tags.PEER_PORT.getKey(), connection.getPort()) .withTag(Tags.PEER_PORT.getKey(), connection.getPort())
.start(); .start();
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
CONSUMER_DECORATE.afterStart(span); CONSUMER_DECORATE.afterStart(span);
CONSUMER_DECORATE.onGet(span, queue); CONSUMER_DECORATE.onGet(span, queue);
CONSUMER_DECORATE.onPeerConnection(span, connection.getAddress()); CONSUMER_DECORATE.onPeerConnection(span, connection.getAddress());
CONSUMER_DECORATE.onError(span, throwable); CONSUMER_DECORATE.onError(span, throwable);
CONSUMER_DECORATE.beforeFinish(span); CONSUMER_DECORATE.beforeFinish(span);
span.finish(); } finally {
CallDepthThreadLocalMap.reset(Channel.class); CallDepthThreadLocalMap.reset(Channel.class);
} }
} }
}
public static class ChannelConsumeAdvice { public static class ChannelConsumeAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)