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