File tree Expand file tree Collapse file tree 6 files changed +69
-0
lines changed
lib/opentelemetry/instrumentation/ethon/patches
test/opentelemetry/instrumentation/ethon Expand file tree Collapse file tree 6 files changed +69
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments