Sha256: 38cb8306f709eaa9000ccca52e1b46c545497f39c190204cd36dd677edc99e65

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

class SentrySmartSampler
  class SampleRatePerErrorRegistry
    attr_reader :default_sample_rate, :registry
    private     :default_sample_rate, :registry

    def initialize(default_sample_rate)
      @default_sample_rate = default_sample_rate
      @registry = []
    end

    def declare(samplable, sample_rate:)
      registry << Registration.new(samplable: samplable, sample_rate: sample_rate)
    end

    def sample_rate_registration_for(error)
      registry.find(-> { default_registration }) { |registration| registration.matches?(error) }
    end

    private

    def default_registration
      Registration.new(samplable: nil, sample_rate: default_sample_rate)
    end

    class Registration
      attr_reader :samplable, :sample_rate

      def initialize(samplable:, sample_rate:)
        @samplable = samplable
        @sample_rate = sample_rate
      end

      def matches?(matchable_error)
        if samplable.is_a?(Regexp) || samplable.respond_to?(:to_str)
          matchable_error.message.scan(samplable).any?
        else
          matchable_error.is_a?(samplable)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sentry-smart-sampler-0.1.0 lib/sentry_smart_sampler/sample_rate_per_error_registry.rb