Skip to content

Commit 17f4653

Browse files
authored
Make sure budgets apply to line probes, too. (#8378)
1 parent 5b7780b commit 17f4653

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,19 @@ public void commit(CapturedContext lineContext, int line) {
701701
shouldCommit = true;
702702
}
703703
if (shouldCommit) {
704-
if (isCaptureSnapshot()) {
705-
// freeze context just before commit because line probes have only one context
706-
Duration timeout =
707-
Duration.of(Config.get().getDynamicInstrumentationCaptureTimeout(), ChronoUnit.MILLIS);
708-
lineContext.freeze(new TimeoutChecker(timeout));
709-
snapshot.addLine(lineContext, line);
704+
incrementBudget();
705+
if (inBudget()) {
706+
if (isCaptureSnapshot()) {
707+
// freeze context just before commit because line probes have only one context
708+
Duration timeout =
709+
Duration.of(
710+
Config.get().getDynamicInstrumentationCaptureTimeout(), ChronoUnit.MILLIS);
711+
lineContext.freeze(new TimeoutChecker(timeout));
712+
snapshot.addLine(lineContext, line);
713+
}
714+
commitSnapshot(snapshot, sink);
715+
return;
710716
}
711-
commitSnapshot(snapshot, sink);
712-
return;
713717
}
714718
sink.skipSnapshot(id, DebuggerContext.SkipCause.CONDITION);
715719
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,34 @@ public void budgets() {
102102
assertEquals(runs * LogProbe.NON_CAPTURING_PROBE_BUDGET, sink.highRate);
103103
}
104104

105+
@Test
106+
public void budgetsOnLineProbes() {
107+
BudgetSink sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
108+
DebuggerAgentHelper.injectSink(sink);
109+
110+
TracerAPI tracer =
111+
CoreTracer.builder().idGenerationStrategy(IdGenerationStrategy.fromName("random")).build();
112+
AgentTracer.registerIfAbsent(tracer);
113+
int runs = 100;
114+
for (int i = 0; i < runs; i++) {
115+
runTrace(tracer, true, 100);
116+
}
117+
assertEquals(runs * LogProbe.CAPTURING_PROBE_BUDGET, sink.captures);
118+
119+
sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
120+
DebuggerAgentHelper.injectSink(sink);
121+
runs = 1010;
122+
for (int i = 0; i < runs; i++) {
123+
runTrace(tracer, false, 100);
124+
}
125+
assertEquals(runs * LogProbe.NON_CAPTURING_PROBE_BUDGET, sink.highRate);
126+
}
127+
105128
private void runTrace(TracerAPI tracer, boolean captureSnapshot) {
129+
runTrace(tracer, captureSnapshot, null);
130+
}
131+
132+
private void runTrace(TracerAPI tracer, boolean captureSnapshot, Integer line) {
106133
AgentSpan span = tracer.startSpan("budget testing", "test span");
107134
span.setTag(Tags.PROPAGATED_DEBUG, "12345:1");
108135
try (AgentScope scope = tracer.activateSpan(span, ScopeSource.MANUAL)) {
@@ -125,7 +152,11 @@ private void runTrace(TracerAPI tracer, boolean captureSnapshot) {
125152
int runs = budget + 20;
126153

127154
for (int i = 0; i < runs; i++) {
128-
logProbe.commit(entryContext, exitContext, emptyList());
155+
if (line == null) {
156+
logProbe.commit(entryContext, exitContext, emptyList());
157+
} else {
158+
logProbe.commit(entryContext, line);
159+
}
129160
}
130161
assertEquals(runs, span.getLocalRootSpan().getTag(format("_dd.ld.probe_id.%s", logProbe.id)));
131162
}

0 commit comments

Comments
 (0)