Class: Aspecto::OpenTelemetry::Sampler::RulesSampler

Inherits:
Object
  • Object
show all
Defined in:
lib/aspecto/opentelemetry/sampler/rules_sampler.rb

Overview

OpenTelemetry sampler which implements the remote rules logic with fallback to service sampler

Instance Method Summary collapse

Constructor Details

#initialize(config, fallback_sampler, service_name, env) ⇒ RulesSampler

Returns a new instance of RulesSampler.



12
13
14
15
16
17
18
# File 'lib/aspecto/opentelemetry/sampler/rules_sampler.rb', line 12

def initialize(config, fallback_sampler, service_name, env)
  @rules = config
           .select { |rule| Operator.new(rule["packageName"]["comparison"], rule["packageName"]["value"]).satisfies?(service_name) }
           .select { |rule| Operator.new(rule["env"]["comparison"], rule["env"]["value"]).satisfies?(env) }
           .map { |rule_config| SamplingRule.new rule_config }
  @fallback_sampler = fallback_sampler
end

Instance Method Details

#descriptionObject



26
27
28
# File 'lib/aspecto/opentelemetry/sampler/rules_sampler.rb', line 26

def description
  "RulesSampler {#{rules.length} rules}"
end

#matching_rule(name:, attributes:) ⇒ Object



30
31
32
# File 'lib/aspecto/opentelemetry/sampler/rules_sampler.rb', line 30

def matching_rule(name:, attributes:)
  @rules.find { |rule| rule.satisfies?(attributes, name) }
end

#should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/aspecto/opentelemetry/sampler/rules_sampler.rb', line 20

def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
  rule = matching_rule(name: name, attributes: attributes)
  delegate_sampler = rule ? rule.sampler : @fallback_sampler
  delegate_sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
end