@@ -43,18 +43,18 @@ public class LogProbeTest {
43
43
44
44
@ Test
45
45
public void testCapture () {
46
- LogProbe . Builder builder = createLog (null );
46
+ Builder builder = createLog (null );
47
47
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 ());
51
51
}
52
52
53
53
@ Test
54
54
public void testSampling () {
55
- LogProbe . Builder builder = createLog (null );
55
+ Builder builder = createLog (null );
56
56
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 );
58
58
}
59
59
60
60
@ Test
@@ -78,6 +78,44 @@ public void noDebugSession() {
78
78
"With no debug sessions, snapshots should get filled." );
79
79
}
80
80
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
+
81
119
private boolean fillSnapshot (DebugSessionStatus status ) {
82
120
DebuggerAgentHelper .injectSink (new DebuggerSink (getConfig (), mock (ProbeStatusSink .class )));
83
121
TracerAPI tracer =
@@ -152,12 +190,11 @@ public void fillSnapshot_shouldSend(String methodLocation) {
152
190
LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .valueOf (methodLocation )).build ();
153
191
CapturedContext entryContext = new CapturedContext ();
154
192
CapturedContext exitContext = new CapturedContext ();
155
- LogProbe .LogStatus logEntryStatus =
156
- prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
193
+ LogStatus logEntryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
157
194
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 );
159
196
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 );
161
198
assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
162
199
}
163
200
@@ -172,16 +209,16 @@ public void fillSnapshot(
172
209
LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .EXIT ).build ();
173
210
CapturedContext entryContext = new CapturedContext ();
174
211
CapturedContext exitContext = new CapturedContext ();
175
- LogProbe . LogStatus entryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
212
+ LogStatus entryStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
176
213
fillStatus (entryStatus , sampled , condition , conditionErrors , logTemplateErrors );
177
- LogProbe . LogStatus exitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
214
+ LogStatus exitStatus = prepareContext (exitContext , logProbe , MethodLocation .EXIT );
178
215
fillStatus (exitStatus , sampled , condition , conditionErrors , logTemplateErrors );
179
- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
216
+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
180
217
assertEquals (shouldCommit , logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
181
218
}
182
219
183
220
private void fillStatus (
184
- LogProbe . LogStatus entryStatus ,
221
+ LogStatus entryStatus ,
185
222
boolean sampled ,
186
223
boolean condition ,
187
224
boolean conditionErrors ,
@@ -193,10 +230,10 @@ private void fillStatus(
193
230
entryStatus .setLogTemplateErrors (logTemplateErrors );
194
231
}
195
232
196
- private LogProbe . LogStatus prepareContext (
233
+ private LogStatus prepareContext (
197
234
CapturedContext context , LogProbe logProbe , MethodLocation methodLocation ) {
198
235
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 ());
200
237
}
201
238
202
239
private static Stream <Arguments > statusValues () {
@@ -222,15 +259,15 @@ public void fillSnapshot_shouldSend_exit() {
222
259
prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
223
260
CapturedContext exitContext = new CapturedContext ();
224
261
prepareContext (exitContext , logProbe , MethodLocation .EXIT );
225
- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
262
+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
226
263
assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
227
264
}
228
265
229
266
@ Test
230
267
public void fillSnapshot_shouldSend_evalErrors () {
231
268
LogProbe logProbe = createLog (null ).evaluateAt (MethodLocation .EXIT ).build ();
232
269
CapturedContext entryContext = new CapturedContext ();
233
- LogProbe . LogStatus logStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
270
+ LogStatus logStatus = prepareContext (entryContext , logProbe , MethodLocation .ENTRY );
234
271
logStatus .addError (new EvaluationError ("expr" , "msg1" ));
235
272
logStatus .setLogTemplateErrors (true );
236
273
entryContext .addThrowable (new RuntimeException ("errorEntry" ));
@@ -239,7 +276,7 @@ public void fillSnapshot_shouldSend_evalErrors() {
239
276
logStatus .addError (new EvaluationError ("expr" , "msg2" ));
240
277
logStatus .setLogTemplateErrors (true );
241
278
exitContext .addThrowable (new RuntimeException ("errorExit" ));
242
- Snapshot snapshot = new Snapshot (Thread . currentThread (), logProbe , 10 );
279
+ Snapshot snapshot = new Snapshot (currentThread (), logProbe , 10 );
243
280
assertTrue (logProbe .fillSnapshot (entryContext , exitContext , null , snapshot ));
244
281
assertEquals (2 , snapshot .getEvaluationErrors ().size ());
245
282
assertEquals ("msg1" , snapshot .getEvaluationErrors ().get (0 ).getMessage ());
@@ -250,7 +287,7 @@ public void fillSnapshot_shouldSend_evalErrors() {
250
287
"errorExit" , snapshot .getCaptures ().getReturn ().getCapturedThrowable ().getMessage ());
251
288
}
252
289
253
- private LogProbe . Builder createLog (String template ) {
290
+ private Builder createLog (String template ) {
254
291
return LogProbe .builder ()
255
292
.language (LANGUAGE )
256
293
.probeId (PROBE_ID )
0 commit comments