# frozen_string_literal: true require_relative "./sampling_rule" 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| 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:) 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