diff --git a/opentracing-adapter/src/main/java/io/opentracing/contrib/specialagent/LocalSpanContext.java b/opentracing-adapter/src/main/java/io/opentracing/contrib/specialagent/LocalSpanContext.java index bd07a49fc..a627afa95 100644 --- a/opentracing-adapter/src/main/java/io/opentracing/contrib/specialagent/LocalSpanContext.java +++ b/opentracing-adapter/src/main/java/io/opentracing/contrib/specialagent/LocalSpanContext.java @@ -17,31 +17,38 @@ import io.opentracing.Scope; import io.opentracing.Span; +import java.util.HashMap; +import java.util.Map; /** - * Thread local holder for Span, Scope and counter to control stack of calls. + * Thread local map holder for Span, Scope and counter to control stack of calls. + * Map is used to avoid suppressing of creation of new span when active span of another component exists. + * Key of the map is component name. */ public class LocalSpanContext { - private static final ThreadLocal instance = new ThreadLocal<>(); + private static final ThreadLocal> instance = new ThreadLocal<>(); + private final String name; private final Span span; private final Scope scope; private int counter = 1; - private LocalSpanContext(final Span span, final Scope scope) { + private LocalSpanContext(final String name, final Span span, final Scope scope) { + this.name = name; this.span = span; this.scope = scope; } - public static LocalSpanContext get() { - return instance.get(); + public static LocalSpanContext get(final String name) { + final Map map = instance.get(); + if (map != null) + return map.get(name); + return null; } - public static void set(final Span span, final Scope scope) { - instance.set(new LocalSpanContext(span, scope)); - } - - public static void remove() { - instance.remove(); + public static void set(final String name, final Span span, final Scope scope) { + if (instance.get() == null) + instance.set(new HashMap()); + instance.get().put(name, new LocalSpanContext(name, span, scope)); } public Span getSpan() { @@ -63,7 +70,12 @@ public void closeAndFinish() { } public void closeScope() { - instance.remove(); + final Map map = instance.get(); + if (map != null) { + map.remove(name); + if (map.isEmpty()) + instance.remove(); + } if (scope != null) scope.close(); } diff --git a/rule/akka-actor/src/main/java/io/opentracing/contrib/specialagent/rule/akka/actor/AkkaAgentIntercept.java b/rule/akka-actor/src/main/java/io/opentracing/contrib/specialagent/rule/akka/actor/AkkaAgentIntercept.java index 434b17e2e..194d0675c 100644 --- a/rule/akka-actor/src/main/java/io/opentracing/contrib/specialagent/rule/akka/actor/AkkaAgentIntercept.java +++ b/rule/akka-actor/src/main/java/io/opentracing/contrib/specialagent/rule/akka/actor/AkkaAgentIntercept.java @@ -37,8 +37,8 @@ public class AkkaAgentIntercept { static final String COMPONENT_NAME = "java-akka"; public static Object aroundReceiveStart(final Object thiz, final Object message) { - if (!(message instanceof TracedMessage) && LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (!(message instanceof TracedMessage) && LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return message; } @@ -61,13 +61,13 @@ public static Object aroundReceiveStart(final Object thiz, final Object message) final Span span = spanBuilder.start(); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); return tracedMessage != null ? tracedMessage.getMessage() : message; } public static void aroundReceiveEnd(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; @@ -114,7 +114,7 @@ else if (arg0 instanceof ActorSelection) tracer.inject(span.context(), Format.Builtin.TEXT_MAP_INJECT, headers::put); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); return new TracedMessage<>(message, headers); } @@ -123,7 +123,7 @@ public static void askEnd(final Object arg0, final Object message, final Throwab if (sender instanceof PromiseActorRef || arg0 instanceof PromiseActorRef || !(message instanceof TracedMessage)) return; - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/akka-http/src/main/java/io/opentracing/contrib/specialagent/rule/akka/http/AkkaAgentIntercept.java b/rule/akka-http/src/main/java/io/opentracing/contrib/specialagent/rule/akka/http/AkkaAgentIntercept.java index aec7efc0b..ad24713d9 100644 --- a/rule/akka-http/src/main/java/io/opentracing/contrib/specialagent/rule/akka/http/AkkaAgentIntercept.java +++ b/rule/akka-http/src/main/java/io/opentracing/contrib/specialagent/rule/akka/http/AkkaAgentIntercept.java @@ -34,8 +34,8 @@ public class AkkaAgentIntercept { static final String COMPONENT_NAME_SERVER = "akka-http-server"; public static Object requestStart(final Object arg0) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME_CLIENT) != null) { + LocalSpanContext.get(COMPONENT_NAME_CLIENT).increment(); return arg0; } @@ -54,14 +54,14 @@ public static Object requestStart(final Object arg0) { final HttpHeadersInjectAdapter injectAdapter = new HttpHeadersInjectAdapter(request); tracer.inject(span.context(), Builtin.HTTP_HEADERS, injectAdapter); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME_CLIENT, span, tracer.activateSpan(span)); return injectAdapter.getHttpRequest(); } @SuppressWarnings("unchecked") public static Object requestEnd(final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME_CLIENT); if (context == null || context.decrementAndGet() != 0) return returned; diff --git a/rule/apache-httpclient/src/main/java/io/opentracing/contrib/specialagent/rule/apache/httpclient/HttpClientAgentIntercept.java b/rule/apache-httpclient/src/main/java/io/opentracing/contrib/specialagent/rule/apache/httpclient/HttpClientAgentIntercept.java index 156333300..91d998f0c 100644 --- a/rule/apache-httpclient/src/main/java/io/opentracing/contrib/specialagent/rule/apache/httpclient/HttpClientAgentIntercept.java +++ b/rule/apache-httpclient/src/main/java/io/opentracing/contrib/specialagent/rule/apache/httpclient/HttpClientAgentIntercept.java @@ -42,7 +42,7 @@ public static Object[] enter(final Object arg0, final Object arg1, final Object return null; } - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context != null) { context.increment(); return null; @@ -59,7 +59,7 @@ public static Object[] enter(final Object arg0, final Object arg1, final Object for (final ApacheClientSpanDecorator decorator : Configuration.spanDecorators) decorator.onRequest(request, arg0 instanceof HttpHost ? (HttpHost)arg0 : null, span); - LocalSpanContext.set(span, null); + LocalSpanContext.set(COMPONENT_NAME, span, null); tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request)); if (arg1 instanceof ResponseHandler) @@ -72,7 +72,7 @@ public static Object[] enter(final Object arg0, final Object arg1, final Object } public static void exit(final Object returned) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; @@ -86,7 +86,7 @@ public static void exit(final Object returned) { } public static void onError(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; diff --git a/rule/feign/src/main/java/io/opentracing/contrib/specialagent/rule/feign/FeignAgentIntercept.java b/rule/feign/src/main/java/io/opentracing/contrib/specialagent/rule/feign/FeignAgentIntercept.java index f59a32937..33252e44b 100644 --- a/rule/feign/src/main/java/io/opentracing/contrib/specialagent/rule/feign/FeignAgentIntercept.java +++ b/rule/feign/src/main/java/io/opentracing/contrib/specialagent/rule/feign/FeignAgentIntercept.java @@ -32,19 +32,22 @@ import io.opentracing.util.GlobalTracer; public class FeignAgentIntercept { + static final String COMPONENT_NAME = "java-feign"; + public static Object onRequest(final Object arg1, final Object arg2) { final Request request = (Request)arg1; final Tracer tracer = GlobalTracer.get(); final Span span = tracer .buildSpan(request.method()) - .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) + .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CLIENT) + .withTag(Tags.COMPONENT, COMPONENT_NAME) .start(); for (final FeignSpanDecorator decorator : Configuration.spanDecorators) decorator.onRequest(request, (Options)arg2, span); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); return inject(tracer, span.context(), request); } @@ -72,7 +75,7 @@ public static void onResponse(final Object arg1, final Object arg2, final Object } private static void finish() { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context != null) context.closeAndFinish(); } diff --git a/rule/google-http-client/src/main/java/io/opentracing/contrib/specialagent/rule/googlehttpclient/GoogleHttpClientAgentIntercept.java b/rule/google-http-client/src/main/java/io/opentracing/contrib/specialagent/rule/googlehttpclient/GoogleHttpClientAgentIntercept.java index 46652d47f..661b9ff4c 100644 --- a/rule/google-http-client/src/main/java/io/opentracing/contrib/specialagent/rule/googlehttpclient/GoogleHttpClientAgentIntercept.java +++ b/rule/google-http-client/src/main/java/io/opentracing/contrib/specialagent/rule/googlehttpclient/GoogleHttpClientAgentIntercept.java @@ -31,8 +31,8 @@ public class GoogleHttpClientAgentIntercept { static final String COMPONENT_NAME = "google-http-client"; public static void enter(final Object thiz) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } @@ -50,11 +50,11 @@ public static void enter(final Object thiz) { final Scope scope = tracer.activateSpan(span); tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request.getHeaders())); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } public static void exit(Throwable thrown, Object returned) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/httpurlconnection/src/main/java/io/opentracing/contrib/specialagent/rule/httpurlconnection/HttpURLConnectionAgentIntercept.java b/rule/httpurlconnection/src/main/java/io/opentracing/contrib/specialagent/rule/httpurlconnection/HttpURLConnectionAgentIntercept.java index 5acd40733..1638301e3 100644 --- a/rule/httpurlconnection/src/main/java/io/opentracing/contrib/specialagent/rule/httpurlconnection/HttpURLConnectionAgentIntercept.java +++ b/rule/httpurlconnection/src/main/java/io/opentracing/contrib/specialagent/rule/httpurlconnection/HttpURLConnectionAgentIntercept.java @@ -33,8 +33,8 @@ public class HttpURLConnectionAgentIntercept { static final String COMPONENT_NAME = "http-url-connection"; public static void enter(final Object thiz, final boolean connected) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } @@ -58,11 +58,11 @@ public static void enter(final Object thiz, final boolean connected) { final Scope scope = tracer.activateSpan(span); tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpURLConnectionInjectAdapter(connection)); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } public static void exit(final Throwable thrown, int responseCode) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/kafka-streams/src/main/java/io/opentracing/contrib/specialagent/rule/kafka/streams/KafkaStreamsAgentIntercept.java b/rule/kafka-streams/src/main/java/io/opentracing/contrib/specialagent/rule/kafka/streams/KafkaStreamsAgentIntercept.java index 5abf2d8a5..592977ae4 100644 --- a/rule/kafka-streams/src/main/java/io/opentracing/contrib/specialagent/rule/kafka/streams/KafkaStreamsAgentIntercept.java +++ b/rule/kafka-streams/src/main/java/io/opentracing/contrib/specialagent/rule/kafka/streams/KafkaStreamsAgentIntercept.java @@ -29,19 +29,21 @@ import io.opentracing.util.GlobalTracer; public class KafkaStreamsAgentIntercept { + static final String COMPONENT_NAME = "kafka-streams"; + public static void onNextRecordExit(final Object record) { if (record == null) return; - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } final Tracer tracer = GlobalTracer.get(); final StampedRecord stampedRecord = (StampedRecord)record; final SpanBuilder spanBuilder = tracer.buildSpan("consume") - .withTag(Tags.COMPONENT, "kafka-streams") + .withTag(Tags.COMPONENT, COMPONENT_NAME) .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER) .withTag(Tags.PEER_SERVICE, "kafka") .withTag("partition", stampedRecord.partition()) @@ -55,11 +57,11 @@ public static void onNextRecordExit(final Object record) { spanBuilder.asChildOf(parentContext); final Span span = spanBuilder.start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void onProcessExit(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; diff --git a/rule/lettuce/src/main/java/io/opentracing/contrib/specialagent/rule/lettuce/LettuceAgentIntercept.java b/rule/lettuce/src/main/java/io/opentracing/contrib/specialagent/rule/lettuce/LettuceAgentIntercept.java index 637182fa6..9a2da3870 100644 --- a/rule/lettuce/src/main/java/io/opentracing/contrib/specialagent/rule/lettuce/LettuceAgentIntercept.java +++ b/rule/lettuce/src/main/java/io/opentracing/contrib/specialagent/rule/lettuce/LettuceAgentIntercept.java @@ -56,11 +56,11 @@ public static void dispatchStart(final Object arg) { .withTag(Tags.DB_TYPE.getKey(), DB_TYPE) .start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void dispatchEnd(final Object command, final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; @@ -138,11 +138,11 @@ public static void connectStart(Object arg) { .withTag("db.redis.dbIndex", redisURI.getDatabase()) .start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void connectEnd(final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; diff --git a/rule/play/src/main/java/io/opentracing/contrib/specialagent/rule/play/PlayAgentIntercept.java b/rule/play/src/main/java/io/opentracing/contrib/specialagent/rule/play/PlayAgentIntercept.java index f6583d886..8e1a5d150 100644 --- a/rule/play/src/main/java/io/opentracing/contrib/specialagent/rule/play/PlayAgentIntercept.java +++ b/rule/play/src/main/java/io/opentracing/contrib/specialagent/rule/play/PlayAgentIntercept.java @@ -35,8 +35,8 @@ public class PlayAgentIntercept { static final String COMPONENT_NAME = "play"; public static void applyStart(final Object arg0) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } @@ -53,12 +53,12 @@ public static void applyStart(final Object arg0) { spanBuilder.asChildOf(parent); final Span span = spanBuilder.start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } @SuppressWarnings("unchecked") public static void applyEnd(final Object thiz, final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/pulsar-client/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/client/PulsarClientAgentIntercept.java b/rule/pulsar-client/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/client/PulsarClientAgentIntercept.java index b04472fc4..f9e2b9445 100644 --- a/rule/pulsar-client/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/client/PulsarClientAgentIntercept.java +++ b/rule/pulsar-client/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/client/PulsarClientAgentIntercept.java @@ -71,8 +71,8 @@ public static void receiveAsyncEnd(final Object thiz, final Object returned) { } public static void internalSendAsyncEnter(final Object thiz, final Object arg) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } @@ -93,12 +93,12 @@ public static void internalSendAsyncEnter(final Object thiz, final Object arg) { tracer.inject(span.context(), Builtin.TEXT_MAP, new PropertiesMapInjectAdapter(message.getMessageBuilder())); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } @SuppressWarnings("unchecked") public static Object internalSendAsyncEnd(final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return returned; diff --git a/rule/pulsar-functions/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/functions/PulsarFunctionsAgentIntercept.java b/rule/pulsar-functions/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/functions/PulsarFunctionsAgentIntercept.java index bed4ec04c..7bfc4550e 100644 --- a/rule/pulsar-functions/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/functions/PulsarFunctionsAgentIntercept.java +++ b/rule/pulsar-functions/src/main/java/io/opentracing/contrib/specialagent/rule/pulsar/functions/PulsarFunctionsAgentIntercept.java @@ -51,7 +51,7 @@ public static void handleMessageEnter(final Object function, final Object contex final Span span = spanBuilder.start(); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } private static String getFunctionName(final Object function, final Object contextArg) { @@ -69,7 +69,7 @@ private static String getFunctionName(final Object function, final Object contex } public static void handleMessageEnd(final Object returned, final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/rabbitmq-client/src/main/java/io/opentracing/contrib/specialagent/rule/rabbitmq/client/RabbitMQAgentIntercept.java b/rule/rabbitmq-client/src/main/java/io/opentracing/contrib/specialagent/rule/rabbitmq/client/RabbitMQAgentIntercept.java index 521d30201..83a292970 100644 --- a/rule/rabbitmq-client/src/main/java/io/opentracing/contrib/specialagent/rule/rabbitmq/client/RabbitMQAgentIntercept.java +++ b/rule/rabbitmq-client/src/main/java/io/opentracing/contrib/specialagent/rule/rabbitmq/client/RabbitMQAgentIntercept.java @@ -25,6 +25,7 @@ import io.opentracing.Span; import io.opentracing.Tracer; import io.opentracing.contrib.common.WrapperProxy; +import io.opentracing.contrib.rabbitmq.SpanDecorator; import io.opentracing.contrib.rabbitmq.TracingConsumer; import io.opentracing.contrib.rabbitmq.TracingUtils; import io.opentracing.contrib.specialagent.LocalSpanContext; @@ -41,7 +42,7 @@ public static void exitGet(final Object response, final Object queue, final Thro } public static void finish(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(SpanDecorator.COMPONENT_NAME); if (context == null) return; @@ -57,7 +58,7 @@ public static AMQP.BasicProperties enterPublish(final Object exchange, final Obj final Span span = TracingUtils.buildSpan((String)exchange, (String)routingKey, properties, tracer); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(SpanDecorator.COMPONENT_NAME, span, scope); return inject(properties, span, tracer); } diff --git a/rule/servlet/src/main/java/io/opentracing/contrib/specialagent/rule/servlet/ServletAgentIntercept.java b/rule/servlet/src/main/java/io/opentracing/contrib/specialagent/rule/servlet/ServletAgentIntercept.java index 0a7f31a6f..f7e58be0d 100644 --- a/rule/servlet/src/main/java/io/opentracing/contrib/specialagent/rule/servlet/ServletAgentIntercept.java +++ b/rule/servlet/src/main/java/io/opentracing/contrib/specialagent/rule/servlet/ServletAgentIntercept.java @@ -36,6 +36,7 @@ public class ServletAgentIntercept extends ServletFilterAgentIntercept { private static final List spanDecorators = Configuration.spanDecorators; + static final String COMPONENT_NAME = "java-web-servlet"; public static void init(final Object thiz, final Object servletConfig) { filterOrServletToServletContext.put(thiz, ((ServletConfig)servletConfig).getServletContext()); @@ -73,7 +74,7 @@ public static void serviceEnter(final Object thiz, final Object req, final Objec if (request.getAttribute(TracingFilter.SERVER_SPAN_CONTEXT) != null) return; - if (LocalSpanContext.get() != null) + if (LocalSpanContext.get(COMPONENT_NAME) != null) return; if (!Configuration.isTraced(request)) @@ -81,7 +82,7 @@ public static void serviceEnter(final Object thiz, final Object req, final Objec final Tracer tracer = GlobalTracer.get(); final Span span = TracingFilterUtil.buildSpan(request, tracer, spanDecorators); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); if (logger.isLoggable(Level.FINER)) logger.finer("<< ServletAgentIntercept#service(" + AgentRuleUtil.getSimpleNameId(req) + "," + AgentRuleUtil.getSimpleNameId(res) + "," + AgentRuleUtil.getSimpleNameId(context) + ")"); } @@ -92,7 +93,7 @@ public static void serviceEnter(final Object thiz, final Object req, final Objec public static void serviceExit(final Object request, final Object response, final Throwable thrown) { try { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/spring-jms/src/main/java/io/opentracing/contrib/specialagent/rule/spring/jms/SpringJmsAgentIntercept.java b/rule/spring-jms/src/main/java/io/opentracing/contrib/specialagent/rule/spring/jms/SpringJmsAgentIntercept.java index 575569ebf..7ace707cc 100644 --- a/rule/spring-jms/src/main/java/io/opentracing/contrib/specialagent/rule/spring/jms/SpringJmsAgentIntercept.java +++ b/rule/spring-jms/src/main/java/io/opentracing/contrib/specialagent/rule/spring/jms/SpringJmsAgentIntercept.java @@ -32,16 +32,18 @@ import io.opentracing.util.GlobalTracer; public class SpringJmsAgentIntercept { + static final String COMPONENT_NAME = "spring-jms"; + public static void onMessageEnter(final Object msg) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } final Tracer tracer = GlobalTracer.get(); final SpanBuilder builder = tracer .buildSpan("onMessage") - .withTag(Tags.COMPONENT, "spring-jms") + .withTag(Tags.COMPONENT, COMPONENT_NAME) .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER); SpanContext spanContext = null; @@ -55,11 +57,11 @@ public static void onMessageEnter(final Object msg) { builder.addReference(References.FOLLOWS_FROM, spanContext); final Span span = builder.start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void onMessageExit(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; diff --git a/rule/spring-kafka/src/main/java/io/opentracing/contrib/specialagent/rule/spring/kafka/SpringKafkaAgentIntercept.java b/rule/spring-kafka/src/main/java/io/opentracing/contrib/specialagent/rule/spring/kafka/SpringKafkaAgentIntercept.java index 542c29e2a..c764dad64 100644 --- a/rule/spring-kafka/src/main/java/io/opentracing/contrib/specialagent/rule/spring/kafka/SpringKafkaAgentIntercept.java +++ b/rule/spring-kafka/src/main/java/io/opentracing/contrib/specialagent/rule/spring/kafka/SpringKafkaAgentIntercept.java @@ -29,16 +29,18 @@ import io.opentracing.util.GlobalTracer; public class SpringKafkaAgentIntercept { + static final String COMPONENT_NAME = "spring-kafka"; + public static void onMessageEnter(final Object record) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } final Tracer tracer = GlobalTracer.get(); final SpanBuilder builder = tracer .buildSpan("onMessage") - .withTag(Tags.COMPONENT, "spring-kafka") + .withTag(Tags.COMPONENT, COMPONENT_NAME) .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER); if (record instanceof ConsumerRecord) { @@ -49,11 +51,11 @@ public static void onMessageEnter(final Object record) { } final Span span = builder.start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void onMessageExit(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context != null && context.decrementAndGet() == 0) { if (thrown != null) OpenTracingApiUtil.setErrorTag(context.getSpan(), thrown); diff --git a/rule/spring-rabbitmq/src/main/java/io/opentracing/contrib/specialagent/rule/spring/rabbitmq/SpringRabbitMQAgentIntercept.java b/rule/spring-rabbitmq/src/main/java/io/opentracing/contrib/specialagent/rule/spring/rabbitmq/SpringRabbitMQAgentIntercept.java index 35036d3bd..795eef4c1 100644 --- a/rule/spring-rabbitmq/src/main/java/io/opentracing/contrib/specialagent/rule/spring/rabbitmq/SpringRabbitMQAgentIntercept.java +++ b/rule/spring-rabbitmq/src/main/java/io/opentracing/contrib/specialagent/rule/spring/rabbitmq/SpringRabbitMQAgentIntercept.java @@ -38,16 +38,18 @@ import io.opentracing.util.GlobalTracer; public class SpringRabbitMQAgentIntercept { + static final String COMPONENT_NAME = "spring-rabbitmq"; + public static void onMessageEnter(final Object msg) { - if (LocalSpanContext.get() != null) { - LocalSpanContext.get().increment(); + if (LocalSpanContext.get(COMPONENT_NAME) != null) { + LocalSpanContext.get(COMPONENT_NAME).increment(); return; } final Tracer tracer = GlobalTracer.get(); final SpanBuilder builder = tracer .buildSpan("onMessage") - .withTag(Tags.COMPONENT, "spring-rabbitmq") + .withTag(Tags.COMPONENT, COMPONENT_NAME) .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER); final Message message = (Message)msg; @@ -59,11 +61,11 @@ public static void onMessageEnter(final Object msg) { } final Span span = builder.start(); - LocalSpanContext.set(span, tracer.activateSpan(span)); + LocalSpanContext.set(COMPONENT_NAME, span, tracer.activateSpan(span)); } public static void onMessageExit(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null || context.decrementAndGet() != 0) return; @@ -84,11 +86,11 @@ public static void handleDeliveryStart(Object thiz, Object props) { final Tracer tracer = GlobalTracer.get(); final Span span = TracingUtils.buildChildSpan(properties, null, tracer); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } public static void handleDeliveryEnd(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return; diff --git a/rule/spring-scheduling/src/main/java/io/opentracing/contrib/specialagent/rule/spring/scheduling/SpringSchedulingAgentIntercept.java b/rule/spring-scheduling/src/main/java/io/opentracing/contrib/specialagent/rule/spring/scheduling/SpringSchedulingAgentIntercept.java index 014a33630..9c0c96c15 100644 --- a/rule/spring-scheduling/src/main/java/io/opentracing/contrib/specialagent/rule/spring/scheduling/SpringSchedulingAgentIntercept.java +++ b/rule/spring-scheduling/src/main/java/io/opentracing/contrib/specialagent/rule/spring/scheduling/SpringSchedulingAgentIntercept.java @@ -28,22 +28,24 @@ import io.opentracing.util.GlobalTracer; public class SpringSchedulingAgentIntercept { + static final String COMPONENT_NAME = "spring-scheduled"; + public static void enter(final Object thiz) { final ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)thiz; final Tracer tracer = GlobalTracer.get(); final Span span = tracer .buildSpan(runnable.getMethod().getName()) - .withTag(Tags.COMPONENT.getKey(), "spring-scheduled") + .withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME) .withTag("class", runnable.getClass().getSimpleName()) .withTag("method", runnable.getMethod().getName()) .start(); final Scope scope = tracer.activateSpan(span); - LocalSpanContext.set(span, scope); + LocalSpanContext.set(COMPONENT_NAME, span, scope); } public static void exit(final Throwable thrown) { - final LocalSpanContext context = LocalSpanContext.get(); + final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME); if (context == null) return;