diff --git a/instrumentation/faraday/Appraisals b/instrumentation/faraday/Appraisals index 0c19136a4..4ed32e342 100644 --- a/instrumentation/faraday/Appraisals +++ b/instrumentation/faraday/Appraisals @@ -4,7 +4,7 @@ # # SPDX-License-Identifier: Apache-2.0 -%w[0.17.6 1.0 2.0].each do |version| +%w[1.0 2.0].each do |version| appraise "faraday-#{version}" do gem 'faraday', "~> #{version}" end diff --git a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb index 17d4826e8..a194d709e 100644 --- a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb +++ b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb @@ -10,12 +10,18 @@ module Faraday # The Instrumentation class contains logic to detect and install the Faraday # instrumentation class Instrumentation < OpenTelemetry::Instrumentation::Base + MINIMUM_VERSION = Gem::Version.new('1.0') + install do |_config| require_dependencies register_tracer_middleware use_middleware_by_default end + compatible do + gem_version >= MINIMUM_VERSION + end + present do defined?(::Faraday) end @@ -25,10 +31,13 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base private + def gem_version + Gem::Version.new(::Faraday::VERSION) + end + def require_dependencies require_relative 'middlewares/tracer_middleware' require_relative 'patches/connection' - require_relative 'patches/rack_builder' end def register_tracer_middleware @@ -38,11 +47,7 @@ def register_tracer_middleware end def use_middleware_by_default - if Gem::Version.new(::Faraday::VERSION) >= Gem::Version.new('1') - ::Faraday::Connection.prepend(Patches::Connection) - else - ::Faraday::RackBuilder.prepend(Patches::RackBuilder) - end + ::Faraday::Connection.prepend(Patches::Connection) end end end diff --git a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/connection.rb b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/connection.rb index 51d8ad0b5..1819488ad 100644 --- a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/connection.rb +++ b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/connection.rb @@ -13,7 +13,7 @@ module Patches module Connection # Wraps Faraday::Connection#initialize: # https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92 - def initialize(*args) + def initialize(...) super.tap do use(:open_telemetry) unless builder.handlers.any? do |handler| handler.klass == Middlewares::TracerMiddleware diff --git a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/rack_builder.rb b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/rack_builder.rb deleted file mode 100644 index 1b32a00da..000000000 --- a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/patches/rack_builder.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -module OpenTelemetry - module Instrumentation - module Faraday - module Patches - # Module to be prepended to force Faraday to use the middleware by - # default so the user doesn't have to call `use` for every connection. - module RackBuilder - def adapter(*args) - use(:open_telemetry) unless @handlers.any? do |handler| - handler.klass == Faraday::Middlewares::TracerMiddleware - end - - super - end - end - end - end - end -end