Skip to content

Commit 36ea12c

Browse files
fix: improve Ethon exception handling (#1634)
* capture exceptions and close span in perform * remove binding.pry * fix rubocop errors --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent 93f5c82 commit 36ea12c

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/dup/easy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def headers=(headers)
3838
def perform
3939
otel_before_request
4040
super
41+
rescue StandardError => e
42+
# If an exception occurs before we can call `complete`, we should add and error status and close the span
43+
@otel_span&.status = OpenTelemetry::Trace::Status.error("Request threw an exception: #{e.message}")
44+
@otel_span&.finish
45+
@otel_span = nil
4146
end
4247

4348
def complete

instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def headers=(headers)
3535
def perform
3636
otel_before_request
3737
super
38+
rescue StandardError => e
39+
# If an exception occurs before we can call `complete`, we should add and error status and close the span
40+
@otel_span&.status = OpenTelemetry::Trace::Status.error("Request threw an exception: #{e.message}")
41+
@otel_span&.finish
42+
@otel_span = nil
3843
end
3944

4045
def complete

instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/stable/easy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def headers=(headers)
3838
def perform
3939
otel_before_request
4040
super
41+
rescue StandardError => e
42+
# If an exception occurs before we can call `complete`, we should add and error status and close the span
43+
@otel_span&.status = OpenTelemetry::Trace::Status.error("Request threw an exception: #{e.message}")
44+
@otel_span&.finish
45+
@otel_span = nil
4146
end
4247

4348
def complete

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@
8282
end
8383
end
8484
end
85+
86+
it 'when the perform fails before complete with an exception' do
87+
Ethon::Curl.stub(:easy_perform, ->(_handle) { raise StandardError, 'Connection failed' }) do
88+
easy.perform
89+
90+
# NOTE: check the finished spans since we expect to have closed it
91+
span = exporter.finished_spans.first
92+
_(span.name).must_equal 'HTTP'
93+
_(span.attributes['http.method']).must_equal 'N/A'
94+
_(span.attributes['http.status_code']).must_be_nil
95+
_(span.attributes['http.url']).must_equal 'http://example.com/test'
96+
_(span.attributes['http.request.method']).must_equal '_OTHER'
97+
_(span.attributes['http.response.status_code']).must_be_nil
98+
_(span.attributes['url.full']).must_equal 'http://example.com/test'
99+
_(span.status.code).must_equal(
100+
OpenTelemetry::Trace::Status::ERROR
101+
)
102+
_(easy.instance_eval { @otel_span }).must_be_nil
103+
end
104+
end
85105
end
86106

87107
describe '#complete' do

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@
7777
end
7878
end
7979
end
80+
81+
it 'when the perform fails before complete with an exception' do
82+
Ethon::Curl.stub(:easy_perform, ->(_handle) { raise StandardError, 'Connection failed' }) do
83+
easy.perform
84+
85+
# NOTE: check the finished spans since we expect to have closed it
86+
span = exporter.finished_spans.first
87+
_(span.name).must_equal 'HTTP N/A'
88+
_(span.attributes['http.method']).must_equal 'N/A'
89+
_(span.attributes['http.status_code']).must_be_nil
90+
_(span.attributes['http.url']).must_equal 'http://example.com/test'
91+
_(span.status.code).must_equal(
92+
OpenTelemetry::Trace::Status::ERROR
93+
)
94+
_(easy.instance_eval { @otel_span }).must_be_nil
95+
end
96+
end
8097
end
8198

8299
describe '#complete' do

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@
7878
end
7979
end
8080
end
81+
82+
it 'when the perform fails before complete with an exception' do
83+
Ethon::Curl.stub(:easy_perform, ->(_handle) { raise StandardError, 'Connection failed' }) do
84+
easy.perform
85+
86+
# NOTE: check the finished spans since we expect to have closed it
87+
span = exporter.finished_spans.first
88+
_(span.name).must_equal 'HTTP'
89+
_(span.attributes['http.request.method']).must_equal '_OTHER'
90+
_(span.attributes['http.response.status_code']).must_be_nil
91+
_(span.attributes['url.full']).must_equal 'http://example.com/test'
92+
_(span.status.code).must_equal(
93+
OpenTelemetry::Trace::Status::ERROR
94+
)
95+
_(easy.instance_eval { @otel_span }).must_be_nil
96+
end
97+
end
8198
end
8299

83100
describe '#complete' do

0 commit comments

Comments
 (0)