Skip to content

Commit aa8fc13

Browse files
authored
Combine play spans with surrounding framework context before attaching them (#10554)
Add AgentSpan.attachWithCurrent() which attaches a combination of the span with the current context to the current execution unit Combine play spans with surrounding custom context before attaching them Clarify difference between AgentSpan.attach and AgentSpan.attachWithCurrent Review feedback Merge remote-tracking branch 'origin/master' into mcculls/merge-play-spans-with-framework-context Co-authored-by: stuart.mcculloch <stuart.mcculloch@datadoghq.com>
1 parent 940ffd0 commit aa8fc13

6 files changed

Lines changed: 31 additions & 7 deletions

File tree

dd-java-agent/instrumentation/play/play-2.3/src/main/java/datadog/trace/instrumentation/play23/PlayAdvice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static ContextScope onEnter(@Advice.Argument(0) final Request<?> req) {
3434
// Do not extract the context.
3535
span = startSpan("play", PLAY_REQUEST);
3636
span.setMeasured(true);
37-
scope = span.attach();
37+
scope = span.attachWithCurrent();
3838
}
3939

4040
DECORATE.afterStart(span);

dd-java-agent/instrumentation/play/play-2.4/src/main/java/datadog/trace/instrumentation/play24/PlayAdvice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static ContextScope onEnter(@Advice.Argument(value = 0, readOnly = false)
4040
// Do not extract the context.
4141
span = startSpan("play", PLAY_REQUEST);
4242
span.setMeasured(true);
43-
scope = span.attach();
43+
scope = span.attachWithCurrent();
4444
}
4545

4646
DECORATE.afterStart(span);

dd-java-agent/instrumentation/play/play-2.6/src/main/java/datadog/trace/instrumentation/play26/PlayAdvice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static ContextScope onEnter(@Advice.Argument(value = 0, readOnly = false)
4141
// Do not extract the context.
4242
parentContext = getRootContext();
4343
span = startSpan("play", PLAY_REQUEST);
44-
scope = span.attach();
44+
scope = span.attachWithCurrent();
4545
}
4646

4747
span.setMeasured(true);

internal-api/src/main/java/datadog/trace/api/datastreams/DataStreamsContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import datadog.context.Context;
77
import datadog.context.ContextKey;
88
import datadog.context.ImplicitContextKeyed;
9+
import javax.annotation.Nonnull;
910

1011
public class DataStreamsContext implements ImplicitContextKeyed {
1112
private static final ContextKey<DataStreamsContext> CONTEXT_KEY =
@@ -102,7 +103,7 @@ public boolean sendCheckpoint() {
102103
}
103104

104105
@Override
105-
public Context storeInto(Context context) {
106+
public Context storeInto(@Nonnull Context context) {
106107
return context.with(CONTEXT_KEY, this);
107108
}
108109
}

internal-api/src/main/java/datadog/trace/api/gateway/InferredProxySpan.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import javax.annotation.Nonnull;
2122

2223
public class InferredProxySpan implements ImplicitContextKeyed {
2324
private static final ContextKey<InferredProxySpan> CONTEXT_KEY = named("inferred-proxy-key");
@@ -131,7 +132,7 @@ public void finish() {
131132
}
132133

133134
@Override
134-
public Context storeInto(Context context) {
135+
public Context storeInto(@Nonnull Context context) {
135136
return context.with(CONTEXT_KEY, this);
136137
}
137138
}

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentSpan.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datadog.context.Context;
66
import datadog.context.ContextKey;
7+
import datadog.context.ContextScope;
78
import datadog.context.ImplicitContextKeyed;
89
import datadog.trace.api.DDSpanId;
910
import datadog.trace.api.DDTraceId;
@@ -209,8 +210,8 @@ default void copyPropagationAndBaggage(final AgentSpan source) {
209210
}
210211

211212
@Override
212-
default Context storeInto(Context context) {
213-
return context.with(SPAN_KEY, this);
213+
default Context storeInto(@Nonnull Context context) {
214+
return context == Context.root() ? this : context.with(SPAN_KEY, this);
214215
}
215216

216217
@Nullable
@@ -224,4 +225,25 @@ default <T> T get(@Nonnull ContextKey<T> key) {
224225
default <T> Context with(@Nonnull ContextKey<T> key, @Nullable T value) {
225226
return SPAN_KEY == key ? (Context) value : Context.root().with(SPAN_KEY, this, key, value);
226227
}
228+
229+
/**
230+
* Attaches a context containing just the span to the current execution unit. Use this when you
231+
* want to temporarily suppress any surrounding custom context during the span's scope.
232+
*
233+
* @return a scope to be closed when the span context is invalid.
234+
*/
235+
@Override
236+
default ContextScope attach() {
237+
return Context.super.attach();
238+
}
239+
240+
/**
241+
* Attaches a context combining the span with the current context to the current execution unit.
242+
* Use this when you want to maintain any surrounding custom context during the span's scope.
243+
*
244+
* @return a scope to be closed when the combined context is invalid.
245+
*/
246+
default ContextScope attachWithCurrent() {
247+
return storeInto(Context.current()).attach();
248+
}
227249
}

0 commit comments

Comments
 (0)