Sha256: 5b996826def1fa9b748ae431410a7523f90829a689b30baa2aa7c43435f015de

Contents?: true

Size: 1.83 KB

Versions: 27

Compression:

Stored size: 1.83 KB

Contents

module Datadog
  module Contrib
    module Faraday
      COMPATIBLE_UNTIL = Gem::Version.new('1.0.0')
      SERVICE = 'faraday'.freeze
      NAME = 'faraday.request'.freeze

      # Responsible for hooking the instrumentation into faraday
      module Patcher
        include Base

        register_as :faraday, auto_patch: true

        DEFAULT_ERROR_HANDLER = lambda do |env|
          Ext::HTTP::ERROR_RANGE.cover?(env[:status])
        end

        option :service_name, default: SERVICE
        option :distributed_tracing, default: false
        option :error_handler, default: DEFAULT_ERROR_HANDLER
        option :tracer, default: Datadog.tracer

        @patched = false

        class << self
          def patch
            return @patched if patched? || !compatible?

            require 'ddtrace/ext/app_types'
            require 'ddtrace/contrib/faraday/middleware'

            add_pin
            add_middleware

            @patched = true
          rescue => e
            Tracer.log.error("Unable to apply Faraday integration: #{e}")
            @patched
          end

          def patched?
            @patched
          end

          def register_service(name)
            get_option(:tracer).set_service_info(name, 'faraday', Ext::AppTypes::WEB)
          end

          private

          def compatible?
            return unless defined?(::Faraday::VERSION)

            Gem::Version.new(::Faraday::VERSION) < COMPATIBLE_UNTIL
          end

          def add_pin
            Pin.new(
              get_option(:service_name),
              app: 'faraday',
              app_type: Ext::AppTypes::WEB,
              tracer: get_option(:tracer)
            ).onto(::Faraday)
          end

          def add_middleware
            ::Faraday::Middleware.register_middleware(ddtrace: Middleware)
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ddtrace-0.16.1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.15.0.internaltracinfeature1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.16.0 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.2.disableprotocolversion4 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.15.0 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.2.withoutpriorityparsing1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.2 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.15.0.beta1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.0 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.0.rc1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.13.2 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.0.beta2 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.14.0.beta1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.13.1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.13.0 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.12.1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.13.0.beta1 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.12.0 lib/ddtrace/contrib/faraday/patcher.rb
ddtrace-0.12.0.rc1 lib/ddtrace/contrib/faraday/patcher.rb