Skip to content

Commit cfdacad

Browse files
committed
Migrate simple try-with-resources use of AgentScope to ContextScope
1 parent e292db6 commit cfdacad

88 files changed

Lines changed: 305 additions & 284 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dd-java-agent/agent-aiguard/src/main/java/com/datadog/aiguard/AIGuardInternal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.squareup.moshi.Moshi;
1212
import com.squareup.moshi.Types;
1313
import datadog.communication.http.OkHttpUtils;
14+
import datadog.context.ContextScope;
1415
import datadog.trace.api.Config;
1516
import datadog.trace.api.aiguard.AIGuard;
1617
import datadog.trace.api.aiguard.AIGuard.AIGuardAbortError;
@@ -26,7 +27,6 @@
2627
import datadog.trace.api.aiguard.noop.NoOpEvaluator;
2728
import datadog.trace.api.gateway.RequestContext;
2829
import datadog.trace.api.telemetry.WafMetricCollector;
29-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
3030
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
3131
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
3232
import datadog.trace.bootstrap.instrumentation.api.ClientIpAddressData;
@@ -281,7 +281,7 @@ public Evaluation evaluate(final List<Message> messages, final Options options)
281281
// sure client IP tags were populated.
282282
copyAnomalyDetectionTags(span, localRootSpan);
283283
}
284-
try (final AgentScope scope = tracer.activateSpan(span)) {
284+
try (final ContextScope scope = tracer.activateSpan(span)) {
285285
final Message last = messages.get(messages.size() - 1);
286286
if (isToolCall(last)) {
287287
span.setTag(TARGET_TAG, "tool");

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/httpurlconnection/HttpUrlState.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import static datadog.trace.bootstrap.instrumentation.httpurlconnection.HttpUrlConnectionDecorator.DECORATE;
66
import static datadog.trace.bootstrap.instrumentation.httpurlconnection.HttpUrlConnectionDecorator.HTTP_URL_CONNECTION;
77

8+
import datadog.context.ContextScope;
89
import datadog.trace.bootstrap.ContextStore;
9-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1010
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1111
import java.net.HttpURLConnection;
1212

@@ -18,7 +18,7 @@ public class HttpUrlState {
1818

1919
public AgentSpan start(final HttpURLConnection connection) {
2020
span = startSpan(HTTP_URL_CONNECTION.toString(), DECORATE.operationName());
21-
try (final AgentScope scope = activateSpan(span)) {
21+
try (final ContextScope scope = activateSpan(span)) {
2222
DECORATE.afterStart(span);
2323
DECORATE.onRequest(span, connection);
2424
return span;
@@ -39,7 +39,7 @@ public void finish() {
3939

4040
public void finishSpan(
4141
final HttpURLConnection connection, final int responseCode, final Throwable throwable) {
42-
try (final AgentScope scope = activateSpan(span)) {
42+
try (final ContextScope scope = activateSpan(span)) {
4343
if (responseCode > 0) {
4444
// safe to access response data as 'responseCode' is set
4545
DECORATE.onResponse(span, connection);
@@ -62,7 +62,7 @@ public void finishSpan(final HttpURLConnection connection, final int responseCod
6262
* (e.g. breaks getOutputStream).
6363
*/
6464
if (responseCode > 0) {
65-
try (final AgentScope scope = activateSpan(span)) {
65+
try (final ContextScope scope = activateSpan(span)) {
6666
// safe to access response data as 'responseCode' is set
6767
DECORATE.onResponse(span, connection);
6868
DECORATE.beforeFinish(span);

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/concurrent/Wrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE;
66
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.exclude;
77

8+
import datadog.context.ContextScope;
89
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
910
import java.util.concurrent.RunnableFuture;
1011

@@ -43,7 +44,7 @@ public Wrapper(T delegate, AgentScope.Continuation continuation) {
4344

4445
@Override
4546
public void run() {
46-
try (AgentScope scope = activate()) {
47+
try (ContextScope scope = activate()) {
4748
delegate.run();
4849
}
4950
}

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import com.datadog.debugger.sink.DebuggerSink;
1717
import com.datadog.debugger.sink.ProbeStatusSink;
1818
import com.datadog.debugger.sink.Snapshot;
19+
import datadog.context.ContextScope;
1920
import datadog.trace.api.Config;
2021
import datadog.trace.api.IdGenerationStrategy;
2122
import datadog.trace.bootstrap.debugger.CapturedContext;
2223
import datadog.trace.bootstrap.debugger.EvaluationError;
2324
import datadog.trace.bootstrap.debugger.MethodLocation;
2425
import datadog.trace.bootstrap.debugger.ProbeId;
2526
import datadog.trace.bootstrap.debugger.ProbeRateLimiter;
26-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2727
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
2828
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
2929
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.TracerAPI;
@@ -136,7 +136,7 @@ private int runTrace(TracerAPI tracer, boolean captureSnapshot, Integer line, St
136136
if (sessionId != null) {
137137
span.setTag(Tags.PROPAGATED_DEBUG, sessionId + ":1");
138138
}
139-
try (AgentScope scope = tracer.activateManualSpan(span)) {
139+
try (ContextScope scope = tracer.activateManualSpan(span)) {
140140
Builder builder =
141141
createLog("Budget testing").probeId(ProbeId.newId()).captureSnapshot(captureSnapshot);
142142
if (sessionId != null) {
@@ -177,7 +177,7 @@ private boolean fillSnapshot(DebugSessionStatus status) {
177177
CoreTracer.builder().idGenerationStrategy(IdGenerationStrategy.fromName("random")).build();
178178
AgentTracer.registerIfAbsent(tracer);
179179
AgentSpan span = tracer.startSpan("log probe debug session testing", "test span");
180-
try (AgentScope scope = tracer.activateManualSpan(span)) {
180+
try (ContextScope scope = tracer.activateManualSpan(span)) {
181181
if (status == DebugSessionStatus.ACTIVE) {
182182
span.setTag(Tags.PROPAGATED_DEBUG, DEBUG_SESSION_ID + ":1");
183183
} else if (status == DebugSessionStatus.DISABLED) {

dd-java-agent/agent-debugger/src/test/resources/com/datadog/debugger/CapturedSnapshot20.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.datadog.debugger;
22

3+
import datadog.context.ContextScope;
34
import datadog.trace.agent.tooling.TracerInstaller;
4-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
66
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
77
import datadog.trace.core.CoreTracer;
@@ -25,7 +25,7 @@ public class CapturedSnapshot20 {
2525
public static int main(String arg) {
2626
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
2727
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "process").start();
28-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
28+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
2929
if (arg.equals("exception") || arg.equals("illegal")) {
3030
return new CapturedSnapshot20().processWithException(arg);
3131
}

dd-java-agent/agent-debugger/src/test/resources/com/datadog/debugger/CapturedSnapshot21.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.datadog.debugger;
22

3+
import datadog.context.ContextScope;
34
import datadog.trace.agent.tooling.TracerInstaller;
4-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
66
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
77
import datadog.trace.core.CoreTracer;
@@ -25,7 +25,7 @@ public class CapturedSnapshot21 {
2525
public static int main(String arg) {
2626
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
2727
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "rootProcess").start();
28-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
28+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
2929
return new CapturedSnapshot21().rootProcess(arg);
3030
} finally {
3131
span.finish();
@@ -35,7 +35,7 @@ public static int main(String arg) {
3535
private int rootProcess(String arg) {
3636
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
3737
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "process1").start();
38-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
38+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
3939
return process1(arg) + 1;
4040
} finally {
4141
span.finish();
@@ -45,7 +45,7 @@ private int rootProcess(String arg) {
4545
private int process1(String arg) {
4646
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
4747
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "process2").start();
48-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
48+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
4949
return process2(arg) + 1;
5050
} finally {
5151
span.finish();
@@ -55,7 +55,7 @@ private int process1(String arg) {
5555
private int process2(String arg) {
5656
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
5757
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "process3").start();
58-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
58+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
5959
return process3(arg) + 1;
6060
} finally {
6161
span.finish();

dd-java-agent/agent-debugger/src/test/resources/com/datadog/debugger/CapturedSnapshot28.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.datadog.debugger;
22

3+
import datadog.context.ContextScope;
34
import datadog.trace.agent.tooling.TracerInstaller;
4-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
66
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
77
import datadog.trace.core.CoreTracer;
@@ -26,7 +26,7 @@ public class CapturedSnapshot28 {
2626
public static int main(String arg) {
2727
AgentTracer.TracerAPI tracerAPI = AgentTracer.get();
2828
AgentSpan span = tracerAPI.buildSpan("dynamic-instrumentation", "process").start();
29-
try (AgentScope scope = tracerAPI.activateManualSpan(span)) {
29+
try (ContextScope scope = tracerAPI.activateManualSpan(span)) {
3030
return new CapturedSnapshot28().process(arg);
3131
} finally {
3232
span.finish();

dd-java-agent/agent-iast/src/main/java/com/datadog/iast/Reporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.datadog.iast.model.Vulnerability;
88
import com.datadog.iast.model.VulnerabilityBatch;
99
import com.datadog.iast.taint.TaintedObjects;
10+
import datadog.context.ContextScope;
1011
import datadog.trace.api.Config;
1112
import datadog.trace.api.ProductTraceSource;
1213
import datadog.trace.api.gateway.RequestContext;
@@ -61,7 +62,7 @@ public void report(@Nullable final AgentSpan span, @Nonnull final Vulnerability
6162
}
6263
if (span == null) {
6364
final AgentSpan newSpan = startNewSpan();
64-
try (final AgentScope autoClosed = tracer().activateManualSpan(newSpan)) {
65+
try (final ContextScope autoClosed = tracer().activateManualSpan(newSpan)) {
6566
vulnerability.updateSpan(newSpan);
6667
reportVulnerability(newSpan, vulnerability);
6768
} finally {

dd-java-agent/instrumentation/aerospike-4.0/src/main/java/datadog/trace/instrumentation/aerospike4/TracingListener.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.aerospike.client.listener.RecordListener;
1818
import com.aerospike.client.listener.RecordSequenceListener;
1919
import com.aerospike.client.listener.WriteListener;
20+
import datadog.context.ContextScope;
2021
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2122
import datadog.trace.bootstrap.instrumentation.api.AgentScope.Continuation;
2223
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
@@ -73,7 +74,7 @@ public void onSuccess(final Key key, final boolean exists) {
7374
clientSpan.finish();
7475

7576
if (listener != null) {
76-
try (final AgentScope scope = continuation.activate()) {
77+
try (final ContextScope scope = continuation.activate()) {
7778
if (listener instanceof ExistsListener) {
7879
((ExistsListener) listener).onSuccess(key, exists);
7980
} else if (listener instanceof DeleteListener) {
@@ -91,7 +92,7 @@ public void onSuccess(final Key[] keys, final boolean[] exists) {
9192
clientSpan.finish();
9293

9394
if (listener != null) {
94-
try (final AgentScope scope = continuation.activate()) {
95+
try (final ContextScope scope = continuation.activate()) {
9596
((ExistsArrayListener) listener).onSuccess(keys, exists);
9697
}
9798
} else {
@@ -105,7 +106,7 @@ public void onSuccess(final Key key, final Record record) {
105106
clientSpan.finish();
106107

107108
if (listener != null) {
108-
try (final AgentScope scope = continuation.activate()) {
109+
try (final ContextScope scope = continuation.activate()) {
109110
((RecordListener) listener).onSuccess(key, record);
110111
}
111112
} else {
@@ -119,7 +120,7 @@ public void onSuccess(final Key[] keys, final Record[] records) {
119120
clientSpan.finish();
120121

121122
if (listener != null) {
122-
try (final AgentScope scope = continuation.activate()) {
123+
try (final ContextScope scope = continuation.activate()) {
123124
((RecordArrayListener) listener).onSuccess(keys, records);
124125
}
125126
} else {
@@ -133,7 +134,7 @@ public void onSuccess(final List<BatchRead> records) {
133134
clientSpan.finish();
134135

135136
if (listener != null) {
136-
try (final AgentScope scope = continuation.activate()) {
137+
try (final ContextScope scope = continuation.activate()) {
137138
((BatchListListener) listener).onSuccess(records);
138139
}
139140
} else {
@@ -147,7 +148,7 @@ public void onSuccess(final Key key) {
147148
clientSpan.finish();
148149

149150
if (listener != null) {
150-
try (final AgentScope scope = continuation.activate()) {
151+
try (final ContextScope scope = continuation.activate()) {
151152
((WriteListener) listener).onSuccess(key);
152153
}
153154
} else {
@@ -161,7 +162,7 @@ public void onSuccess(final Key key, final Object obj) {
161162
clientSpan.finish();
162163

163164
if (listener != null) {
164-
try (final AgentScope scope = continuation.activate()) {
165+
try (final ContextScope scope = continuation.activate()) {
165166
((ExecuteListener) listener).onSuccess(key, obj);
166167
}
167168
} else {
@@ -175,7 +176,7 @@ public void onSuccess() {
175176
clientSpan.finish();
176177

177178
if (listener != null) {
178-
try (final AgentScope scope = continuation.activate()) {
179+
try (final ContextScope scope = continuation.activate()) {
179180
if (listener instanceof ExistsSequenceListener) {
180181
((ExistsSequenceListener) listener).onSuccess();
181182
} else if (listener instanceof RecordSequenceListener) {
@@ -196,7 +197,7 @@ public void onFailure(final AerospikeException error) {
196197
clientSpan.finish();
197198

198199
if (listener != null) {
199-
try (final AgentScope scope = continuation.activate()) {
200+
try (final ContextScope scope = continuation.activate()) {
200201
if (listener instanceof ExistsListener) {
201202
((ExistsListener) listener).onFailure(error);
202203
} else if (listener instanceof ExistsSequenceListener) {

dd-java-agent/instrumentation/akka/akka-actor-2.5/src/test/java/LinearTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
33

44
import akka.dispatch.forkjoin.RecursiveTask;
5-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
5+
import datadog.context.ContextScope;
66
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
77

88
public class LinearTask extends RecursiveTask<Integer> {
@@ -32,7 +32,7 @@ protected Integer compute() {
3232
} else {
3333
int next = parent + 1;
3434
AgentSpan span = startSpan("test", Integer.toString(next));
35-
try (AgentScope scope = activateSpan(span)) {
35+
try (ContextScope scope = activateSpan(span)) {
3636
LinearTask child = new LinearTask(next, depth);
3737
return child.fork().join();
3838
} finally {

0 commit comments

Comments
 (0)