Sha256: d23c67f2ddca287892fbbb2c83aaf858945d5788d18fcf77755301e203d1b3ac

Contents?: true

Size: 1.94 KB

Versions: 7

Compression:

Stored size: 1.94 KB

Contents

require 'set'
require 'ddtrace/ext/http'

module Datadog
  module Contrib
    # Contains methods helpful for tracing/annotating HTTP request libraries
    class StatusCodeMatcher
      REGEX_PARSER = /^\d{3}(?:-\d{3})?(?:,\d{3}(?:-\d{3})?)*$/.freeze

      def initialize(range)
        @error_response_range = range
        set_range
      end

      def include?(exception_status)
        set_range.include?(exception_status)
      end

      def to_s
        @error_response_range.to_s
      end

      private

      def set_range
        @datadog_set ||= begin
          set = Set.new
          handle_statuses.each do |statuses|
            status = statuses.to_s.split('-')
            case status.length
            when 1
              set.add(Integer(status[0]))
            when 2
              min, max = status.minmax
              Array(min..max).each do |i|
                set.add(Integer(i))
              end
            end
          end
          set
        end
        @datadog_set
      end

      def error_responses
        return @error_response_range if @error_response_range.is_a?(String) && !@error_response_range.nil?

        @error_response_range.join(',') if @error_response_range.is_a?(Array) && !@error_response_range.empty?
      end

      def handle_statuses
        if error_responses
          filter_error_responses = error_responses.gsub(/\s+/, '').split(',').select do |code|
            if !code.to_s.match(REGEX_PARSER)
              Datadog.logger.debug("Invalid config provided: #{code}. Must be formatted like '400-403,405,410-499'.")
              next
            else
              true
            end
          end
          filter_error_responses.empty? ? Datadog::Ext::HTTP::ERROR_RANGE.to_a : filter_error_responses
        else
          Datadog.logger.debug('No valid config was provided for :error_statuses - falling back to default.')
          Datadog::Ext::HTTP::ERROR_RANGE.to_a
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.51.0 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.50.0 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.49.0 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.48.0 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.47.0 lib/ddtrace/contrib/status_code_matcher.rb
ddtrace-0.46.0 lib/ddtrace/contrib/status_code_matcher.rb