Skip to content

Commit b302b88

Browse files
committed
make sure budgets apply to line probes, too.
1 parent 3fd5db0 commit b302b88

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-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: 55 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
}
@@ -172,6 +203,29 @@ private static CapturedContext capturedContext(AgentSpan span, ProbeDefinition p
172203
return context;
173204
}
174205

206+
@Test
207+
public void testDebugLineProbes() {
208+
BudgetSink sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
209+
DebuggerAgentHelper.injectSink(sink);
210+
211+
TracerAPI tracer =
212+
CoreTracer.builder().idGenerationStrategy(IdGenerationStrategy.fromName("random")).build();
213+
AgentTracer.registerIfAbsent(tracer);
214+
int runs = 100;
215+
for (int i = 0; i < runs; i++) {
216+
runTrace(tracer, true);
217+
}
218+
assertEquals(runs * LogProbe.CAPTURING_PROBE_BUDGET, sink.captures);
219+
220+
sink = new BudgetSink(getConfig(), mock(ProbeStatusSink.class));
221+
DebuggerAgentHelper.injectSink(sink);
222+
runs = 1010;
223+
for (int i = 0; i < runs; i++) {
224+
runTrace(tracer, false);
225+
}
226+
assertEquals(runs * LogProbe.NON_CAPTURING_PROBE_BUDGET, sink.highRate);
227+
}
228+
175229
@Test
176230
public void log() {
177231
LogProbe logProbe = createLog(null).build();

0 commit comments

Comments
 (0)