Class: Datadog::Tracing::Sampling::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/sampling/rule.rb

Overview

Sampling rule that dictates if a trace matches a specific criteria and what sampling strategy to apply in case of a positive match.

Direct Known Subclasses

SimpleRule

Constant Summary collapse

PROVENANCE_LOCAL =
:local
PROVENANCE_REMOTE_USER =
:customer
PROVENANCE_REMOTE_DYNAMIC =
:dynamic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, sampler, provenance) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • matcher (Matcher)

    A matcher to verify trace conformity against

  • sampler (Sampler)

    A sampler to be consulted on a positive match



22
23
24
25
26
# File 'lib/datadog/tracing/sampling/rule.rb', line 22

def initialize(matcher, sampler, provenance)
  @matcher = matcher
  @sampler = sampler
  @provenance = provenance
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



18
19
20
# File 'lib/datadog/tracing/sampling/rule.rb', line 18

def matcher
  @matcher
end

#provenanceObject (readonly)

Returns the value of attribute provenance.



18
19
20
# File 'lib/datadog/tracing/sampling/rule.rb', line 18

def provenance
  @provenance
end

#samplerObject (readonly)

Returns the value of attribute sampler.



18
19
20
# File 'lib/datadog/tracing/sampling/rule.rb', line 18

def sampler
  @sampler
end

Instance Method Details

#match?(trace) ⇒ Boolean, NilClass

Evaluates if the provided ‘trace` conforms to the `matcher`.

Parameters:

Returns:

  • (Boolean)

    whether this rules applies to the trace

  • (NilClass)

    if the matcher fails errs during evaluation



33
34
35
36
37
38
39
40
41
# File 'lib/datadog/tracing/sampling/rule.rb', line 33

def match?(trace)
  @matcher.match?(trace)
rescue => e
  Datadog.logger.error(
    "Matcher failed. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
  Datadog::Core::Telemetry::Logger.report(e, description: 'Matcher failed')
  nil
end

#sample!(trace) ⇒ Boolean

Returns ‘true` if the provided trace should be kept. Otherwise, `false`.

This method may modify the ‘trace`, in case changes are necessary based on the sampling decision.

Parameters:

Returns:

  • (Boolean)

    should this trace be kept?



44
45
46
# File 'lib/datadog/tracing/sampling/rule.rb', line 44

def sample!(trace)
  @sampler.sample!(trace)
end

#sample_rate(trace) ⇒ Float?

The sampling rate, if this sampler has such concept. Otherwise, ‘nil`.

Parameters:

Returns:

  • (Float, nil)

    sampling ratio between 0.0 and 1.0 (inclusive), or ‘nil` if not applicable



49
50
51
# File 'lib/datadog/tracing/sampling/rule.rb', line 49

def sample_rate(trace)
  @sampler.sample_rate(trace)
end