Skip to content
Open
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
@@ -1,5 +1,8 @@
package datadog.opentelemetry.shim.context;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.isAsyncPropagationEnabled;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled;

import datadog.opentelemetry.shim.baggage.OtelBaggage;
import datadog.opentelemetry.shim.trace.OtelSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -40,7 +43,13 @@ public static Context current() {

@Override
public Scope makeCurrent() {
return new OtelScope(Context.super.makeCurrent(), delegate.attach());
boolean asyncPropagation = isAsyncPropagationEnabled();
Scope scope = new OtelScope(Context.super.makeCurrent(), delegate.attach());
// Manual tracing inherits disabled propagation from the active scope.
if (!asyncPropagation) {
setAsyncPropagationEnabled(false);
}
return scope;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TraceMatcher.trace;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.isAsyncPropagationEnabled;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -22,9 +24,39 @@
import opentelemetry14.AbstractOpenTelemetry14Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.tabletest.junit.TableTest;

public class ContextTest extends AbstractOpenTelemetry14Test {

@Test
void firstManualAttachmentDefaultsToAsyncPropagation() {
Context context = Context.root().with(ContextKey.named("test-key"), "value");
try (Scope ignored = context.makeCurrent()) {
assertTrue(isAsyncPropagationEnabled());
}
}

@TableTest({
"Scenario | asyncPropagation | sameContext",
"Enabled parent, new context | true | false ",
"Disabled parent, new context | false | false ",
"Enabled parent, current context | true | true ",
"Disabled parent, current context | false | true "
})
void manualAttachmentInheritsAsyncPropagation(boolean asyncPropagation, boolean sameContext) {
ContextKey<String> key = ContextKey.named("test-key");
Context parent = Context.root().with(key, "parent");
try (Scope ignored = parent.makeCurrent()) {
setAsyncPropagationEnabled(asyncPropagation);
Context child = sameContext ? parent : parent.with(key, "child");
try (Scope ignored2 = child.makeCurrent()) {
assertEquals(asyncPropagation, isAsyncPropagationEnabled());
}
assertEquals(asyncPropagation, isAsyncPropagationEnabled());
}
assertTrue(isAsyncPropagationEnabled());
}

@DisplayName("test Span.current/makeCurrent()")
@Test
void testSpanCurrentMakeCurrent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ private ContextScope activate(final Context context) {

assert context != null;

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

final ContinuableScope scope =
new ContinuableScope(this, context, CONTEXT, asyncPropagation, createScopeState(context));
new ContinuableScope(
this, context, CONTEXT, DEFAULT_ASYNC_PROPAGATING, createScopeState(context));
scopeStack.push(scope);
healthMetrics.onActivateScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ void noActiveScopeDefaultsToAsyncPropagation() {
}
}

@Test
void attachingNewContextResetsAsyncPropagation() {
ContextKey<String> key = ContextKey.named("test-key");
Context parent = Context.root().with(key, "parent");
try (ContextScope ignored = parent.attach()) {
tracer.setAsyncPropagationEnabled(false);
try (ContextScope ignored2 = parent.with(key, "child").attach()) {
assertTrue(tracer.isAsyncPropagationEnabled());
}
assertFalse(tracer.isAsyncPropagationEnabled());
}
assertTrue(tracer.isAsyncPropagationEnabled());
}

@Test
void attachingTheCurrentContextPreservesAsyncPropagation() {
Context context = Context.root().with(ContextKey.named("test-key"), "value");
try (ContextScope ignored = context.attach()) {
tracer.setAsyncPropagationEnabled(false);
try (ContextScope ignored2 = context.attach()) {
assertFalse(tracer.isAsyncPropagationEnabled());
}
assertFalse(tracer.isAsyncPropagationEnabled());
}
assertTrue(tracer.isAsyncPropagationEnabled());
}

@Test
void nonDdspanActivationResultsInAContinuableScope() {
ContextScope scope = scopeManager.activateSpan(noopSpan());
Expand Down
Loading