Skip to content

Commit f701ba9

Browse files
authored
Use two budgets depending on type (#8283)
1 parent 5bd6139 commit f701ba9

File tree

4 files changed

+78
-19
lines changed

4 files changed

+78
-19
lines changed

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/DebuggerContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public String tag() {
3939
public String tag() {
4040
return "cause:debug session disabled";
4141
}
42+
},
43+
BUDGET {
44+
@Override
45+
public String tag() {
46+
return "cause:budget_exceeded";
47+
}
4248
};
4349

4450
public abstract String tag();

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

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

33
import static com.datadog.debugger.probe.LogProbe.Capture.toLimits;
4+
import static java.lang.String.format;
45

56
import com.datadog.debugger.agent.DebuggerAgent;
67
import com.datadog.debugger.agent.Generated;
@@ -62,7 +63,8 @@ public class LogProbe extends ProbeDefinition implements Sampled {
6263
private static final Limits LIMITS = new Limits(1, 3, 8192, 5);
6364
private static final int LOG_MSG_LIMIT = 8192;
6465

65-
public static final int PROBE_BUDGET = 10;
66+
public static final int CAPTURING_PROBE_BUDGET = 10;
67+
public static final int NON_CAPTURING_PROBE_BUDGET = 1000;
6668

6769
/** Stores part of a templated message either a str or an expression */
6870
public static class Segment {
@@ -575,14 +577,17 @@ public void commit(
575577
CapturedContext exitContext,
576578
List<CapturedContext.CapturedThrowable> caughtExceptions) {
577579
Snapshot snapshot = createSnapshot();
578-
boolean shouldCommit =
579-
inBudget() && fillSnapshot(entryContext, exitContext, caughtExceptions, snapshot);
580+
boolean shouldCommit = fillSnapshot(entryContext, exitContext, caughtExceptions, snapshot);
580581
DebuggerSink sink = DebuggerAgent.getSink();
581582
if (shouldCommit) {
582-
commitSnapshot(snapshot, sink);
583583
incrementBudget();
584-
if (snapshotProcessor != null) {
585-
snapshotProcessor.accept(snapshot);
584+
if (inBudget()) {
585+
commitSnapshot(snapshot, sink);
586+
if (snapshotProcessor != null) {
587+
snapshotProcessor.accept(snapshot);
588+
}
589+
} else {
590+
sink.skipSnapshot(id, DebuggerContext.SkipCause.BUDGET);
586591
}
587592
} else {
588593
sink.skipSnapshot(id, DebuggerContext.SkipCause.CONDITION);
@@ -866,7 +871,9 @@ public String toString() {
866871

867872
private boolean inBudget() {
868873
AtomicInteger budgetLevel = getBudgetLevel();
869-
return budgetLevel == null || budgetLevel.get() < PROBE_BUDGET;
874+
return budgetLevel == null
875+
|| budgetLevel.get()
876+
<= (captureSnapshot ? CAPTURING_PROBE_BUDGET : NON_CAPTURING_PROBE_BUDGET);
870877
}
871878

872879
private AtomicInteger getBudgetLevel() {
@@ -881,6 +888,11 @@ private void incrementBudget() {
881888
AtomicInteger budgetLevel = getBudgetLevel();
882889
if (budgetLevel != null) {
883890
budgetLevel.incrementAndGet();
891+
TracerAPI tracer = AgentTracer.get();
892+
AgentSpan span = tracer != null ? tracer.activeSpan() : null;
893+
if (span != null) {
894+
span.getLocalRootSpan().setTag(format("_dd.ld.probe_id.%s", id), budgetLevel.get());
895+
}
884896
}
885897
}
886898

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/sink/SnapshotSink.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public SnapshotSink(Config config, String tags, BatchUploader snapshotUploader)
5353
this.snapshotUploader = snapshotUploader;
5454
}
5555

56-
public BlockingQueue<Snapshot> getLowRateSnapshots() {
57-
return lowRateSnapshots;
58-
}
59-
6056
public void start() {
6157
if (started.compareAndSet(false, true)) {
6258
highRateScheduled =

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.datadog.debugger.sink.DebuggerSink;
1717
import com.datadog.debugger.sink.ProbeStatusSink;
1818
import com.datadog.debugger.sink.Snapshot;
19+
import datadog.trace.api.Config;
1920
import datadog.trace.api.IdGenerationStrategy;
2021
import datadog.trace.bootstrap.debugger.CapturedContext;
2122
import datadog.trace.bootstrap.debugger.EvaluationError;
@@ -80,20 +81,28 @@ public void noDebugSession() {
8081

8182
@Test
8283
public void budgets() {
83-
DebuggerSink sink = new DebuggerSink(getConfig(), mock(ProbeStatusSink.class));
84+
BudgetSink sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
8485
DebuggerAgentHelper.injectSink(sink);
85-
assertEquals(0, sink.getSnapshotSink().getLowRateSnapshots().size());
86+
8687
TracerAPI tracer =
8788
CoreTracer.builder().idGenerationStrategy(IdGenerationStrategy.fromName("random")).build();
8889
AgentTracer.registerIfAbsent(tracer);
8990
int runs = 100;
9091
for (int i = 0; i < runs; i++) {
91-
runTrace(tracer);
92+
runTrace(tracer, true);
93+
}
94+
assertEquals(runs * LogProbe.CAPTURING_PROBE_BUDGET, sink.captures);
95+
96+
sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
97+
DebuggerAgentHelper.injectSink(sink);
98+
runs = 1010;
99+
for (int i = 0; i < runs; i++) {
100+
runTrace(tracer, false);
92101
}
93-
assertEquals(runs * LogProbe.PROBE_BUDGET, sink.getSnapshotSink().getLowRateSnapshots().size());
102+
assertEquals(runs * LogProbe.NON_CAPTURING_PROBE_BUDGET, sink.highRate);
94103
}
95104

96-
private void runTrace(TracerAPI tracer) {
105+
private void runTrace(TracerAPI tracer, boolean captureSnapshot) {
97106
AgentSpan span = tracer.startSpan("budget testing", "test span");
98107
span.setTag(Tags.PROPAGATED_DEBUG, "12345:1");
99108
try (AgentScope scope = tracer.activateSpan(span, ScopeSource.MANUAL)) {
@@ -102,17 +111,23 @@ private void runTrace(TracerAPI tracer) {
102111
createLog("I'm in a debug session")
103112
.probeId(ProbeId.newId())
104113
.tags("session_id:12345")
105-
.captureSnapshot(true)
114+
.captureSnapshot(captureSnapshot)
106115
.build();
107-
108116
CapturedContext entryContext = capturedContext(span, logProbe);
109117
CapturedContext exitContext = capturedContext(span, logProbe);
110118
logProbe.evaluate(entryContext, new LogStatus(logProbe), MethodLocation.ENTRY);
111119
logProbe.evaluate(exitContext, new LogStatus(logProbe), MethodLocation.EXIT);
112120

113-
for (int i = 0; i < 20; i++) {
121+
int budget =
122+
logProbe.isCaptureSnapshot()
123+
? LogProbe.CAPTURING_PROBE_BUDGET
124+
: LogProbe.NON_CAPTURING_PROBE_BUDGET;
125+
int runs = budget + 20;
126+
127+
for (int i = 0; i < runs; i++) {
114128
logProbe.commit(entryContext, exitContext, emptyList());
115129
}
130+
assertEquals(runs, span.getLocalRootSpan().getTag(format("_dd.ld.probe_id.%s", logProbe.id)));
116131
}
117132
}
118133

@@ -293,4 +308,34 @@ private Builder createLog(String template) {
293308
.probeId(PROBE_ID)
294309
.template(template, parseTemplate(template));
295310
}
311+
312+
private static class BudgetSink extends DebuggerSink {
313+
314+
public int captures;
315+
public int highRate;
316+
317+
public BudgetSink(Config config, ProbeStatusSink probeStatusSink) {
318+
super(config, probeStatusSink);
319+
}
320+
321+
@Override
322+
public void addHighRateSnapshot(Snapshot snapshot) {
323+
highRate++;
324+
}
325+
326+
@Override
327+
public void addSnapshot(Snapshot snapshot) {
328+
captures++;
329+
}
330+
331+
@Override
332+
public void start() {
333+
super.start();
334+
}
335+
336+
@Override
337+
public void stop() {
338+
super.stop();
339+
}
340+
}
296341
}

0 commit comments

Comments
 (0)