diff --git a/packages/serverless/src/awslambda.ts b/packages/serverless/src/awslambda.ts index aff1e58675f5..3f6ed2ad0ef8 100644 --- a/packages/serverless/src/awslambda.ts +++ b/packages/serverless/src/awslambda.ts @@ -319,35 +319,35 @@ export function wrapHandler( }); } - const scope = hub.pushScope(); - let rv: TResult; - try { - enhanceScopeWithEnvironmentData(scope, context, START_TIME); - if (options.startTrace) { - enhanceScopeWithTransactionData(scope, context); - // We put the transaction on the scope so users can attach children to it - scope.setSpan(transaction); - } - rv = await asyncHandler(event, context); - - // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises - if (options.captureAllSettledReasons && Array.isArray(rv) && isPromiseAllSettledResult(rv)) { - const reasons = getRejectedReasons(rv); - reasons.forEach(exception => { - captureException(exception, scope => markEventUnhandled(scope)); + return withScope(async scope => { + let rv: TResult; + try { + enhanceScopeWithEnvironmentData(scope, context, START_TIME); + if (options.startTrace) { + enhanceScopeWithTransactionData(scope, context); + // We put the transaction on the scope so users can attach children to it + scope.setSpan(transaction); + } + rv = await asyncHandler(event, context); + + // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises + if (options.captureAllSettledReasons && Array.isArray(rv) && isPromiseAllSettledResult(rv)) { + const reasons = getRejectedReasons(rv); + reasons.forEach(exception => { + captureException(exception, scope => markEventUnhandled(scope)); + }); + } + } catch (e) { + captureException(e, scope => markEventUnhandled(scope)); + throw e; + } finally { + clearTimeout(timeoutWarningTimer); + transaction?.finish(); + await flush(options.flushTimeout).catch(e => { + DEBUG_BUILD && logger.error(e); }); } - } catch (e) { - captureException(e, scope => markEventUnhandled(scope)); - throw e; - } finally { - clearTimeout(timeoutWarningTimer); - transaction?.finish(); - hub.popScope(); - await flush(options.flushTimeout).catch(e => { - DEBUG_BUILD && logger.error(e); - }); - } - return rv; + return rv; + }); }; } diff --git a/packages/serverless/test/awslambda.test.ts b/packages/serverless/test/awslambda.test.ts index a3085c8b0f65..5c67c8481d4a 100644 --- a/packages/serverless/test/awslambda.test.ts +++ b/packages/serverless/test/awslambda.test.ts @@ -95,8 +95,6 @@ describe('AWSLambda', () => { }); test('captureTimeoutWarning enabled (default)', async () => { - expect.assertions(2); - const handler: Handler = (_event, _context, callback) => { setTimeout(() => { callback(null, 42); @@ -105,14 +103,13 @@ describe('AWSLambda', () => { const wrappedHandler = wrapHandler(handler); await wrappedHandler(fakeEvent, fakeContext, fakeCallback); + expect(Sentry.withScope).toBeCalledTimes(2); expect(Sentry.captureMessage).toBeCalled(); // @ts-expect-error see "Why @ts-expect-error" note expect(SentryNode.fakeScope.setTag).toBeCalledWith('timeout', '1s'); }); test('captureTimeoutWarning disabled', async () => { - expect.assertions(2); - const handler: Handler = (_event, _context, callback) => { setTimeout(() => { callback(null, 42); @@ -123,8 +120,10 @@ describe('AWSLambda', () => { }); await wrappedHandler(fakeEvent, fakeContext, fakeCallback); - expect(Sentry.withScope).not.toBeCalled(); + expect(Sentry.withScope).toBeCalledTimes(1); expect(Sentry.captureMessage).not.toBeCalled(); + // @ts-expect-error see "Why @ts-expect-error" note + expect(SentryNode.fakeScope.setTag).not.toBeCalledWith('timeout', '1s'); }); test('captureTimeoutWarning with configured timeoutWarningLimit', async () => {