Sha256: 7b1f374acf18a87a2eea8775a679427c30c81a0909cf890c40b199ff2fb36dbf

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# typed: false

require 'datadog/tracing/contrib/faraday/connection'
require 'datadog/tracing/contrib/faraday/ext'
require 'datadog/tracing/contrib/faraday/rack_builder'
require 'datadog/tracing/contrib/patcher'

module Datadog
  module Tracing
    module Contrib
      module Faraday
        # Patcher enables patching of 'faraday' module.
        module Patcher
          include Contrib::Patcher

          module_function

          def target_version
            Integration.version
          end

          def patch
            require 'datadog/tracing/contrib/faraday/middleware'

            register_middleware!
            add_default_middleware!
          end

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

          def add_default_middleware!
            if target_version >= Gem::Version.new('1.0.0')
              # Patch the default connection (e.g. +Faraday.get+)
              ::Faraday.default_connection.use(:ddtrace)

              # Patch new connection instances (e.g. +Faraday.new+)
              ::Faraday::Connection.prepend(Connection)
            else
              # Patch the default connection (e.g. +Faraday.get+)
              #
              # We insert our middleware before the 'adapter', which is
              # always the last handler.
              idx = ::Faraday.default_connection.builder.handlers.size - 1
              ::Faraday.default_connection.builder.insert(idx, Middleware)

              # Patch new connection instances (e.g. +Faraday.new+)
              ::Faraday::RackBuilder.prepend(RackBuilder)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddtrace-1.2.0 lib/datadog/tracing/contrib/faraday/patcher.rb
ddtrace-1.1.0 lib/datadog/tracing/contrib/faraday/patcher.rb