Class: Datadog::Tracing::Sampling::RuleSampler
- Inherits:
-
Object
- Object
- Datadog::Tracing::Sampling::RuleSampler
- Defined in:
- lib/datadog/tracing/sampling/rule_sampler.rb
Overview
Instance Attribute Summary collapse
-
#default_sampler ⇒ Object
readonly
Returns the value of attribute default_sampler.
-
#rate_limiter ⇒ Object
readonly
Returns the value of attribute rate_limiter.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(rules = [], rate_limit: Datadog.configuration.tracing.sampling.rate_limit, rate_limiter: nil, default_sample_rate: Datadog.configuration.tracing.sampling.default_rate, default_sampler: nil) ⇒ RuleSampler
constructor
A new instance of RuleSampler.
- #sample!(trace) ⇒ Object
-
#sample?(_trace) ⇒ Boolean
/RuleSampler’s components (it’s rate limiter, for example) are not be guaranteed to be size-effect free.
Constructor Details
#initialize(rules = [], rate_limit: Datadog.configuration.tracing.sampling.rate_limit, rate_limiter: nil, default_sample_rate: Datadog.configuration.tracing.sampling.default_rate, default_sampler: nil) ⇒ RuleSampler
Returns a new instance of RuleSampler.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 23 def initialize( rules = [], rate_limit: Datadog.configuration.tracing.sampling.rate_limit, rate_limiter: nil, default_sample_rate: Datadog.configuration.tracing.sampling.default_rate, default_sampler: nil ) @rules = rules @rate_limiter = if rate_limiter rate_limiter elsif rate_limit TokenBucket.new(rate_limit) else UnlimitedLimiter.new end @default_sampler = if default_sampler default_sampler elsif default_sample_rate # Add to the end of the rule list a rule always matches any trace @rules << SimpleRule.new(sample_rate: default_sample_rate) nil else # TODO: Simplify .tags access, as `Tracer#tags` can't be arbitrarily changed anymore RateByServiceSampler.new(1.0, env: -> { Tracing.send(:tracer).['env'] }) end end |
Instance Attribute Details
#default_sampler ⇒ Object (readonly)
Returns the value of attribute default_sampler.
15 16 17 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 15 def default_sampler @default_sampler end |
#rate_limiter ⇒ Object (readonly)
Returns the value of attribute rate_limiter.
15 16 17 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 15 def rate_limiter @rate_limiter end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
15 16 17 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 15 def rules @rules end |
Class Method Details
.parse(rules, rate_limit, default_sample_rate) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 51 def self.parse(rules, rate_limit, default_sample_rate) parsed_rules = JSON.parse(rules).map do |rule| sample_rate = rule['sample_rate'] begin sample_rate = Float(sample_rate) rescue raise "Rule '#{rule.inspect}' does not contain a float property `sample_rate`" end kwargs = { name: rule['name'], service: rule['service'], resource: rule['resource'], tags: rule['tags'], sample_rate: sample_rate, } Core::BackportFrom24.hash_compact!(kwargs) SimpleRule.new(**kwargs) end new(parsed_rules, rate_limit: rate_limit, default_sample_rate: default_sample_rate) rescue => e Datadog.logger.error do "Could not parse trace sampling rules '#{rules}': #{e.class.name} #{e.} at #{Array(e.backtrace).first}" end nil end |
Instance Method Details
#sample!(trace) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 92 def sample!(trace) sampled = sample_trace(trace) do |t| @default_sampler.sample!(t).tap do # We want to make sure the trace is tagged with the agent-derived # service rate. Retrieve this from the rate by service sampler. # Only do this if it was set by a RateByServiceSampler. trace.agent_sample_rate = @default_sampler.sample_rate(trace) if @default_sampler.is_a?(RateByServiceSampler) end end trace.sampled = sampled end |
#sample?(_trace) ⇒ Boolean
88 89 90 |
# File 'lib/datadog/tracing/sampling/rule_sampler.rb', line 88 def sample?(_trace) raise 'RuleSampler cannot be evaluated without side-effects' end |