Skip to content

Commit e1d3633

Browse files
authored
ref(serverless): Avoid using pushScope (#9883)
This is the only place I've found where we use `pushScope`, and since we updated the `withScope` signature we can rewrite this.
1 parent f922414 commit e1d3633

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

packages/serverless/src/awslambda.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -319,35 +319,35 @@ export function wrapHandler<TEvent, TResult>(
319319
});
320320
}
321321

322-
const scope = hub.pushScope();
323-
let rv: TResult;
324-
try {
325-
enhanceScopeWithEnvironmentData(scope, context, START_TIME);
326-
if (options.startTrace) {
327-
enhanceScopeWithTransactionData(scope, context);
328-
// We put the transaction on the scope so users can attach children to it
329-
scope.setSpan(transaction);
330-
}
331-
rv = await asyncHandler(event, context);
332-
333-
// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
334-
if (options.captureAllSettledReasons && Array.isArray(rv) && isPromiseAllSettledResult(rv)) {
335-
const reasons = getRejectedReasons(rv);
336-
reasons.forEach(exception => {
337-
captureException(exception, scope => markEventUnhandled(scope));
322+
return withScope(async scope => {
323+
let rv: TResult;
324+
try {
325+
enhanceScopeWithEnvironmentData(scope, context, START_TIME);
326+
if (options.startTrace) {
327+
enhanceScopeWithTransactionData(scope, context);
328+
// We put the transaction on the scope so users can attach children to it
329+
scope.setSpan(transaction);
330+
}
331+
rv = await asyncHandler(event, context);
332+
333+
// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
334+
if (options.captureAllSettledReasons && Array.isArray(rv) && isPromiseAllSettledResult(rv)) {
335+
const reasons = getRejectedReasons(rv);
336+
reasons.forEach(exception => {
337+
captureException(exception, scope => markEventUnhandled(scope));
338+
});
339+
}
340+
} catch (e) {
341+
captureException(e, scope => markEventUnhandled(scope));
342+
throw e;
343+
} finally {
344+
clearTimeout(timeoutWarningTimer);
345+
transaction?.finish();
346+
await flush(options.flushTimeout).catch(e => {
347+
DEBUG_BUILD && logger.error(e);
338348
});
339349
}
340-
} catch (e) {
341-
captureException(e, scope => markEventUnhandled(scope));
342-
throw e;
343-
} finally {
344-
clearTimeout(timeoutWarningTimer);
345-
transaction?.finish();
346-
hub.popScope();
347-
await flush(options.flushTimeout).catch(e => {
348-
DEBUG_BUILD && logger.error(e);
349-
});
350-
}
351-
return rv;
350+
return rv;
351+
});
352352
};
353353
}

packages/serverless/test/awslambda.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ describe('AWSLambda', () => {
9595
});
9696

9797
test('captureTimeoutWarning enabled (default)', async () => {
98-
expect.assertions(2);
99-
10098
const handler: Handler = (_event, _context, callback) => {
10199
setTimeout(() => {
102100
callback(null, 42);
@@ -105,14 +103,13 @@ describe('AWSLambda', () => {
105103
const wrappedHandler = wrapHandler(handler);
106104
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
107105

106+
expect(Sentry.withScope).toBeCalledTimes(2);
108107
expect(Sentry.captureMessage).toBeCalled();
109108
// @ts-expect-error see "Why @ts-expect-error" note
110109
expect(SentryNode.fakeScope.setTag).toBeCalledWith('timeout', '1s');
111110
});
112111

113112
test('captureTimeoutWarning disabled', async () => {
114-
expect.assertions(2);
115-
116113
const handler: Handler = (_event, _context, callback) => {
117114
setTimeout(() => {
118115
callback(null, 42);
@@ -123,8 +120,10 @@ describe('AWSLambda', () => {
123120
});
124121
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
125122

126-
expect(Sentry.withScope).not.toBeCalled();
123+
expect(Sentry.withScope).toBeCalledTimes(1);
127124
expect(Sentry.captureMessage).not.toBeCalled();
125+
// @ts-expect-error see "Why @ts-expect-error" note
126+
expect(SentryNode.fakeScope.setTag).not.toBeCalledWith('timeout', '1s');
128127
});
129128

130129
test('captureTimeoutWarning with configured timeoutWarningLimit', async () => {

0 commit comments

Comments
 (0)