Skip to content

Commit eff2a4b

Browse files
jpbempelsarahchen6
andauthored
Add rate limit on Span decoration probe errors (#10529)
Add rate limit on Span decoration probe errors if an evaluation error is generated by a span decoration probe we are rate limiting it Co-authored-by: sarah.chen <sarah.chen@datadoghq.com>
1 parent 13b6001 commit eff2a4b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/SpanDecorationProbe.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import datadog.trace.bootstrap.debugger.MethodLocation;
1919
import datadog.trace.bootstrap.debugger.ProbeId;
2020
import datadog.trace.bootstrap.debugger.ProbeImplementation;
21+
import datadog.trace.bootstrap.debugger.ProbeRateLimiter;
2122
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
2223
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
2324
import datadog.trace.util.TagsHelper;
@@ -294,6 +295,10 @@ private void handleEvaluationErrors(SpanDecorationStatus status) {
294295
if (status.getErrors().isEmpty()) {
295296
return;
296297
}
298+
boolean sampled = ProbeRateLimiter.tryProbe(id);
299+
if (!sampled) {
300+
return;
301+
}
297302
Snapshot snapshot = new Snapshot(Thread.currentThread(), this, -1);
298303
snapshot.addEvaluationErrors(status.getErrors());
299304
DebuggerAgent.getSink().addSnapshot(snapshot);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/SpanDecorationProbeInstrumentationTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import datadog.trace.bootstrap.debugger.MethodLocation;
4242
import datadog.trace.bootstrap.debugger.ProbeId;
4343
import datadog.trace.bootstrap.debugger.ProbeImplementation;
44+
import datadog.trace.bootstrap.debugger.ProbeRateLimiter;
4445
import datadog.trace.bootstrap.debugger.util.Redaction;
4546
import datadog.trace.core.CoreTracer;
4647
import java.io.IOException;
@@ -78,6 +79,7 @@ public void setUp() {
7879
public void after() {
7980
super.after();
8081
Redaction.clearUserDefinedTypes();
82+
ProbeRateLimiter.resetAll();
8183
}
8284

8385
@Test
@@ -188,6 +190,11 @@ public void methodTagEvalError() throws IOException, URISyntaxException {
188190
assertNull(span.getTags().get("tag1"));
189191
assertEquals(
190192
"Cannot dereference field: noarg", span.getTags().get("_dd.di.tag1.evaluation_error"));
193+
assertEquals(1, mockSink.getSnapshots().size());
194+
Snapshot snapshot = mockSink.getSnapshots().get(0);
195+
assertEquals(1, snapshot.getEvaluationErrors().size());
196+
assertEquals(
197+
"Cannot dereference field: noarg", snapshot.getEvaluationErrors().get(0).getMessage());
191198
}
192199

193200
@Test
@@ -588,6 +595,43 @@ public void typeRedactionConditions() throws IOException, URISyntaxException {
588595
snapshot.getEvaluationErrors().get(2).getMessage());
589596
}
590597

598+
@Test
599+
public void ensureCallingSamplingTagEvalError() throws IOException, URISyntaxException {
600+
doSamplingTest(this::methodTagEvalError, 1, 1);
601+
}
602+
603+
@Test
604+
public void ensureCallingSamplingMethodInvalidCondition() throws IOException, URISyntaxException {
605+
doSamplingTest(this::methodActiveSpanInvalidCondition, 1, 1);
606+
}
607+
608+
@Test
609+
public void ensureCallingSamplingLineInvalidCondition() throws IOException, URISyntaxException {
610+
doSamplingTest(this::lineActiveSpanInvalidCondition, 1, 1);
611+
}
612+
613+
@Test
614+
public void ensureCallingSamplingKeywordRedactionConditions()
615+
throws IOException, URISyntaxException {
616+
doSamplingTest(this::keywordRedactionConditions, 1, 1);
617+
}
618+
619+
private void doSamplingTest(
620+
CapturingTestBase.TestMethod testRun, int expectedGlobalCount, int expectedProbeCount)
621+
throws IOException, URISyntaxException {
622+
MockSampler probeSampler = new MockSampler();
623+
MockSampler globalSampler = new MockSampler();
624+
ProbeRateLimiter.setSamplerSupplier(rate -> rate < 101 ? probeSampler : globalSampler);
625+
ProbeRateLimiter.setGlobalSnapshotRate(1000);
626+
try {
627+
testRun.run();
628+
} finally {
629+
ProbeRateLimiter.setSamplerSupplier(null);
630+
}
631+
assertEquals(expectedGlobalCount, globalSampler.getCallCount());
632+
assertEquals(expectedProbeCount, probeSampler.getCallCount());
633+
}
634+
591635
private SpanDecorationProbe.Decoration createDecoration(String tagName, String valueDsl) {
592636
List<SpanDecorationProbe.Tag> tags =
593637
asList(

0 commit comments

Comments
 (0)