Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sentry-ruby/lib/sentry/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ def initialize(url = nil, options = nil)

# Ensure that we attach instrumentation only if the adapter is not net/http
# because if is is, then the net/http instrumentation will take care of it
if builder.adapter.name != "Faraday::Adapter::NetHttp"
if builder.adapter.name != "Faraday::Adapter::NetHttp" &&
!builder.locked? &&
!builder.instance_variable_defined?(:@sentry_patched)
# Make sure that it's going to be the first middleware so that it can capture
# the entire request processing involving other middlewares
builder.insert(0, ::Faraday::Request::Instrumentation, name: OP_NAME, instrumenter: Instrumenter.new)
builder.instance_variable_set(:@sentry_patched, true)
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions sentry-ruby/spec/sentry/faraday_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
end

context "with tracing enabled" do
it "does not insert instrumentation more than once when a builder is reused" do
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.get("/") { [200, {}, "ok"] }
end

builder = Faraday::RackBuilder.new do |connection|
connection.adapter :test, stubs
end

primary = Faraday.new("https://primary.example", builder: builder)
Faraday.new("https://secondary.example", builder: builder)

expect(builder.handlers.count { |handler| handler == Faraday::Request::Instrumentation }).to eq(1)

primary.get("/")

expect {
Faraday.new("https://fallback.example", builder: builder)
}.not_to raise_error
end

let(:http) do
Faraday.new(url) do |f|
f.request :json
Expand Down
Loading