Sha256: 754aa520c37d3b2727cbb4f0ac7488cba0509d18afdcbf7903f446af841fa7ff

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aspecto-opentelemetry-0.1.1 lib/aspecto/opentelemetry/sampler/rules_sampler.rb