From 1d31eaa589b374267366207fefe2eefc0db952b5 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Tue, 10 Mar 2020 16:09:28 -0400 Subject: [PATCH] Better support for Faraday v0 (#971) --- Appraisals | 20 +++++++++++++++++++ Rakefile | 10 ++++++++++ lib/ddtrace/contrib/faraday/patcher.rb | 7 ++++++- lib/ddtrace/contrib/faraday/rack_builder.rb | 18 +++++++++++++++++ .../contrib/faraday/middleware_spec.rb | 6 +++++- 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/ddtrace/contrib/faraday/rack_builder.rb diff --git a/Appraisals b/Appraisals index 9c931a91ef9..debb39c33d0 100644 --- a/Appraisals +++ b/Appraisals @@ -518,6 +518,10 @@ elsif Gem::Version.new('2.3.0') <= Gem::Version.new(RUBY_VERSION) \ gem 'sucker_punch' gem 'typhoeus' end + + appraise 'contrib-old' do + gem 'faraday', '0.17' + end end elsif Gem::Version.new('2.4.0') <= Gem::Version.new(RUBY_VERSION) \ && Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0') @@ -591,6 +595,10 @@ elsif Gem::Version.new('2.4.0') <= Gem::Version.new(RUBY_VERSION) \ gem 'sucker_punch' gem 'typhoeus' end + + appraise 'contrib-old' do + gem 'faraday', '0.17' + end end elsif Gem::Version.new('2.5.0') <= Gem::Version.new(RUBY_VERSION) \ && Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.6.0') @@ -698,6 +706,10 @@ elsif Gem::Version.new('2.5.0') <= Gem::Version.new(RUBY_VERSION) \ gem 'sucker_punch' gem 'typhoeus' end + + appraise 'contrib-old' do + gem 'faraday', '0.17' + end end elsif Gem::Version.new('2.6.0') <= Gem::Version.new(RUBY_VERSION) \ && Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0') @@ -806,6 +818,10 @@ elsif Gem::Version.new('2.6.0') <= Gem::Version.new(RUBY_VERSION) \ gem 'sucker_punch' gem 'typhoeus' end + + appraise 'contrib-old' do + gem 'faraday', '0.17' + end end elsif Gem::Version.new('2.7.0') <= Gem::Version.new(RUBY_VERSION) if RUBY_PLATFORM != 'java' @@ -916,5 +932,9 @@ elsif Gem::Version.new('2.7.0') <= Gem::Version.new(RUBY_VERSION) gem 'sucker_punch' gem 'typhoeus' end + + appraise 'contrib-old' do + gem 'faraday', '0.17' + end end end diff --git a/Rakefile b/Rakefile index 1f962b21c20..85a4d98451f 100644 --- a/Rakefile +++ b/Rakefile @@ -381,6 +381,8 @@ task :ci do sh 'bundle exec appraisal contrib rake spec:shoryuken' sh 'bundle exec appraisal contrib rake spec:sinatra' sh 'bundle exec appraisal contrib rake spec:ethon' + # Contrib specs with old gem versions + sh 'bundle exec appraisal contrib-old rake spec:faraday' # Rails minitests sh 'bundle exec appraisal rails30-postgres rake test:rails' sh 'bundle exec appraisal rails30-postgres rake spec:railsdisableenv' @@ -451,6 +453,8 @@ task :ci do sh 'bundle exec appraisal contrib rake spec:shoryuken' sh 'bundle exec appraisal contrib rake spec:sinatra' sh 'bundle exec appraisal contrib rake spec:ethon' + # Contrib specs with old gem versions + sh 'bundle exec appraisal contrib-old rake spec:faraday' # Rails minitests # We only test Rails 5+ because older versions require Bundler < 2.0 sh 'bundle exec appraisal rails5-mysql2 rake test:rails' @@ -506,6 +510,8 @@ task :ci do sh 'bundle exec appraisal contrib rake spec:shoryuken' sh 'bundle exec appraisal contrib rake spec:sinatra' sh 'bundle exec appraisal contrib rake spec:ethon' + # Contrib specs with old gem versions + sh 'bundle exec appraisal contrib-old rake spec:faraday' # Rails minitests # We only test Rails 5+ because older versions require Bundler < 2.0 sh 'bundle exec appraisal rails5-mysql2 rake test:rails' @@ -571,6 +577,8 @@ task :ci do sh 'bundle exec appraisal contrib rake spec:shoryuken' sh 'bundle exec appraisal contrib rake spec:sinatra' sh 'bundle exec appraisal contrib rake spec:ethon' + # Contrib specs with old gem versions + sh 'bundle exec appraisal contrib-old rake spec:faraday' # Rails minitests # We only test Rails 5+ because older versions require Bundler < 2.0 sh 'bundle exec appraisal rails5-mysql2 rake test:rails' @@ -635,6 +643,8 @@ task :ci do sh 'bundle exec appraisal contrib rake spec:shoryuken' sh 'bundle exec appraisal contrib rake spec:sinatra' sh 'bundle exec appraisal contrib rake spec:ethon' + # Contrib specs with old gem versions + sh 'bundle exec appraisal contrib-old rake spec:faraday' # Rails minitests # We only test Rails 5+ because older versions require Bundler < 2.0 sh 'bundle exec appraisal rails5-mysql2 rake test:rails' diff --git a/lib/ddtrace/contrib/faraday/patcher.rb b/lib/ddtrace/contrib/faraday/patcher.rb index 8d0bab38aca..13b3c572d76 100644 --- a/lib/ddtrace/contrib/faraday/patcher.rb +++ b/lib/ddtrace/contrib/faraday/patcher.rb @@ -2,6 +2,7 @@ require 'ddtrace/ext/app_types' require 'ddtrace/contrib/faraday/ext' require 'ddtrace/contrib/faraday/connection' +require 'ddtrace/contrib/faraday/rack_builder' module Datadog module Contrib @@ -39,7 +40,11 @@ def register_middleware! end def add_default_middleware! - ::Faraday::Connection.send(:prepend, Connection) + if target_version >= Gem::Version.new('1.0.0') + ::Faraday::Connection.send(:prepend, Connection) + else + ::Faraday::RackBuilder.send(:prepend, RackBuilder) + end end def get_option(option) diff --git a/lib/ddtrace/contrib/faraday/rack_builder.rb b/lib/ddtrace/contrib/faraday/rack_builder.rb new file mode 100644 index 00000000000..f98c7571e6b --- /dev/null +++ b/lib/ddtrace/contrib/faraday/rack_builder.rb @@ -0,0 +1,18 @@ +module Datadog + module Contrib + module Faraday + # Handles installation of our middleware if the user has *not* + # already explicitly configured it for this correction. + # + # RackBuilder class was introduced in faraday 0.9.0: + # https://github.com/lostisland/faraday/commit/77d7546d6d626b91086f427c56bc2cdd951353b3 + module RackBuilder + def adapter(*args) + use(:ddtrace) unless @handlers.any? { |h| h.klass == Middleware } + + super + end + end + end + end +end diff --git a/spec/ddtrace/contrib/faraday/middleware_spec.rb b/spec/ddtrace/contrib/faraday/middleware_spec.rb index a28f6d55e11..b2998972cd7 100644 --- a/spec/ddtrace/contrib/faraday/middleware_spec.rb +++ b/spec/ddtrace/contrib/faraday/middleware_spec.rb @@ -42,7 +42,7 @@ end context 'without explicit middleware configured' do - subject!(:response) { client.get('/success') } + subject(:response) { client.get('/success') } let(:use_middleware) { false } it 'uses default configuration' do @@ -60,6 +60,10 @@ expect(request_span.span_type).to eq(Datadog::Ext::HTTP::TYPE_OUTBOUND) expect(request_span).to_not have_error end + + it 'executes without warnings' do + expect { response }.to_not output(/WARNING/).to_stderr + end end context 'when there is no interference' do