Skip to content

Commit

Permalink
aws-sdk-lambda: don't use external segments
Browse files Browse the repository at this point in the history
the use of external request segments can cause confusion for Lambda
entity relationships. should we wish to expose the underlying net call
to the AWS back-end API in future, we can do so by simply removing the
`disable_all_tracing` wrapper and allow the underlying Net::HTTP call to
be instrumented.
  • Loading branch information
fallwith committed Nov 11, 2024
1 parent 927a31d commit f15914e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ def with_tracing(action, *args)
end

def process_response(response, segment)
process_status_code(response, segment) if response.respond_to?(:status_code)
process_function_error(response) if response.respond_to?(:function_error)
rescue => e
NewRelic::Agent.logger.error("Error processing aws-sdk-lambda invocation response: #{e}")
end

def process_status_code(response, segment)
status_code = response.status_code
return unless status_code

segment.process_response_headers(WRAPPED_RESPONSE.new(status_code, true))
end

# notice error that was raised / unhandled by the function
def process_function_error(response)
function_error = response.function_error
Expand All @@ -73,18 +65,11 @@ def generate_segment(action, options = {})
region = aws_region
account_id = aws_account_id
arn = aws_arn(function, region)

segment = NewRelic::Agent::Tracer.start_external_request_segment(
library: INSTRUMENTATION_NAME,
uri: "https://lambda.#{region || 'unknown-region'}.amazonaws.com",
procedure: action
)
segment.name = "External/Lambda/#{action}/#{function}"
segment = NewRelic::Agent::Tracer.start_segment(name: "Lambda/#{action}/#{function}")
segment.add_agent_attribute('cloud.account.id', account_id)
segment.add_agent_attribute('cloud.platform', CLOUD_PLATFORM)
segment.add_agent_attribute('cloud.region', region)
segment.add_agent_attribute('cloud.resource_id', arn) if arn

segment
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_client_call_raises_an_exception

NewRelic::Agent::Tracer.stub(:capture_segment_error,
capture_proc,
[NewRelic::Agent::Transaction::ExternalRequestSegment]) do
[NewRelic::Agent::Transaction::Segment]) do
assert_raises(RuntimeError) { client.invoke(function_name: 'Invoke-Me-And-Explode') }
end

Expand All @@ -69,7 +69,7 @@ def test_errors_in_processing_the_invocation_response_are_logged_but_not_raised_
response = {status_code: 200}
client = Aws::Lambda::Client.new(region: REGION, stub_responses: {invoke: response})
client.config.account_id = AWS_ACCOUNT_ID
def client.process_status_code(*_args); raise 'kaboom'; end
def client.process_function_error(*_args); raise 'kaboom'; end

NewRelic::Agent.stub(:logger, NewRelic::Agent::MemoryLogger.new) do
client.invoke(function_name: 'Invoke-Me-And-Only-The-Agent-Explodes')
Expand Down Expand Up @@ -123,10 +123,7 @@ def perform_invocation(method, response = {}, extra_args = {})

segment = lambda_segment(txn)

assert_equal("External/Lambda/#{method}/#{function_name}", segment.name)
assert_equal("lambda.#{REGION}.amazonaws.com", segment.host)
assert_equal(200, segment.http_status_code) unless method == :invoke_async
assert_equal('aws_sdk_lambda', segment.library) # rubocop:disable Minitest/EmptyLineBeforeAssertionMethods
assert_equal("Lambda/#{method}/#{function_name}", segment.name)
assert_equal({'cloud.platform' => 'aws_lambda',
'cloud.region' => REGION,
'cloud.account.id' => AWS_ACCOUNT_ID,
Expand Down

0 comments on commit f15914e

Please sign in to comment.