Skip to content

Commit 64a1b09

Browse files
authored
fix(serverless): Capture custom tags in GCP Background and CloudEvent function error events (#7301)
Fix adding custom tags for GCP Background and CloudEvent function error events. As for Http functions, we also previously captured errors just via `domain.on('error',...)` which used the wrong domain, leading to tags (+other data) not being captured correctly. With this fix, we now also directly capture exceptions during function invocation.
1 parent 7029da4 commit 64a1b09

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

packages/serverless/src/gcpfunction/cloud_events.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureException, flush, getCurrentHub } from '@sentry/node';
2-
import { logger } from '@sentry/utils';
2+
import { isThenable, logger } from '@sentry/utils';
33

44
import { domainify, getActiveDomain, proxyFunction } from '../utils';
55
import type { CloudEventFunction, CloudEventFunctionWithCallback, WrapperOptions } from './general';
@@ -49,8 +49,6 @@ function _wrapCloudEventFunction(
4949

5050
const activeDomain = getActiveDomain()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
5151

52-
activeDomain.on('error', captureException);
53-
5452
const newCallback = activeDomain.bind((...args: unknown[]) => {
5553
if (args[0] !== null && args[0] !== undefined) {
5654
captureException(args[0]);
@@ -67,7 +65,22 @@ function _wrapCloudEventFunction(
6765
});
6866

6967
if (fn.length > 1) {
70-
return (fn as CloudEventFunctionWithCallback)(context, newCallback);
68+
let fnResult;
69+
try {
70+
fnResult = (fn as CloudEventFunctionWithCallback)(context, newCallback);
71+
} catch (err) {
72+
captureException(err);
73+
throw err;
74+
}
75+
76+
if (isThenable(fnResult)) {
77+
fnResult.then(null, err => {
78+
captureException(err);
79+
throw err;
80+
});
81+
}
82+
83+
return fnResult;
7184
}
7285

7386
return Promise.resolve()

packages/serverless/src/gcpfunction/events.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureException, flush, getCurrentHub } from '@sentry/node';
2-
import { logger } from '@sentry/utils';
2+
import { isThenable, logger } from '@sentry/utils';
33

44
import { domainify, getActiveDomain, proxyFunction } from '../utils';
55
import type { EventFunction, EventFunctionWithCallback, WrapperOptions } from './general';
@@ -51,8 +51,6 @@ function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>
5151

5252
const activeDomain = getActiveDomain()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
5353

54-
activeDomain.on('error', captureException);
55-
5654
const newCallback = activeDomain.bind((...args: unknown[]) => {
5755
if (args[0] !== null && args[0] !== undefined) {
5856
captureException(args[0]);
@@ -71,7 +69,22 @@ function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>
7169
});
7270

7371
if (fn.length > 2) {
74-
return (fn as EventFunctionWithCallback)(data, context, newCallback);
72+
let fnResult;
73+
try {
74+
fnResult = (fn as EventFunctionWithCallback)(data, context, newCallback);
75+
} catch (err) {
76+
captureException(err);
77+
throw err;
78+
}
79+
80+
if (isThenable(fnResult)) {
81+
fnResult.then(null, err => {
82+
captureException(err);
83+
throw err;
84+
});
85+
}
86+
87+
return fnResult;
7588
}
7689

7790
return Promise.resolve()

0 commit comments

Comments
 (0)