Skip to content

Commit

Permalink
feat: Set span error only for 5xx response range (#1196)
Browse files Browse the repository at this point in the history
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 <arielvalentin@users.noreply.github.com>
  • Loading branch information
j15e and arielvalentin authored Oct 23, 2024
1 parent d7e9065 commit 5c647b5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class EventHandler
include ::Rack::Events::Abstract

OTEL_TOKEN_AND_SPAN = 'otel.rack.token_and_span'
GOOD_HTTP_STATUSES = (100..499)

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

def add_response_attributes(span, response)
span.status = OpenTelemetry::Trace::Status.error unless GOOD_HTTP_STATUSES.include?(response.status.to_i)
span.status = OpenTelemetry::Trace::Status.error if response.server_error?
attributes = extract_response_attributes(response)
span.add_attributes(attributes)
rescue StandardError => e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def create_request_span_name(request_uri_or_path_info, env)
end

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

# NOTE: if data is available, it would be good to do this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@
_(proxy_event).must_be_nil
end

describe 'with a hijacked response' do
let(:service) do
lambda do |env|
env['rack.hijack?'] = true
[-1, {}, []]
end
end

it 'sets the span status to "unset"' do
_(rack_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
end
end

describe 'when baggage is set' do
let(:headers) do
Hash(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@
_(first_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
end

describe 'with a hijacked response' do
let(:app) do
lambda do |env|
env['rack.hijack?'] = true
[-1, {}, []]
end
end

it 'sets the span status to "unset"' do
_(first_span.status.code).must_equal OpenTelemetry::Trace::Status::UNSET
end
end

it 'has no parent' do
_(first_span.parent_span_id).must_equal OpenTelemetry::Trace::INVALID_SPAN_ID
end
Expand Down

0 comments on commit 5c647b5

Please sign in to comment.