Class: Datadog::DI::CorrelationSampler::TraceBudget Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/correlation_sampler/trace_budget.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.

Per-trace emission counters: an all counter shared by every probe, and a per-probe counter that starts at per_probe_budget for each distinct probe.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(per_probe_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 the all-probe counter to all_budget with each probe's counter starting at per_probe_budget.

Parameters:

  • per_probe_budget (Integer) —

    per-probe counter start value

  • all_budget (Integer) —

    all-probe counter start value

Raises:

  • (ArgumentError) —

    when either budget is negative or non-Integer



36
37
38
39
40
41
42
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 36

def initialize(per_probe_budget:, all_budget:)
  TraceBudget.validate_budgets!(per_probe_budget: per_probe_budget, all_budget: all_budget)

  @all_remaining = all_budget
  @per_probe_budget = per_probe_budget
  @per_probe = {}
end

Instance Attribute Details

#all_remaining ⇒ Integer (readonly)

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.

Remaining all-probe counter.

Returns:

  • (Integer)


47
48
49
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 47

def all_remaining
  @all_remaining
end

#per_probe ⇒ Hash[String, Integer] (readonly)

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.

Remaining per-probe counters, keyed by probe id.

Returns:

  • (Hash[String, Integer])


57
58
59
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 57

def per_probe
  @per_probe
end

#per_probe_budget ⇒ Integer (readonly)

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.

Per-probe counter start value this budget was constructed with.

Returns:

  • (Integer)


52
53
54
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 52

def per_probe_budget
  @per_probe_budget
end

Class Method Details

.validate_budgets!(per_probe_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.

This method returns an undefined value.

Validates the budget arguments, raising ArgumentError when either is negative or non-Integer.

Parameters:

  • per_probe_budget (Integer) —

    per-probe counter start value

  • all_budget (Integer) —

    all-probe counter start value



18
19
20
21
22
23
24
25
26
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 18

def self.validate_budgets!(per_probe_budget:, all_budget:)
  unless per_probe_budget.is_a?(Integer) && per_probe_budget >= 0
    raise ArgumentError, "per_probe_budget must be a non-negative Integer: #{per_probe_budget}"
  end
  unless all_budget.is_a?(Integer) && all_budget >= 0
    raise ArgumentError, "all_budget must be a non-negative Integer: #{all_budget}"
  end
  nil
end

Instance Method Details

#admit(probe_id) ⇒ 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 per-probe and one all token for probe_id.

Parameters:

  • probe_id (String)

Returns:

  • (Boolean) —

    true when both counters had budget and were consumed, false when either was exhausted



64
65
66
67
68
69
70
71
# File 'lib/datadog/di/correlation_sampler/trace_budget.rb', line 64

def admit(probe_id)
  remaining = per_probe.fetch(probe_id, per_probe_budget)
  return false if remaining <= 0 || all_remaining <= 0

  per_probe[probe_id] = remaining - 1
  @all_remaining = all_remaining - 1
  true
end