Class: Datadog::DI::CorrelationSampler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/correlation_sampler.rb,
lib/datadog/di/correlation_sampler/trace_budget.rb,
lib/datadog/di/correlation_sampler/trace_budget_ledger.rb,
lib/datadog/di/correlation_sampler/trace_budget_ledger/entry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Sampling gate that coordinates capturing Live Debugger probe hits within one trace and bounds total snapshot volume with process-wide budgets.

Defined Under Namespace

Classes: TraceBudget, TraceBudgetLedger

Constant Summary collapse

DEFAULT_SWEEP_THRESHOLD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Sweep floor for the per-trace budget ledger.

4096
TOP_RATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Snapshots per second, process-wide, that may establish an emitting trace.

10
GLOBAL_RATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Snapshots per second, process-wide, across all emits.

20
PER_PROBE_BUDGET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Snapshots one probe may emit within one trace.

1
ALL_BUDGET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Snapshots all probes together may emit within one trace.

20

Instance Method Summary collapse

Constructor Details

#initialize(sweep_threshold: DEFAULT_SWEEP_THRESHOLD, top_rate: TOP_RATE, global_rate: GLOBAL_RATE, per_probe_budget: PER_PROBE_BUDGET, all_budget: ALL_BUDGET) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an empty per-trace ledger and full process-wide TOP/GLOBAL buckets.

Parameters:

  • sweep_threshold (Integer) (defaults to: DEFAULT_SWEEP_THRESHOLD) —

    sweep floor for the per-trace budget ledger

  • top_rate (Numeric) (defaults to: TOP_RATE) —

    TOP rate limit, snapshots/second

  • global_rate (Numeric) (defaults to: GLOBAL_RATE) —

    GLOBAL rate limit, snapshots/second

  • per_probe_budget (Integer) (defaults to: PER_PROBE_BUDGET) —

    per-probe counter start value for each trace's budget

  • all_budget (Integer) (defaults to: ALL_BUDGET) —

    all-probe counter start value for each trace's budget

Raises:

  • (ArgumentError) —

    when sweep_threshold is zero, negative, or non-Integer, or when per_probe_budget or all_budget is negative or non-Integer



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/datadog/di/correlation_sampler.rb', line 45

def initialize(sweep_threshold: DEFAULT_SWEEP_THRESHOLD, top_rate: TOP_RATE,
  global_rate: GLOBAL_RATE, per_probe_budget: PER_PROBE_BUDGET,
  all_budget: ALL_BUDGET)
  TraceBudget.validate_budgets!(per_probe_budget: per_probe_budget, all_budget: all_budget)

  @per_probe_budget = per_probe_budget
  @all_budget = all_budget
  @sweep_threshold = sweep_threshold
  @top_rate = top_rate
  @global_rate = global_rate
  rebuild_state
end

Instance Method Details

#admit_process_wide_snapshot? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Consumes one GLOBAL token for a snapshot payload the coordinated emit path does not gate, such as a condition-evaluation-failure notification with no active trace or from a non-capturing probe. GLOBAL admits the payload only while the balance is positive, matching the uncorrelated emit path, so condition-error payloads and capturing emits share one process-wide snapshot budget.

Returns:

  • (Boolean) —

    true when the process-wide budget admits the payload



97
98
99
100
101
# File 'lib/datadog/di/correlation_sampler.rb', line 97

def admit_process_wide_snapshot?
  lock.synchronize do
    global_limiter.try_consume
  end
end

#after_fork! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Rebuilds the process-scoped state for a freshly forked child. Must run while the child is single-threaded.



107
108
109
110
# File 'lib/datadog/di/correlation_sampler.rb', line 107

def after_fork!
  rebuild_state
  nil
end

#emit?(probe, trace) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decides whether this capturing probe hit emits a snapshot.

Parameters:

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/datadog/di/correlation_sampler.rb', line 84

def emit?(probe, trace)
  admitted, = emit_with_reason?(probe, trace)
  admitted
end

#emit_with_reason?(probe, trace) ⇒ Array(Boolean, Symbol, nil)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decides whether this capturing probe hit emits a snapshot, and names the drop decision for a denial.

Parameters:

Returns:

  • (Array(Boolean, Symbol, nil)) —

    the admitted flag, and the drop decision naming the denying limit: :own_rate_limit, :global_budget, :top_gate or :trace_budget, nil when admitted



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/datadog/di/correlation_sampler.rb', line 66

def emit_with_reason?(probe, trace)
  lock.synchronize do
    return emit_uncorrelated?(probe) if trace.nil?

    budget = trace_budgets.fetch(trace)
    if budget
      emit_correlated?(budget, probe)
    else
      emit_first_probe?(probe, trace)
    end
  end
end