-
Notifications
You must be signed in to change notification settings - Fork 293
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
Introduce tracing propagator #8313
Open
PerfectSlayer
wants to merge
1
commit into
master
Choose a base branch
from
bbujon/context-propagation-step2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−4
Open
Changes from all commits
Commits
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 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
76 changes: 76 additions & 0 deletions
76
dd-trace-core/src/main/java/datadog/trace/core/propagation/TracingPropagator.java
This file contains 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,76 @@ | ||
package datadog.trace.core.propagation; | ||
|
||
import static datadog.trace.bootstrap.instrumentation.api.AgentSpan.fromContext; | ||
import static datadog.trace.bootstrap.instrumentation.api.AgentSpan.fromSpanContext; | ||
|
||
import datadog.context.Context; | ||
import datadog.context.propagation.CarrierSetter; | ||
import datadog.context.propagation.CarrierVisitor; | ||
import datadog.context.propagation.Propagator; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext; | ||
import datadog.trace.bootstrap.instrumentation.api.TagContext; | ||
import datadog.trace.core.DDSpanContext; | ||
import datadog.trace.core.propagation.HttpCodec.Extractor; | ||
import datadog.trace.core.propagation.HttpCodec.Injector; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
/** Propagator for tracing concern. */ | ||
@ParametersAreNonnullByDefault | ||
public class TracingPropagator implements Propagator { | ||
private final Injector injector; | ||
private final Extractor extractor; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param injector The {@link Injector} used for tracing context injection. | ||
* @param extractor The {@link Extractor} used for tracing context extraction. | ||
*/ | ||
public TracingPropagator(Injector injector, Extractor extractor) { | ||
this.injector = injector; | ||
this.extractor = extractor; | ||
} | ||
|
||
@Override | ||
public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) { | ||
AgentSpan span; | ||
//noinspection ConstantValue | ||
if (context == null | ||
|| carrier == null | ||
|| setter == null | ||
|| (span = fromContext(context)) == null) { | ||
return; | ||
} | ||
AgentSpanContext spanContext = span.context(); | ||
if (spanContext instanceof DDSpanContext) { | ||
DDSpanContext ddSpanContext = (DDSpanContext) spanContext; | ||
ddSpanContext.getTraceCollector().setSamplingPriorityIfNecessary(); | ||
this.injector.inject(ddSpanContext, carrier, setter::set); | ||
} | ||
} | ||
|
||
@Override | ||
public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor) { | ||
//noinspection ConstantValue | ||
if (context == null || carrier == null || visitor == null) { | ||
return context; | ||
} | ||
TagContext spanContext = this.extractor.extract(carrier, toContextVisitor(visitor)); | ||
// If the extraction fails, return the original context | ||
if (spanContext == null) { | ||
return context; | ||
} | ||
// Otherwise, append a fake span wrapper to context | ||
return context.with(fromSpanContext(spanContext)); | ||
} | ||
|
||
private static <C> AgentPropagation.ContextVisitor<C> toContextVisitor( | ||
CarrierVisitor<C> visitor) { | ||
if (visitor instanceof AgentPropagation.ContextVisitor) { | ||
return (AgentPropagation.ContextVisitor<C>) visitor; | ||
} | ||
return (carrier, classifier) -> visitor.forEachKeyValue(carrier, classifier::accept); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
dd-trace-core/src/test/groovy/datadog/trace/core/propagation/TracingPropagatorTest.groovy
This file contains 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,139 @@ | ||
package datadog.trace.core.propagation | ||
|
||
import datadog.context.Context | ||
import datadog.context.propagation.CarrierSetter | ||
import datadog.context.propagation.Propagators | ||
import datadog.trace.api.sampling.PrioritySampling | ||
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation | ||
import datadog.trace.common.writer.LoggingWriter | ||
import datadog.trace.core.ControllableSampler | ||
import datadog.trace.core.test.DDCoreSpecification | ||
|
||
import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP | ||
import static datadog.trace.api.sampling.PrioritySampling.USER_DROP | ||
|
||
class TracingPropagatorTest extends DDCoreSpecification { | ||
HttpCodec.Injector injector | ||
HttpCodec.Extractor extractor | ||
TracingPropagator propagator | ||
|
||
def setup() { | ||
injector = Mock(HttpCodec.Injector) | ||
extractor = Mock(HttpCodec.Extractor) | ||
this.propagator = new TracingPropagator(injector, extractor) | ||
} | ||
|
||
def 'test tracing propagator context injection'() { | ||
setup: | ||
def tracer = tracerBuilder().build() | ||
def span = tracer.buildSpan('test', 'operation').start() | ||
def setter = Mock(CarrierSetter) | ||
def carrier = new Object() | ||
|
||
when: | ||
this.propagator.inject(span, carrier, setter) | ||
|
||
then: | ||
1 * injector.inject(span.context(), carrier, _) | ||
|
||
cleanup: | ||
span.finish() | ||
tracer.close() | ||
} | ||
|
||
def 'test tracing propagator context extractor'() { | ||
setup: | ||
def context = Context.root() | ||
// TODO Use ContextVisitor mock as getter once extractor API is refactored | ||
def getter = Mock(AgentPropagation.ContextVisitor) | ||
def carrier = new Object() | ||
|
||
when: | ||
this.propagator.extract(context, carrier, getter) | ||
|
||
then: | ||
1 * extractor.extract(carrier, _) | ||
} | ||
|
||
def 'span priority set when injecting'() { | ||
given: | ||
injectSysConfig('writer.type', 'LoggingWriter') | ||
def tracer = tracerBuilder().build() | ||
def setter = Mock(CarrierSetter) | ||
def carrier = new Object() | ||
|
||
when: | ||
def root = tracer.buildSpan('test', 'parent').start() | ||
def child = tracer.buildSpan('test', 'child').asChildOf(root).start() | ||
Propagators.defaultPropagator().inject(child, carrier, setter) | ||
|
||
then: | ||
root.getSamplingPriority() == SAMPLER_KEEP as int | ||
child.getSamplingPriority() == root.getSamplingPriority() | ||
1 * setter.set(carrier, DatadogHttpCodec.SAMPLING_PRIORITY_KEY, String.valueOf(SAMPLER_KEEP)) | ||
|
||
cleanup: | ||
child.finish() | ||
root.finish() | ||
tracer.close() | ||
} | ||
|
||
def 'span priority only set after first injection'() { | ||
given: | ||
def sampler = new ControllableSampler() | ||
def tracer = tracerBuilder().writer(new LoggingWriter()).sampler(sampler).build() | ||
def setter = Mock(AgentPropagation.Setter) | ||
def carrier = new Object() | ||
|
||
when: | ||
def root = tracer.buildSpan('test', 'parent').start() | ||
def child = tracer.buildSpan('test', 'child').asChildOf(root).start() | ||
Propagators.defaultPropagator().inject(child, carrier, setter) | ||
|
||
then: | ||
root.getSamplingPriority() == SAMPLER_KEEP as int | ||
child.getSamplingPriority() == root.getSamplingPriority() | ||
1 * setter.set(carrier, DatadogHttpCodec.SAMPLING_PRIORITY_KEY, String.valueOf(SAMPLER_KEEP)) | ||
|
||
when: | ||
sampler.nextSamplingPriority = PrioritySampling.SAMPLER_DROP as int | ||
def child2 = tracer.buildSpan('test', 'child2').asChildOf(root).start() | ||
Propagators.defaultPropagator().inject(child2, carrier, setter) | ||
|
||
then: | ||
root.getSamplingPriority() == SAMPLER_KEEP as int | ||
child.getSamplingPriority() == root.getSamplingPriority() | ||
child2.getSamplingPriority() == root.getSamplingPriority() | ||
1 * setter.set(carrier, DatadogHttpCodec.SAMPLING_PRIORITY_KEY, String.valueOf(SAMPLER_KEEP)) | ||
|
||
cleanup: | ||
child.finish() | ||
child2.finish() | ||
root.finish() | ||
tracer.close() | ||
} | ||
|
||
def 'injection does not override set priority'() { | ||
given: | ||
def sampler = new ControllableSampler() | ||
def tracer = tracerBuilder().writer(new LoggingWriter()).sampler(sampler).build() | ||
def setter = Mock(AgentPropagation.Setter) | ||
def carrier = new Object() | ||
|
||
when: | ||
def root = tracer.buildSpan('test', 'root').start() | ||
def child = tracer.buildSpan('test', 'child').asChildOf(root).start() | ||
child.setSamplingPriority(USER_DROP) | ||
Propagators.defaultPropagator().inject(child, carrier, setter) | ||
|
||
then: | ||
root.getSamplingPriority() == USER_DROP as int | ||
child.getSamplingPriority() == root.getSamplingPriority() | ||
1 * setter.set(carrier, DatadogHttpCodec.SAMPLING_PRIORITY_KEY, String.valueOf(USER_DROP)) | ||
|
||
cleanup: | ||
child.finish() | ||
root.finish() | ||
tracer.close() | ||
} | ||
} |
This file contains 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -10,12 +10,42 @@ | |||||
import datadog.trace.api.gateway.IGSpanInfo; | ||||||
import datadog.trace.api.gateway.RequestContext; | ||||||
import datadog.trace.api.interceptor.MutableSpan; | ||||||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentSpan; | ||||||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopContext; | ||||||
import java.util.Map; | ||||||
import javax.annotation.Nonnull; | ||||||
import javax.annotation.Nullable; | ||||||
|
||||||
public interface AgentSpan | ||||||
extends MutableSpan, ImplicitContextKeyed, Context, IGSpanInfo, WithAgentSpan { | ||||||
|
||||||
/** | ||||||
* Extracts the span from context. | ||||||
* | ||||||
* @param context the context to extract the span from. | ||||||
* @return the span if existing, {@code null} otherwise. | ||||||
*/ | ||||||
static AgentSpan fromContext(Context context) { | ||||||
return context.get(SPAN_KEY); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Creates a span wrapper from a span context. | ||||||
* | ||||||
* <p>Creating a such span will not create a tracing span to complete a local root trace. It gives | ||||||
* a span instance based on a span context for span-based API. It is usually used with an | ||||||
* extracted span context as parameter to represent a remove span. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* @param spanContext the span context to get a full-fledged span. | ||||||
* @return a span wrapped based on a span context. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
static AgentSpan fromSpanContext(AgentSpanContext spanContext) { | ||||||
if (spanContext == null || spanContext == NoopContext.INSTANCE) { | ||||||
return NoopAgentSpan.INSTANCE; | ||||||
} | ||||||
return new AgentTracer.ExtractedSpan(spanContext); | ||||||
} | ||||||
|
||||||
DDTraceId getTraceId(); | ||||||
|
||||||
long getSpanId(); | ||||||
|
@@ -163,13 +193,13 @@ default Context storeInto(Context context) { | |||||
|
||||||
@Nullable | ||||||
@Override | ||||||
default <T> T get(ContextKey<T> key) { | ||||||
default <T> T get(@Nonnull ContextKey<T> key) { | ||||||
// noinspection unchecked | ||||||
return SPAN_KEY == key ? (T) this : Context.root().get(key); | ||||||
} | ||||||
|
||||||
@Override | ||||||
default <T> Context with(ContextKey<T> key, @Nullable T value) { | ||||||
default <T> Context with(@Nonnull ContextKey<T> key, @Nullable T value) { | ||||||
return Context.root().with(SPAN_KEY, this, key, value); | ||||||
} | ||||||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.