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

Amazon API Gateway HTTP API (payload v2) event not detected correctly #2753

Closed
joshuapmorgan opened this issue Nov 19, 2024 · 2 comments · Fixed by #2780
Closed

Amazon API Gateway HTTP API (payload v2) event not detected correctly #2753

joshuapmorgan opened this issue Nov 19, 2024 · 2 comments · Fixed by #2780
Assignees
Labels

Comments

@joshuapmorgan
Copy link

Description

The Node.js agent incorrectly assumes that all fields are present in an API Gateway v2 proxy event object. While this appears to be true for API Gateway v1 proxy event, it is not true for API Gateway v2. For example, in v1, if there are no stage variables configured on the API Gateway stage, the stageVariables field in the proxy event is null. However, in v2, the field is absent. This applies to other fields - for example, if there are not path parameters or query string parameters in the request, the pathParameters and queryStringParameters fields are absent from the event object.

The following logic assumes that all fields are present in API Gateway v2 proxy event.

const httpApiV2Keys = [
'body',
'cookies',
'headers',
'isBase64Encoded',
'pathParameters',
'queryStringParameters',
'rawPath',
'rawQueryString',
'requestContext',
'routeKey',
'stageVariables',
'version'
].join(',')
function isGatewayV2Event(event) {
const keys = Object.keys(event).sort().join(',')
return keys === httpApiV2Keys && event?.version === '2.0'
}

The implication is that isGatewayV2Event is returning false for valid API Gateway v2 proxy events. In turn, this means that isApiGatewayLambdaProxy is false. For my use case, this meant that tracing headers were not being respected in the proxy event and a new trace was created, rather than adding segments to the existing trace.

The workaround is to set the payload version to 1.0 for API Gateway routes.

Expected Behavior

API Gateway v2 proxy events with absent fields are recognised as being valid. In turn, trace headers are pulled out of the event and used for distributed tracing.

Steps to Reproduce

  1. Deploy attached CloudFormation template (drop .txt extension, if required). This template creates an Lambda function, API Gateway HTTP API, and sets up an associated integration and route.
  2. Perform a HTTP GET to the / route of the API. Observe in the Lambda function's logs that the stageVariables key among others (for example, queryStringParameters and pathParameters) is absent in the event object.

Your Environment

AWS Lambda, nodejs18.x
newrelic 12.7.0
template.yaml.txt

@workato-integration
Copy link

@newrelic-node-agent-team newrelic-node-agent-team moved this to Triage Needed: Unprioritized Features in Node.js Engineering Board Nov 19, 2024
@joshuapmorgan joshuapmorgan changed the title AWS API Gateway HTTP API (payload v2) event not detected correctly Amazon API Gateway HTTP API (payload v2) event not detected correctly Nov 19, 2024
@mrickard
Copy link
Member

@joshuapmorgan Thank you for bringing this to our attention, and for attaching a repro. We have this in the queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants