Skip to content

Commit

Permalink
fix(lambda): Record the statusCode and errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ferenc Géczi <ferenc.geczi@ibm.com>
  • Loading branch information
Ferenc- committed Jan 8, 2024
1 parent 96f2618 commit 2bf5520
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/instana/instrumentation/aws_sdk_lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,31 @@ def call(context)
type: context.params[:invocation_type]
}.reject { |_, v| v.nil? }

::Instana.tracer.trace(:"aws.lambda.invoke", {aws: {lambda: {invoke: tags}}}) { @handler.call(context) }
span = ::Instana.tracer.log_entry(:"aws.lambda.invoke", {aws: {lambda: {invoke: tags}}})
begin
response = @handler.call(context)
if response.respond_to? :status_code
span.set_tags(:http => {:status => response.status_code })

# It can happen, that despite a 200 status code there are errors set in the function_error:
# https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/Lambda/Types/
# InvocationResponse.html#function_error-instance_method
if response.respond_to?(:function_error) && !response.function_error.nil?
span.add_error(nil)
span.set_tags(set_tags(:http => { :error => response.function_error }))
# Or In the unlikely scenario in which we somehow didn't get an exception from @handler.call
# but a 500 status_code was somehow still set without function_error
elsif response.status_code >= 500
span.add_error(nil)
end
end
response
rescue => e
::Instana.tracer.log_error(e)
raise
ensure
::Instana.tracer.log_exit(:"aws.lambda.invoke")
end
end
end

Expand Down
44 changes: 44 additions & 0 deletions test/instrumentation/aws_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,49 @@ def test_lambda
assert_equal :"aws.lambda.invoke", lambda_span[:n]
assert_equal 'Test', lambda_span[:data][:aws][:lambda][:invoke][:function]
assert_equal 'Event', lambda_span[:data][:aws][:lambda][:invoke][:type]
assert_equal 200, lambda_span[:data][:http][:status]
assert_nil lambda_span[:ec]
assert_nil lambda_span[:stack]
end

def test_lambda_with_500_status
stub_request(:post, "https://lambda.local.amazonaws.com/2015-03-31/functions/Test/invocations")
.with(
body: "data",
headers: {
'X-Amz-Client-Context' => /.+/
}
)
.to_return(status: 500, body: '{"message": "Internal Server Error" }', headers: {})

lambda = Aws::Lambda::Client.new(
endpoint: 'https://lambda.local.amazonaws.com',
region: 'local',
access_key_id: "test",
secret_access_key: "test"
)

assert_raises(RuntimeError) do
Instana::Tracer.start_or_continue_trace(:lambda_test, {}) do
lambda.invoke(
function_name: 'Test',
invocation_type: 'Event',
payload: 'data'
)
end
end

spans = ::Instana.processor.queued_spans
lambda_span, _entry_span, *rest = spans

assert rest.empty?

assert_equal :"aws.lambda.invoke", lambda_span[:n]
assert_equal 'Test', lambda_span[:data][:aws][:lambda][:invoke][:function]
assert_equal 'Event', lambda_span[:data][:aws][:lambda][:invoke][:type]
refute_nil lambda_span[:ec]
assert_equal 1, lambda_span[:ec]
refute_nil lambda_span[:stack]
assert_equal 30, lambda_span[:stack].length # default limit is 30 in span.add_stack
end
end

0 comments on commit 2bf5520

Please sign in to comment.