Sha256: 3a75bbc8fb7eb69c3b3e1223e25eb3fcd8a1592fd255e8a58a6883c4a9b294c5

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 KB

Contents

# 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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
aspecto-opentelemetry-0.2.0 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.2.0.rc0 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.10 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.9 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.8 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.7 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.7.rc0 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.6 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.5 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.4 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.3 lib/aspecto/opentelemetry/sampler/rules_sampler.rb
aspecto-opentelemetry-0.1.2 lib/aspecto/opentelemetry/sampler/rules_sampler.rb