Skip to content

Commit

Permalink
fix: Fixed matching for Lambda events containing resource but which a…
Browse files Browse the repository at this point in the history
…ren't API Gateway (#2825)
  • Loading branch information
Engerim authored Dec 9, 2024
1 parent 54f034b commit 64d2042
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/serverless/api-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const requiredHttpApiV1Keys = [

// See https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
function isGatewayV1Event(event) {
if (event?.requestContext?.elb !== undefined) {
if (event?.requestContext === undefined || event?.requestContext?.elb !== undefined) {
return false
}
const hasKeys = eventHasRequiredKeys(event, requiredHttpApiV1Keys)
Expand All @@ -173,7 +173,7 @@ function isGatewayV1Event(event) {
const requiredHttpApiV2Keys = ['rawPath', 'rawQueryString', 'routeKey', 'cookies']

function isGatewayV2Event(event) {
if (event?.requestContext?.elb !== undefined) {
if (event?.requestContext === undefined || event?.requestContext?.elb !== undefined) {
return false
}
return eventHasRequiredKeys(event, requiredHttpApiV2Keys) && event?.version === '2.0'
Expand Down
42 changes: 42 additions & 0 deletions test/unit/serverless/aws-lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ test('AwsLambda.patchLambdaHandler', async (t) => {
body: 'worked'
}

await t.test(
'should not create web transaction for custom direct invocation payload',
(t, end) => {
const { agent, awsLambda, stubContext, stubCallback } = t.nr
agent.on('transactionFinished', confirmAgentAttribute)

const nonApiGatewayProxyEvent = {
resource: {
some: 'key'
},
action: 'someAction'
}

const wrappedHandler = awsLambda.patchLambdaHandler((event, context, callback) => {
const transaction = agent.tracer.getTransaction()

assert.ok(transaction)
assert.equal(transaction.type, 'bg')
assert.equal(transaction.getFullName(), expectedBgTransactionName)
assert.equal(transaction.isActive(), true)

callback(null, validResponse)
})

wrappedHandler(nonApiGatewayProxyEvent, stubContext, stubCallback)

function confirmAgentAttribute(transaction) {
const agentAttributes = transaction.trace.attributes.get(ATTR_DEST.TRANS_EVENT)
const segment = transaction.baseSegment
const spanAttributes = segment.attributes.get(ATTR_DEST.SPAN_EVENT)

assert.equal(agentAttributes['request.method'], undefined)
assert.equal(agentAttributes['request.uri'], undefined)

assert.equal(spanAttributes['request.method'], undefined)
assert.equal(spanAttributes['request.uri'], undefined)

end()
}
}
)

await t.test('should create web transaction', (t, end) => {
const { agent, awsLambda, stubContext, stubCallback } = t.nr
agent.on('transactionFinished', confirmAgentAttribute)
Expand Down
11 changes: 10 additions & 1 deletion test/unit/serverless/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,20 @@ const lambaV1InvocationEvent = {
}
}

// Event which contains `resource` key and should not be a web event.
const lambdaEvent = {
someKey: 'someValue',
resource: {
otherKey: 'otherValue'
}
}

module.exports = {
restApiGatewayV1Event,
httpApiGatewayV1Event,
httpApiGatewayV2Event,
httpApiGatewayV2EventAlt,
albEvent,
lambaV1InvocationEvent
lambaV1InvocationEvent,
lambdaEvent
}
6 changes: 5 additions & 1 deletion test/unit/serverless/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const {
httpApiGatewayV2Event,
httpApiGatewayV2EventAlt,
lambaV1InvocationEvent,
albEvent
albEvent,
lambdaEvent
} = require('./fixtures')

test('isGatewayV1Event', () => {
Expand All @@ -30,6 +31,7 @@ test('isGatewayV1Event', () => {
assert.equal(isGatewayV1Event(httpApiGatewayV2EventAlt), false)
assert.equal(isGatewayV1Event(lambaV1InvocationEvent), false)
assert.equal(isGatewayV1Event(albEvent), false)
assert.equal(isGatewayV1Event(lambdaEvent), false)
})

test('isGatewayV2Event', () => {
Expand All @@ -39,6 +41,7 @@ test('isGatewayV2Event', () => {
assert.equal(isGatewayV2Event(httpApiGatewayV2EventAlt), true)
assert.equal(isGatewayV2Event(lambaV1InvocationEvent), false)
assert.equal(isGatewayV2Event(albEvent), false)
assert.equal(isGatewayV2Event(lambdaEvent), false)
})

test('isAlbEvent', () => {
Expand All @@ -48,4 +51,5 @@ test('isAlbEvent', () => {
assert.equal(isAlbEvent(httpApiGatewayV2EventAlt), false)
assert.equal(isAlbEvent(lambaV1InvocationEvent), false)
assert.equal(isAlbEvent(albEvent), true)
assert.equal(isAlbEvent(lambdaEvent), false)
})

0 comments on commit 64d2042

Please sign in to comment.