1616import com .datadog .debugger .sink .DebuggerSink ;
1717import com .datadog .debugger .sink .Snapshot ;
1818import com .datadog .debugger .util .MoshiHelper ;
19+ import com .datadog .debugger .util .WeakIdentityHashMap ;
1920import com .squareup .moshi .Json ;
2021import com .squareup .moshi .JsonAdapter ;
2122import com .squareup .moshi .JsonReader ;
2223import com .squareup .moshi .JsonWriter ;
2324import com .squareup .moshi .Types ;
2425import datadog .trace .api .Config ;
26+ import datadog .trace .api .DDTraceId ;
2527import datadog .trace .bootstrap .debugger .CapturedContext ;
2628import datadog .trace .bootstrap .debugger .CorrelationAccess ;
2729import datadog .trace .bootstrap .debugger .DebuggerContext ;
4951import java .util .List ;
5052import java .util .Map ;
5153import java .util .Objects ;
54+ import java .util .concurrent .atomic .AtomicInteger ;
5255import java .util .function .Consumer ;
5356import org .slf4j .Logger ;
5457import org .slf4j .LoggerFactory ;
@@ -59,6 +62,8 @@ public class LogProbe extends ProbeDefinition implements Sampled {
5962 private static final Limits LIMITS = new Limits (1 , 3 , 8192 , 5 );
6063 private static final int LOG_MSG_LIMIT = 8192 ;
6164
65+ protected static final int PROBE_BUDGET = 10 ;
66+
6267 /** Stores part of a templated message either a str or an expression */
6368 public static class Segment {
6469 private final String str ;
@@ -278,6 +283,7 @@ public String toString() {
278283 private final Capture capture ;
279284 private final Sampling sampling ;
280285 private transient Consumer <Snapshot > snapshotProcessor ;
286+ private transient Map <DDTraceId , AtomicInteger > budget = new WeakIdentityHashMap <>();
281287
282288 // no-arg constructor is required by Moshi to avoid creating instance with unsafe and by-passing
283289 // constructors, including field initializers.
@@ -568,7 +574,8 @@ public void commit(
568574 CapturedContext exitContext ,
569575 List <CapturedContext .CapturedThrowable > caughtExceptions ) {
570576 Snapshot snapshot = createSnapshot ();
571- boolean shouldCommit = fillSnapshot (entryContext , exitContext , caughtExceptions , snapshot );
577+ boolean shouldCommit =
578+ inBudget () && fillSnapshot (entryContext , exitContext , caughtExceptions , snapshot );
572579 DebuggerSink sink = DebuggerAgent .getSink ();
573580 if (shouldCommit ) {
574581 commitSnapshot (snapshot , sink );
@@ -855,6 +862,15 @@ public String toString() {
855862 }
856863 }
857864
865+ private boolean inBudget () {
866+ AgentSpan span = AgentTracer .activeSpan ();
867+ return span == null
868+ || budget
869+ .computeIfAbsent (span .getLocalRootSpan ().getTraceId (), id -> new AtomicInteger ())
870+ .getAndIncrement ()
871+ < PROBE_BUDGET ;
872+ }
873+
858874 @ Generated
859875 @ Override
860876 public boolean equals (Object o ) {
0 commit comments