Skip to content

Commit 93a95f7

Browse files
pafuentkaylareopelle
authored andcommitted
fix: Fixing missing OpenTelemetry::Context detach on Excon instrumentation (open-telemetry#1725)
* fix: Fixing missing OpenTelemetry::Context detach on Excon instrumentation When a span is not recorded, for example when you are sampling traces, the OpenTelemetry::Context attached on `request_call` is never detached. This happens because `handle_response` is doing nothing if `span.recording?` is false. Here is example of the error that the missing detach generated: ``` ERROR -- : OpenTelemetry error: calls to detach should match corresponding calls to attach. ``` * Fixing tests following action_pack strategy to change the sampler * Improving datum handling --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent e3959d5 commit 93a95f7

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/dup/tracer_middleware.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def self.around_default_stack
8484

8585
def handle_response(datum)
8686
datum.delete(:otel_span)&.tap do |span|
87+
token = datum.delete(:otel_token)
88+
OpenTelemetry::Context.detach(token) if token
8789
return unless span.recording?
8890

8991
if datum.key?(:response)
@@ -99,7 +101,6 @@ def handle_response(datum)
99101
end
100102

101103
span.finish
102-
OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token)
103104
end
104105
rescue StandardError => e
105106
OpenTelemetry.handle_error(e)

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/old/tracer_middleware.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def self.around_default_stack
7676

7777
def handle_response(datum)
7878
datum.delete(:otel_span)&.tap do |span|
79+
token = datum.delete(:otel_token)
80+
OpenTelemetry::Context.detach(token) if token
7981
return unless span.recording?
8082

8183
if datum.key?(:response)
@@ -90,7 +92,6 @@ def handle_response(datum)
9092
end
9193

9294
span.finish
93-
OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token)
9495
end
9596
rescue StandardError => e
9697
OpenTelemetry.handle_error(e)

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/stable/tracer_middleware.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def self.around_default_stack
7676

7777
def handle_response(datum)
7878
datum.delete(:otel_span)&.tap do |span|
79+
token = datum.delete(:otel_token)
80+
OpenTelemetry::Context.detach(token) if token
7981
return unless span.recording?
8082

8183
if datum.key?(:response)
@@ -90,7 +92,6 @@ def handle_response(datum)
9092
end
9193

9294
span.finish
93-
OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token)
9495
end
9596
rescue StandardError => e
9697
OpenTelemetry.handle_error(e)

instrumentation/excon/test/opentelemetry/instrumentation/excon/dup/instrumentation_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@
199199

200200
_(span.attributes['peer.service']).must_equal 'example:custom'
201201
end
202+
203+
describe 'context detachment with non-recording spans' do
204+
it 'detaches context when span is not recorded' do
205+
with_sampler(OpenTelemetry::SDK::Trace::Samplers::ALWAYS_OFF) do
206+
initial_context = OpenTelemetry::Context.current
207+
Excon.get('http://example.com/success')
208+
final_context = OpenTelemetry::Context.current
209+
210+
_(final_context).must_equal initial_context
211+
_(exporter.finished_spans).must_be_empty
212+
end
213+
end
214+
end
202215
end
203216

204217
describe 'untraced?' do

instrumentation/excon/test/opentelemetry/instrumentation/excon/old/instrumentation_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158

159159
_(span.attributes['peer.service']).must_equal 'example:custom'
160160
end
161+
162+
describe 'context detachment with non-recording spans' do
163+
it 'detaches context when span is not recorded' do
164+
with_sampler(OpenTelemetry::SDK::Trace::Samplers::ALWAYS_OFF) do
165+
initial_context = OpenTelemetry::Context.current
166+
Excon.get('http://example.com/success')
167+
final_context = OpenTelemetry::Context.current
168+
169+
_(final_context).must_equal initial_context
170+
_(exporter.finished_spans).must_be_empty
171+
end
172+
end
173+
end
161174
end
162175

163176
describe 'untraced?' do

instrumentation/excon/test/opentelemetry/instrumentation/excon/stable/instrumentation_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@
172172

173173
_(span.attributes['peer.service']).must_equal 'example:custom'
174174
end
175+
176+
describe 'context detachment with non-recording spans' do
177+
it 'detaches context when span is not recorded' do
178+
with_sampler(OpenTelemetry::SDK::Trace::Samplers::ALWAYS_OFF) do
179+
initial_context = OpenTelemetry::Context.current
180+
Excon.get('http://example.com/success')
181+
final_context = OpenTelemetry::Context.current
182+
183+
_(final_context).must_equal initial_context
184+
_(exporter.finished_spans).must_be_empty
185+
end
186+
end
187+
end
175188
end
176189

177190
describe 'untraced?' do

instrumentation/excon/test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@
2020
c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)
2121
c.add_span_processor span_processor
2222
end
23+
24+
def with_sampler(sampler)
25+
previous_sampler = OpenTelemetry.tracer_provider.sampler
26+
OpenTelemetry.tracer_provider.sampler = sampler
27+
yield
28+
ensure
29+
OpenTelemetry.tracer_provider.sampler = previous_sampler
30+
end

0 commit comments

Comments
 (0)