lib/aspecto/opentelemetry/sampler/rules_sampler.rb in aspecto-opentelemetry-0.1.1 vs lib/aspecto/opentelemetry/sampler/rules_sampler.rb in aspecto-opentelemetry-0.1.2

- old
+ new

@@ -1,31 +1,35 @@ # frozen_string_literal: true require_relative "./sampling_rule" -require_relative "./utils" +require_relative "./operator" module Aspecto # Aspecto OpenTelemetry Distro module OpenTelemetry module Sampler # OpenTelemetry sampler which implements the remote rules logic with fallback to service sampler class RulesSampler def initialize(config, fallback_sampler, service_name, env) @rules = config - .select { |rule| Sampler.meets_operator?(rule["packageName"]["comparison"].to_sym, rule["packageName"]["value"], service_name) } - .select { |rule| Sampler.meets_operator?(rule["env"]["comparison"].to_sym, rule["env"]["value"], env) } + .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 def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) - matching_rule = @rules.find { |rule| rule.satisfied?(attributes, name) } - delegate_sampler = matching_rule ? matching_rule.sampler : @fallback_sampler + 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 def description "RulesSampler {#{rules.length} rules}" + end + + def matching_rule(name:, attributes:) + @rules.find { |rule| rule.satisfies?(attributes, name) } end end end end end