Skip to content

Commit 76c37d7

Browse files
fix: compatibility with Faraday v1 (#1032)
Fixes #1031 Faraday >= 1 changes where the #adapter method gets called, affecting the RackBuilder patch in this gem. Without this change, the tracer middleware gets added to the beginning of the stack instead of the end, and explicitly adding the middleware would cause it to get added twice. This change patches `Connection#initialize` for Faraday >= 1 to preserve the behavior we had with Faraday < 1. This is similar to the dd-trace-rb change made in DataDog/dd-trace-rb#906. There is some followup work here to explore moving the middleware to the beginning of the stack instead of the end, but we can work on that separately.
1 parent 5b09f9b commit 76c37d7

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2727

2828
def require_dependencies
2929
require_relative 'middlewares/tracer_middleware'
30+
require_relative 'patches/connection'
3031
require_relative 'patches/rack_builder'
3132
end
3233

@@ -37,7 +38,11 @@ def register_tracer_middleware
3738
end
3839

3940
def use_middleware_by_default
40-
::Faraday::RackBuilder.prepend(Patches::RackBuilder)
41+
if Gem::Version.new(::Faraday::VERSION) >= Gem::Version.new('1')
42+
::Faraday::Connection.prepend(Patches::Connection)
43+
else
44+
::Faraday::RackBuilder.prepend(Patches::RackBuilder)
45+
end
4146
end
4247
end
4348
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module Faraday
10+
module Patches
11+
# Module to be prepended to force Faraday to use the middleware by
12+
# default so the user doesn't have to call `use` for every connection.
13+
module Connection
14+
# Wraps Faraday::Connection#initialize:
15+
# https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
16+
def initialize(*args)
17+
super.tap do
18+
use(:open_telemetry) unless builder.handlers.any? do |handler|
19+
handler.klass == Middlewares::TracerMiddleware
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end

instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,18 @@
198198
_(span.status.code).must_equal OpenTelemetry::Trace::Status::ERROR
199199
end
200200
end
201+
202+
describe 'when explicitly adding the tracer middleware' do
203+
let(:client) do
204+
Faraday.new do |builder|
205+
builder.use :open_telemetry
206+
end
207+
end
208+
209+
it 'only adds the middleware once' do
210+
tracers = client.builder.handlers.count(OpenTelemetry::Instrumentation::Faraday::Middlewares::TracerMiddleware)
211+
_(tracers).must_equal 1
212+
end
213+
end
201214
end
202215
end

0 commit comments

Comments
 (0)