Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public ContextScope attach(Context context) {
Context[] holder = CURRENT_HOLDER.get();
Context previous = holder[0];
holder[0] = context;

System.out.println("ThreadLocalContextManager");
return new ContextScope() {
private boolean closed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor
// Run the multiple extractions on cache
for (Propagator propagator : this.propagators) {
context = propagator.extract(context, carrierCache, carrierCache);
System.out.println("Propagator: " + propagator + " context: " + context);
}
System.out.println("Context after composite propagator: " + context);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;

import datadog.appsec.api.blocking.BlockingException;
import datadog.context.Context;
import datadog.context.propagation.Propagators;
import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.function.TriConsumer;
Expand Down Expand Up @@ -132,6 +134,16 @@ public AgentSpanContext.Extracted extract(REQUEST_CARRIER carrier) {
return extractContextAndGetSpanContext(carrier, getter);
}

public Context extract(REQUEST_CARRIER carrier, boolean temp) {
AgentPropagation.ContextVisitor<REQUEST_CARRIER> getter = getter();
if (null == carrier || null == getter) {
return null;
}
Context context = Propagators.defaultPropagator().extract(Context.root(), carrier, getter);
System.out.println("context in extract: " + context);
return context;
}

/** Deprecated. Use {@link #startSpan(String, Object, AgentSpanContext.Extracted)} instead. */
@Deprecated
public AgentSpan startSpan(REQUEST_CARRIER carrier, AgentSpanContext.Extracted context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import akka.http.scaladsl.HttpExt;
import akka.http.scaladsl.model.HttpRequest;
import akka.http.scaladsl.model.HttpResponse;
import datadog.context.Context;
import datadog.trace.api.datastreams.DataStreamsContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand All @@ -30,7 +31,7 @@ public static AgentScope methodEnter(

if (request != null) {
DataStreamsContext dsmContext = DataStreamsContext.fromTags(CLIENT_PATHWAY_EDGE_TAGS);
defaultPropagator().inject(span.with(dsmContext), request, headers);
defaultPropagator().inject(Context.current().with(span).with(dsmContext), request, headers);
// Request is immutable, so we have to assign new value once we update headers
request = headers.getRequest();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import static datadog.trace.instrumentation.okhttp3.OkHttpClientDecorator.OKHTTP_REQUEST;
import static datadog.trace.instrumentation.okhttp3.RequestBuilderInjectAdapter.SETTER;

import datadog.context.Context;
import datadog.trace.api.datastreams.DataStreamsContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Baggage;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
Expand All @@ -23,15 +25,30 @@ public Response intercept(final Chain chain) throws IOException {
return chain.proceed(chain.request());
}

System.out.println("before activateSpan: " + Context.current().getClass());
final AgentSpan span = startSpan("okhttp", OKHTTP_REQUEST);

try (final AgentScope scope = activateSpan(span)) {
System.out.println("AgentScope: " + scope.getClass());
System.out.println("scope.context(): " + scope.context().getClass());
System.out.print("Context.current(): ");
System.out.println(Context.current() == Context.root());
System.out.println(Context.current().getClass());
DECORATE.afterStart(span);
DECORATE.onRequest(span, chain.request());

final Request.Builder requestBuilder = chain.request().newBuilder();
DataStreamsContext dsmContext = DataStreamsContext.fromTags(CLIENT_PATHWAY_EDGE_TAGS);
defaultPropagator().inject(span.with(dsmContext), requestBuilder, SETTER);

Baggage baggage = Baggage.fromContext(Context.current());
if (baggage != null) {
System.out.println("Baggage: " + baggage.getW3cHeader());
} else {
System.out.println("null baggage");
}
System.out.println("span: " + span);
System.out.println("span.with(baggage): " + span.with(baggage));
defaultPropagator()
.inject(Context.current().with(span).with(dsmContext), requestBuilder, SETTER);

final Response response;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.context.Context;
import datadog.trace.agent.tooling.ExcludeFilterProvider;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
Expand Down Expand Up @@ -122,15 +123,27 @@ public static AgentScope onService(@Advice.Argument(0) org.apache.coyote.Request
return activateSpan((AgentSpan) existingSpan);
}

final AgentSpanContext.Extracted extractedContext = DECORATE.extract(req);
req.setAttribute(DD_EXTRACTED_CONTEXT_ATTRIBUTE, extractedContext);
final Context extractedContext = DECORATE.extract(req, true);
AgentSpan extractedSpan = AgentSpan.fromContext(extractedContext);
AgentSpanContext.Extracted extractedSpanContext =
extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();

req.setAttribute(DD_EXTRACTED_CONTEXT_ATTRIBUTE, extractedSpanContext);

final AgentSpan span = DECORATE.startSpan(req, extractedSpanContext);

System.out.println("ExtractedContext: " + extractedContext);
extractedContext.with(span);
final AgentScope scope = (AgentScope) extractedContext.attach();
System.out.print("after attach: ");
System.out.println("Scope: " + scope);
System.out.println(Context.current().getClass());

final AgentSpan span = DECORATE.startSpan(req, extractedContext);
final AgentScope scope = activateSpan(span);
// This span is finished when Request.recycle() is called by RequestInstrumentation.
DECORATE.afterStart(span);

req.setAttribute(DD_SPAN_ATTRIBUTE, span);

req.setAttribute(CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId());
req.setAttribute(CorrelationIdentifier.getSpanIdKey(), GlobalTracer.get().getSpanId());
return scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {
}

String headerValue = baggage.getW3cHeader();
if (headerValue != null) {
setter.set(carrier, BAGGAGE_KEY, headerValue);
return;
}
// if (headerValue != null) {
// System.out.println("Returning cached value immediately");
// setter.set(carrier, BAGGAGE_KEY, headerValue);
// return;
// }e

int processedItems = 0;
int currentBytes = 0;
Expand Down Expand Up @@ -162,6 +163,8 @@ private Map<String, String> parseBaggageHeaders(String input) {

@Override
public void accept(String key, String value) {
System.out.println("key: " + key + " ;value: " + value);

// Only process tags that are relevant to baggage
if (BAGGAGE_KEY.equalsIgnoreCase(key)) {
Map<String, String> baggage = parseBaggageHeaders(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public static Extractor createExtractor(
Config config, Supplier<TraceConfig> traceConfigSupplier) {
final List<Extractor> extractors = new ArrayList<>();
for (final TracePropagationStyle style : config.getTracePropagationStylesToExtract()) {
System.out.println("TracePropagationStyle: " + style);
switch (style) {
case DATADOG:
extractors.add(DatadogHttpCodec.newExtractor(config, traceConfigSupplier));
Expand Down Expand Up @@ -221,7 +222,8 @@ public <C> TagContext extract(
TagContext partialContext = null;
// Extract and cache all headers in advance
ExtractionCache<C> extractionCache = new ExtractionCache<>(carrier, getter);

System.out.println("extractors size: " + this.extractors.size());
System.out.println("extractors size: " + this.extractors);
for (final Extractor extractor : this.extractors) {
TagContext extracted = extractor.extract(extractionCache, extractionCache);
// Check if context is valid
Expand Down Expand Up @@ -260,7 +262,7 @@ else if (extracted != null && partialContext == null) {
partialContext = extracted;
}
}

System.out.println("CompoundExtractor context: " + context);
if (context != null) {
log.debug("Extract complete context {}", context);
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor
if (context == null || carrier == null || visitor == null) {
return context;
}
System.out.println("before httpcodec extract");
TagContext spanContext = this.extractor.extract(carrier, toContextVisitor(visitor));
System.out.println("span context post tracing extract: " + spanContext);
// If the extraction fails, return the original context
if (spanContext == null) {
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public final AgentSpan span() {

@Override
public Context context() {
System.out.println("Coming here");
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ private AgentScope activate(final Context context) {

// Inherit the async propagation from the active scope
boolean asyncPropagation = top != null ? top.isAsyncPropagating() : DEFAULT_ASYNC_PROPAGATING;

System.out.println("context in activate: " + context);
final ContinuableScope scope =
new ContinuableScope(this, context, CONTEXT, asyncPropagation, createScopeState(context));
scopeStack.push(scope);
healthMetrics.onActivateScope();

System.out.println("ContinuableScopeManager");
return scope;
}

Expand Down
6 changes: 4 additions & 2 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2356,11 +2356,13 @@ public boolean isTracePropagationExtractFirst() {
}

public boolean isBaggageExtract() {
return tracePropagationStylesToExtract.contains(TracePropagationStyle.BAGGAGE);
return tracePropagationStylesToExtract.contains(TracePropagationStyle.BAGGAGE)
&& tracePropagationBehaviorExtract != TracePropagationBehaviorExtract.IGNORE;
}

public boolean isBaggageInject() {
return tracePropagationStylesToInject.contains(TracePropagationStyle.BAGGAGE);
return tracePropagationStylesToInject.contains(TracePropagationStyle.BAGGAGE)
&& tracePropagationBehaviorExtract != TracePropagationBehaviorExtract.IGNORE;
}

public boolean isBaggagePropagationEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datadog.context.propagation.Propagators;
import java.util.function.BiConsumer;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.swing.*;

public final class AgentPropagation {
public static final Concern TRACING_CONCERN = named("tracing");
Expand Down Expand Up @@ -43,6 +44,7 @@ default void forEachKeyValue(C carrier, BiConsumer<String, String> visitor) {
forEachKey(
carrier,
(key, value) -> {
System.out.println("carrier key: " + key + " value: " + value);
visitor.accept(key, value);
return true;
});
Expand Down
Loading