-
Notifications
You must be signed in to change notification settings - Fork 304
Reactor: early propagate span in context when subscribing #8166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d7ee06
Reactor: early propagate span in context when subscribing
amarziali 5e09240
review
amarziali 9ad2b7d
Merge branch 'master' into andrea.marziali/reactor-optimizable
amarziali 1323af9
Merge branch 'master' into andrea.marziali/reactor-optimizable
amarziali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...-core-3.1/src/main/java/datadog/trace/instrumentation/reactor/core/ContextSpanHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package datadog.trace.instrumentation.reactor.core; | ||
|
||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.api.WithAgentSpan; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.util.context.Context; | ||
|
||
public class ContextSpanHelper { | ||
private static final String DD_SPAN_KEY = "dd.span"; | ||
|
||
private ContextSpanHelper() {} | ||
|
||
public static AgentSpan extractSpanFromSubscriberContext(final CoreSubscriber<?> subscriber) { | ||
if (subscriber == null) { | ||
return null; | ||
} | ||
Context context = null; | ||
try { | ||
context = subscriber.currentContext(); | ||
} catch (Throwable ignored) { | ||
} | ||
if (context == null) { | ||
return null; | ||
} | ||
if (context.hasKey(DD_SPAN_KEY)) { | ||
Object maybeSpan = context.get(DD_SPAN_KEY); | ||
if (maybeSpan instanceof WithAgentSpan) { | ||
AgentSpan span = ((WithAgentSpan) maybeSpan).asAgentSpan(); | ||
if (span != null) { | ||
return span; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...rc/main/java/datadog/trace/instrumentation/reactor/core/CorePublisherInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package datadog.trace.instrumentation.reactor.core; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.hasSuperType; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf; | ||
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
import static datadog.trace.instrumentation.reactor.core.ContextSpanHelper.extractSpanFromSubscriberContext; | ||
import static net.bytebuddy.matcher.ElementMatchers.isStatic; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.bootstrap.InstrumentationContext; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import reactor.core.CoreSubscriber; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class CorePublisherInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
public CorePublisherInstrumentation() { | ||
super("reactor-core"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "reactor.core.CoreSubscriber"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return implementsInterface(named("reactor.core.CorePublisher")) // from 3.1.7 | ||
.or( | ||
hasSuperType( | ||
namedOneOf( | ||
"reactor.core.publisher.Mono", "reactor.core.publisher.Flux"))); // < 3.1.7 | ||
} | ||
|
||
@Override | ||
public Map<String, String> contextStore() { | ||
final Map<String, String> ret = new HashMap<>(); | ||
ret.put("org.reactivestreams.Subscriber", AgentSpan.class.getName()); | ||
ret.put("org.reactivestreams.Publisher", AgentSpan.class.getName()); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".ContextSpanHelper", | ||
}; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
named("subscribe") | ||
.and(not(isStatic())) | ||
.and(takesArguments(1)) | ||
.and(takesArgument(0, named("reactor.core.CoreSubscriber"))), | ||
getClass().getName() + "$PropagateContextSpanOnSubscribe"); | ||
} | ||
|
||
public static class PropagateContextSpanOnSubscribe { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static AgentScope before( | ||
@Advice.This Publisher<?> self, @Advice.Argument(0) final CoreSubscriber<?> subscriber) { | ||
final AgentSpan span = extractSpanFromSubscriberContext(subscriber); | ||
|
||
if (span != null) { | ||
// we force storing the span state linked to publisher and subscriber to the one explicitly | ||
// present in the context so that, if PublisherInstrumentation is kicking in after this | ||
// advice, it won't override that active span | ||
InstrumentationContext.get(Publisher.class, AgentSpan.class).put(self, span); | ||
InstrumentationContext.get(Subscriber.class, AgentSpan.class).put(subscriber, span); | ||
return activateSpan(span); | ||
} | ||
return null; | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void after(@Advice.Enter final AgentScope scope) { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.