Skip to content

Commit

Permalink
Merge pull request #572 from malafeev/fix-local-span-context
Browse files Browse the repository at this point in the history
Avoid suppressing of creation of new span when active span of another component exists
  • Loading branch information
safris authored May 12, 2020
2 parents 46f3a82 + d9b8d65 commit 771e5cf
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalSpanContext> instance = new ThreadLocal<>();
private static final ThreadLocal<Map<String, LocalSpanContext>> 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<String, LocalSpanContext> 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<String, LocalSpanContext>());
instance.get().put(name, new LocalSpanContext(name, span, scope));
}

public Span getSpan() {
Expand All @@ -63,7 +70,12 @@ public void closeAndFinish() {
}

public void closeScope() {
instance.remove();
final Map<String, LocalSpanContext> map = instance.get();
if (map != null) {
map.remove(name);
if (map.isEmpty())
instance.remove();
}
if (scope != null)
scope.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down
Loading

0 comments on commit 771e5cf

Please sign in to comment.