Skip to content

Commit

Permalink
Merge pull request #2942 from newrelic/scombriformes
Browse files Browse the repository at this point in the history
aws-sdk-lambda: don't use external segments
  • Loading branch information
tannalynn authored Nov 13, 2024
2 parents 747f2a6 + fb9c1e7 commit e81c62c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/new_relic/agent/instrumentation/aws_sdk_lambda/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative 'instrumentation'

module NewRelic::Agent::Instrumentation
module AwsSdkLambda::Chain
def self.instrument!
Expand Down
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
2 changes: 2 additions & 0 deletions lib/new_relic/agent/instrumentation/aws_sdk_lambda/prepend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative 'instrumentation'

module NewRelic::Agent::Instrumentation
module AwsSdkLambda::Prepend
include NewRelic::Agent::Instrumentation::AwsSdkLambda
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 e81c62c

Please sign in to comment.