Skip to content

Commit b76fbae

Browse files
authored
Set spans as errored in AWS integration when AWS requests fail (#4672)
1 parent 27d3cbe commit b76fbae

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

lib/datadog/tracing/contrib/aws/instrumentation.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ def annotate!(span, context)
3535
span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
3636
span.name = Ext::SPAN_COMMAND
3737
span.resource = context.safely(:resource)
38+
39+
# Set error on the span if the Response Status Code is in error range
40+
if Tracing::Metadata::Ext::HTTP::ERROR_RANGE.cover?(context.safely(:status_code))
41+
# At this point we do not have any additional diagnostics
42+
# besides the HTTP status code which is recorded in the span tags
43+
# later in this method.
44+
# Just set the span as errored.
45+
span.set_error(nil)
46+
end
47+
3848
aws_service = span.resource.split('.')[0]
3949
span.set_tag(Ext::TAG_AWS_SERVICE, aws_service)
4050
params = context.safely(:params)

lib/datadog/tracing/contrib/aws/parsed_context.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ def params
2626
context.params
2727
end
2828

29+
def http_response
30+
context.http_response
31+
end
32+
2933
def status_code
30-
context.http_response.status_code
34+
http_response.status_code
3135
end
3236

3337
def http_method

lib/datadog/tracing/metadata/errors.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ module Metadata
1010
# Adds error tagging behavior
1111
# @public_api
1212
module Errors
13-
def set_error(e)
13+
def set_error(error)
1414
Datadog::Core.log_deprecation do
1515
'Errors.set_error(..) is deprecated. ' \
1616
'Use Errors.set_error_tags(..) instead.'
1717
end
18-
set_error_tags(e)
18+
set_error_tags(error)
1919
end
2020

2121
# Mark the span with the given error.
22-
def set_error_tags(e)
23-
e = Core::Error.build_from(e)
22+
def set_error_tags(error)
23+
e = Core::Error.build_from(error)
2424

2525
set_tag(Ext::Errors::TAG_TYPE, e.type) unless e.type.empty?
2626
set_tag(Ext::Errors::TAG_MSG, e.message) unless e.message.empty?

spec/datadog/tracing/contrib/aws/instrumentation_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
it 'generates a span' do
5555
expect(span.name).to eq('aws.command')
56+
expect(span).not_to have_error
5657
expect(span.service).to eq('aws')
5758
expect(span.type).to eq('http')
5859
expect(span.resource).to eq('sts.get_access_key_info')
@@ -133,6 +134,31 @@
133134
end
134135
end
135136

137+
context 'when the client runs and the API returns an error' do
138+
subject(:list_buckets) { client.list_buckets }
139+
140+
let(:client) { ::Aws::S3::Client.new(stub_responses: true) }
141+
142+
before do
143+
client.stub_responses(
144+
:list_buckets,
145+
status_code: 500,
146+
body: 'test body with 500 error',
147+
headers: {}
148+
)
149+
end
150+
151+
it 'generates an errored span' do
152+
expect do
153+
list_buckets
154+
end.to raise_error(Aws::S3::Errors::Http500Error)
155+
# The Http500Error instance does not contain the body of the
156+
# response.
157+
expect(span).to have_error
158+
expect(span.tags['http.status_code']).to eq('500')
159+
end
160+
end
161+
136162
describe '#list_objects' do
137163
subject!(:list_objects) { client.list_objects(bucket: 'bucketname', max_keys: 2) }
138164

spec/datadog/tracing/contrib/aws/parsed_context_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
http_method: 'GET',
2828
region: 'us-west-2',
2929
retry_attempts: 0,
30-
path: '/'
30+
path: '/',
31+
http_response: be_kind_of(Seahorse::Client::Http::Response),
3132
)
3233
end
3334

0 commit comments

Comments
 (0)