Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set span error only for 5xx response range #1196

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Contributor Author

@j15e j15e Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found out that here we have access to the final Rack::Response object so this neat server_error? method can be re-used (not the case in the middleware instrument version though)

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)
arielvalentin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading