Skip to content

Commit

Permalink
ENG-53423 add handling for empty responses
Browse files Browse the repository at this point in the history
  • Loading branch information
prodion23 committed Nov 21, 2024
1 parent 1c615f5 commit 4b3df64
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
30 changes: 24 additions & 6 deletions src/instrumentation/ExtendedAwsLambdaInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export class ExtendedAwsLambdaInstrumentation extends AwsLambdaInstrumentation {

// @ts-ignore
if (config.requestHook) {
// @ts-ignore
config.requestHook(span, {event, context: lambdaContext});
try{
// @ts-ignore
config.requestHook(span, {event, context: lambdaContext});
}catch(e){
logger.debug("Error processing request hook")
logger.debug(e)
}

}

return otelContext.with(trace.setSpan(otelContext.active(), span), async () => {
Expand All @@ -35,8 +41,14 @@ export class ExtendedAwsLambdaInstrumentation extends AwsLambdaInstrumentation {

// @ts-ignore
if (config.responseHook) {
// @ts-ignore
config.responseHook(span, {err: null, res: result});
try{
// @ts-ignore
config.responseHook(span, {err: null, res: result});
}catch(e){
logger.debug("Error processing success state response hook")
logger.debug(e)
}

}

span.end();
Expand All @@ -52,8 +64,14 @@ export class ExtendedAwsLambdaInstrumentation extends AwsLambdaInstrumentation {
} catch (error) {
// @ts-ignore
if (config.responseHook) {
// @ts-ignore
config.responseHook(span, {err: error});
try{
// @ts-ignore
config.responseHook(span, {err: error});
}catch(e){
logger.debug("Error processing error state response hook")
logger.debug(e)
}

}

span.recordException(error);
Expand Down
2 changes: 1 addition & 1 deletion src/instrumentation/LambdaInstrumentationWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function LambdaRequestHook(span, {event, context}){
}

export function LambdaResponseHook(span, {err, res}) : void {
if(err && !res){
if(err || !res){
return
}
let statusCode = res['statusCode']
Expand Down
14 changes: 14 additions & 0 deletions test/instrumentation/LambdaInstrumentationWrapperTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,18 @@ describe("manually instrument lambda function", () => {
}
expect(errorCount).to.equal(0)
})

it('can handle non-apigateway events without a response gracefully', async () => {
async function myHandler(event, context, callback){
}

let wrappedHandler = agentTestWrapper.instrumentLambda(myHandler)
let errorCount = 0
try {
await wrappedHandler({"some-different-event": "foo"}, {}, () => {})
} catch(error){
errorCount += 1
}
expect(errorCount).to.equal(0)
})
})

0 comments on commit 4b3df64

Please sign in to comment.