Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 352
Introduce tracing propagator#8313
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } |
| 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() | ||
| } | ||
| } |
| 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. | ||||||
Contributor 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. | ||||||
Contributor 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); | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.
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.