Skip to content

Commit 5c647b5

Browse files
j15earielvalentin
andauthored
feat: Set span error only for 5xx response range (#1196)
feat: Sets span error only for 5xx response range This also avoids setting span in error when a Rails ActionCable hijacked response returns a -1 status code Co-authored-by: Ariel Valentin <[email protected]>
1 parent d7e9065 commit 5c647b5

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class EventHandler
4343
include ::Rack::Events::Abstract
4444

4545
OTEL_TOKEN_AND_SPAN = 'otel.rack.token_and_span'
46-
GOOD_HTTP_STATUSES = (100..499)
4746

4847
# Creates a server span for this current request using the incoming parent context
4948
# and registers them as the {current_span}
@@ -208,7 +207,7 @@ def detach_context(request)
208207
end
209208

210209
def add_response_attributes(span, response)
211-
span.status = OpenTelemetry::Trace::Status.error unless GOOD_HTTP_STATUSES.include?(response.status.to_i)
210+
span.status = OpenTelemetry::Trace::Status.error if response.server_error?
212211
attributes = extract_response_attributes(response)
213212
span.add_attributes(attributes)
214213
rescue StandardError => e

instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def create_request_span_name(request_uri_or_path_info, env)
152152
end
153153

154154
def set_attributes_after_request(span, status, headers, _response)
155-
span.status = OpenTelemetry::Trace::Status.error unless (100..499).cover?(status.to_i)
155+
span.status = OpenTelemetry::Trace::Status.error if (500..599).cover?(status.to_i)
156156
span.set_attribute('http.status_code', status)
157157

158158
# NOTE: if data is available, it would be good to do this:

instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/event_handler_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@
105105
_(proxy_event).must_be_nil
106106
end
107107

108+
describe 'with a hijacked response' do
109+
let(:service) do
110+
lambda do |env|
111+
env['rack.hijack?'] = true
112+
[-1, {}, []]
113+
end
114+
end
115+
116+
it 'sets the span status to "unset"' do
117+
_(rack_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
118+
end
119+
end
120+
108121
describe 'when baggage is set' do
109122
let(:headers) do
110123
Hash(

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@
7272
_(first_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
7373
end
7474

75+
describe 'with a hijacked response' do
76+
let(:app) do
77+
lambda do |env|
78+
env['rack.hijack?'] = true
79+
[-1, {}, []]
80+
end
81+
end
82+
83+
it 'sets the span status to "unset"' do
84+
_(first_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
85+
end
86+
end
87+
7588
it 'has no parent' do
7689
_(first_span.parent_span_id).must_equal OpenTelemetry::Trace::INVALID_SPAN_ID
7790
end

0 commit comments

Comments
 (0)