# frozen_string_literal: true module Aspecto # Aspecto OpenTelemetry Distro module OpenTelemetry # Sampling logic for aspecto otel distribution module Sampler # A single operator used to evaluate sampling rules class Operator def initialize(operator, expected) # rubocop:disable Metrics/AbcSize @expected = expected&.downcase @expected = Regexp.new(@expected) if operator == "matches" operator_to_proc = { "eq" => proc { |actual| @expected == actual }, "ne" => proc { |actual| @expected != actual }, "starts_with" => proc { |actual| actual&.start_with?(expected) }, "ends_with" => proc { |actual| actual&.end_with?(expected) }, "contains" => proc { |actual| actual&.include?(expected) }, "not_contains" => proc { |actual| !actual&.include?(expected) }, "matches" => proc { |actual| @expected.match(actual) }, "defined" => proc { |actual| !actual.nil? }, "undefined" => proc { |actual| actual.nil? }, "any" => proc { true } } @evaluate = operator_to_proc.fetch(operator, proc { false }) end def satisfies?(actual) @evaluate.call actual&.downcase end end end end end