@@ -43,18 +43,18 @@ public class LogProbeTest {
4343
4444 @ Test
4545 public void testCapture () {
46- LogProbe . Builder builder = createLog (null );
46+ Builder builder = createLog (null );
4747 LogProbe snapshotProbe = builder .capture (1 , 420 , 255 , 20 ).build ();
48- Assertions . assertEquals (1 , snapshotProbe .getCapture ().getMaxReferenceDepth ());
49- Assertions . assertEquals (420 , snapshotProbe .getCapture ().getMaxCollectionSize ());
50- Assertions . assertEquals (255 , snapshotProbe .getCapture ().getMaxLength ());
48+ assertEquals (1 , snapshotProbe .getCapture ().getMaxReferenceDepth ());
49+ assertEquals (420 , snapshotProbe .getCapture ().getMaxCollectionSize ());
50+ assertEquals (255 , snapshotProbe .getCapture ().getMaxLength ());
5151 }
5252
5353 @ Test
5454 public void testSampling () {
55- LogProbe . Builder builder = createLog (null );
55+ Builder builder = createLog (null );
5656 LogProbe snapshotProbe = builder .sampling (0.25 ).build ();
57- Assertions . assertEquals (0.25 , snapshotProbe .getSampling ().getEventsPerSecond (), 0.01 );
57+ assertEquals (0.25 , snapshotProbe .getSampling ().getEventsPerSecond (), 0.01 );
5858 }
5959
6060 @ Test
@@ -78,6 +78,44 @@ public void noDebugSession() {
7878 "With no debug sessions, snapshots should get filled." );
7979 }
8080
81+ @ Test
82+ public void budgets () {
83+ DebuggerSink sink = new DebuggerSink (getConfig (), mock (ProbeStatusSink .class ));
84+ DebuggerAgentHelper .injectSink (sink );
85+ assertEquals (0 , sink .getSnapshotSink ().getLowRateSnapshots ().size ());
86+ TracerAPI tracer =
87+ CoreTracer .builder ().idGenerationStrategy (IdGenerationStrategy .fromName ("random" )).build ();
88+ AgentTracer .registerIfAbsent (tracer );
89+ int runs = 100 ;
90+ for (int i = 0 ; i < runs ; i ++) {
91+ runTrace (tracer );
92+ }
93+ assertEquals (runs * LogProbe .PROBE_BUDGET , sink .getSnapshotSink ().getLowRateSnapshots ().size ());
94+ }
95+
96+ private void runTrace (TracerAPI tracer ) {
97+ AgentSpan span = tracer .startSpan ("budget testing" , "test span" );
98+ span .setTag (Tags .PROPAGATED_DEBUG , "12345:1" );
99+ try (AgentScope scope = tracer .activateSpan (span , ScopeSource .MANUAL )) {
100+
101+ LogProbe logProbe =
102+ createLog ("I'm in a debug session" )
103+ .probeId (ProbeId .newId ())
104+ .tags ("session_id:12345" )
105+ .captureSnapshot (true )
106+ .build ();
107+
108+ CapturedContext entryContext = capturedContext (span , logProbe );
109+ CapturedContext exitContext = capturedContext (span , logProbe );
110+ logProbe .evaluate (entryContext , new LogStatus (logProbe ), MethodLocation .ENTRY );
111+ logProbe .evaluate (exitContext , new LogStatus (logProbe ), MethodLocation .EXIT );
112+
113+ for (int i = 0 ; i < 20 ; i ++) {
114+ logProbe .commit (entryContext , exitContext , emptyList ());
115+ }
116+ }
117+ }
118+
81119 private boolean fillSnapshot (DebugSessionStatus status ) {
82120 DebuggerAgentHelper .injectSink (new DebuggerSink (getConfig (), mock (ProbeStatusSink .class )));
83121 TracerAPI tracer =
@@ -152,12 +190,11 @@ public void fillSnapshot_shouldSend(String methodLocation) {
152190 LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .valueOf (methodLocation )).build ();
153191 CapturedContext entryContext = new CapturedContext ();
154192 CapturedContext exitContext = new CapturedContext ();
155- LogProbe .LogStatus logEntryStatus =
156- prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
193+ LogStatus logEntryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
157194 logEntryStatus .setSampled (true ); // force sampled to avoid rate limiting executing tests!
158- LogProbe . LogStatus logExitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
195+ LogStatus logExitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
159196 logExitStatus .setSampled (true ); // force sampled to avoid rate limiting executing tests!
160- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
197+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
161198 assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
162199 }
163200
@@ -172,16 +209,16 @@ public void fillSnapshot(
172209 LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .EXIT ).build ();
173210 CapturedContext entryContext = new CapturedContext ();
174211 CapturedContext exitContext = new CapturedContext ();
175- LogProbe . LogStatus entryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
212+ LogStatus entryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
176213 fillStatus (entryStatus , sampled , condition , conditionErrors , logTemplateErrors );
177- LogProbe . LogStatus exitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
214+ LogStatus exitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
178215 fillStatus (exitStatus , sampled , condition , conditionErrors , logTemplateErrors );
179- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
216+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
180217 assertEquals (shouldCommit , logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
181218 }
182219
183220 private void fillStatus (
184- LogProbe . LogStatus entryStatus ,
221+ LogStatus entryStatus ,
185222 boolean sampled ,
186223 boolean condition ,
187224 boolean conditionErrors ,
@@ -193,10 +230,10 @@ private void fillStatus(
193230 entryStatus .setLogTemplateErrors (logTemplateErrors );
194231 }
195232
196- private LogProbe . LogStatus prepareContext (
233+ private LogStatus prepareContext (
197234 CapturedContext context , LogProbe logProbe , MethodLocation methodLocation ) {
198235 context .evaluate (PROBE_ID .getEncodedId (), logProbe , "" , 0 , methodLocation );
199- return (LogProbe . LogStatus ) context .getStatus (PROBE_ID .getEncodedId ());
236+ return (LogStatus ) context .getStatus (PROBE_ID .getEncodedId ());
200237 }
201238
202239 private static Stream <Arguments > statusValues () {
@@ -222,15 +259,15 @@ public void fillSnapshot_shouldSend_exit() {
222259 prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
223260 CapturedContext exitContext = new CapturedContext ();
224261 prepareContext (exitContext , logProbe , MethodLocation .EXIT );
225- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
262+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
226263 assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
227264 }
228265
229266 @ Test
230267 public void fillSnapshot_shouldSend_evalErrors () {
231268 LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .EXIT ).build ();
232269 CapturedContext entryContext = new CapturedContext ();
233- LogProbe . LogStatus logStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
270+ LogStatus logStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
234271 logStatus .addError (new EvaluationError ("expr" , "msg1" ));
235272 logStatus .setLogTemplateErrors (true );
236273 entryContext .addThrowable (new RuntimeException ("errorEntry" ));
@@ -239,7 +276,7 @@ public void fillSnapshot_shouldSend_evalErrors() {
239276 logStatus .addError (new EvaluationError ("expr" , "msg2" ));
240277 logStatus .setLogTemplateErrors (true );
241278 exitContext .addThrowable (new RuntimeException ("errorExit" ));
242- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
279+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
243280 assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
244281 assertEquals (2 , snapshot .getEvaluationErrors ().size ());
245282 assertEquals ("msg1" , snapshot .getEvaluationErrors ().get (0 ).getMessage ());
@@ -250,7 +287,7 @@ public void fillSnapshot_shouldSend_evalErrors() {
250287 "errorExit" , snapshot .getCaptures ().getReturn ().getCapturedThrowable ().getMessage ());
251288 }
252289
253- private LogProbe . Builder createLog (String template ) {
290+ private Builder createLog (String template ) {
254291 return LogProbe .builder ()
255292 .language (LANGUAGE )
256293 .probeId (PROBE_ID )
0 commit comments