Skip to content

Commit ac57eb1

Browse files
authored
ref(google-cloud-serverless): Add mechanism.type to captured errors (#17300)
adjusts the event `mechanism.type` value of GCP serverless events, following the trace origin naming scheme. Values are now identical with the `sentry.origin` span attribute values of the enclosing spans. ref #17212 closes #17257
1 parent 5ae054c commit ac57eb1

File tree

7 files changed

+72
-8
lines changed

7 files changed

+72
-8
lines changed

packages/google-cloud-serverless/src/gcpfunction/cloud_events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function _wrapCloudEventFunction(
4949

5050
const newCallback = domainify((...args: unknown[]) => {
5151
if (args[0] !== null && args[0] !== undefined) {
52-
captureException(args[0], scope => markEventUnhandled(scope));
52+
captureException(args[0], scope => markEventUnhandled(scope, 'auto.function.serverless.gcp_cloud_event'));
5353
}
5454
span.end();
5555

@@ -69,7 +69,7 @@ function _wrapCloudEventFunction(
6969
return handleCallbackErrors(
7070
() => (fn as CloudEventFunctionWithCallback)(context, newCallback),
7171
err => {
72-
captureException(err, scope => markEventUnhandled(scope));
72+
captureException(err, scope => markEventUnhandled(scope, 'auto.function.serverless.gcp_cloud_event'));
7373
},
7474
);
7575
}

packages/google-cloud-serverless/src/gcpfunction/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>
5252

5353
const newCallback = domainify((...args: unknown[]) => {
5454
if (args[0] !== null && args[0] !== undefined) {
55-
captureException(args[0], scope => markEventUnhandled(scope));
55+
captureException(args[0], scope => markEventUnhandled(scope, 'auto.function.serverless.gcp_event'));
5656
}
5757
span.end();
5858

@@ -72,7 +72,7 @@ function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>
7272
return handleCallbackErrors(
7373
() => (fn as EventFunctionWithCallback)(data, context, newCallback),
7474
err => {
75-
captureException(err, scope => markEventUnhandled(scope));
75+
captureException(err, scope => markEventUnhandled(scope, 'auto.function.serverless.gcp_event'));
7676
},
7777
);
7878
}

packages/google-cloud-serverless/src/gcpfunction/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function _wrapHttpFunction(fn: HttpFunction, options: Partial<WrapperOptions>):
7878
return handleCallbackErrors(
7979
() => fn(req, res),
8080
err => {
81-
captureException(err, scope => markEventUnhandled(scope));
81+
captureException(err, scope => markEventUnhandled(scope, 'auto.function.serverless.gcp_http'));
8282
},
8383
);
8484
},

packages/google-cloud-serverless/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export function proxyFunction<A extends any[], R, F extends (...args: A) => R>(
4343
/**
4444
* Marks an event as unhandled by adding a span processor to the passed scope.
4545
*/
46-
export function markEventUnhandled(scope: Scope): Scope {
46+
export function markEventUnhandled(scope: Scope, type: string): Scope {
4747
scope.addEventProcessor(event => {
48-
addExceptionMechanism(event, { handled: false });
48+
addExceptionMechanism(event, { handled: false, type });
4949
return event;
5050
});
5151

packages/google-cloud-serverless/test/gcpfunction/cloud_event.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ describe('wrapCloudEventFunction', () => {
134134

135135
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
136136
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
137+
138+
const scopeFunction = mockCaptureException.mock.calls[0][1];
139+
const event: Event = { exception: { values: [{}] } };
140+
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
141+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
142+
143+
expect(evtProcessor).toBeInstanceOf(Function);
144+
// @ts-expect-error just mocking around...
145+
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
146+
handled: false,
147+
type: 'auto.function.serverless.gcp_cloud_event',
148+
});
149+
137150
expect(mockSpan.end).toBeCalled();
138151
expect(mockFlush).toBeCalled();
139152
});
@@ -158,6 +171,19 @@ describe('wrapCloudEventFunction', () => {
158171

159172
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
160173
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
174+
175+
const scopeFunction = mockCaptureException.mock.calls[0][1];
176+
const event: Event = { exception: { values: [{}] } };
177+
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
178+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
179+
180+
expect(evtProcessor).toBeInstanceOf(Function);
181+
// @ts-expect-error just mocking around...
182+
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
183+
handled: false,
184+
type: 'auto.function.serverless.gcp_cloud_event',
185+
});
186+
161187
expect(mockSpan.end).toBeCalled();
162188
expect(mockFlush).toBeCalled();
163189
});
@@ -204,6 +230,19 @@ describe('wrapCloudEventFunction', () => {
204230

205231
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
206232
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
233+
234+
const scopeFunction = mockCaptureException.mock.calls[0][1];
235+
const event: Event = { exception: { values: [{}] } };
236+
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
237+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
238+
239+
expect(evtProcessor).toBeInstanceOf(Function);
240+
// @ts-expect-error just mocking around...
241+
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
242+
handled: false,
243+
type: 'auto.function.serverless.gcp_cloud_event',
244+
});
245+
207246
expect(mockSpan.end).toBeCalled();
208247
expect(mockFlush).toBeCalled();
209248
});
@@ -227,6 +266,18 @@ describe('wrapCloudEventFunction', () => {
227266

228267
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
229268
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
269+
270+
const scopeFunction = mockCaptureException.mock.calls[0][1];
271+
const event: Event = { exception: { values: [{}] } };
272+
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
273+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
274+
275+
expect(evtProcessor).toBeInstanceOf(Function);
276+
// @ts-expect-error just mocking around...
277+
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
278+
handled: false,
279+
type: 'auto.function.serverless.gcp_cloud_event',
280+
});
230281
});
231282
});
232283

packages/google-cloud-serverless/test/gcpfunction/events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('wrapEventFunction', () => {
245245
// @ts-expect-error just mocking around...
246246
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
247247
handled: false,
248-
type: 'generic',
248+
type: 'auto.function.serverless.gcp_event',
249249
});
250250
});
251251

packages/google-cloud-serverless/test/gcpfunction/http.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ describe('GCPFunction', () => {
125125

126126
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
127127
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
128+
129+
const scopeFunction = mockCaptureException.mock.calls[0][1];
130+
const event: Event = { exception: { values: [{}] } };
131+
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
132+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
133+
134+
expect(evtProcessor).toBeInstanceOf(Function);
135+
// @ts-expect-error just mocking around...
136+
expect(evtProcessor(event).exception.values[0]?.mechanism).toEqual({
137+
handled: false,
138+
type: 'auto.function.serverless.gcp_http',
139+
});
140+
128141
expect(mockSpan.end).toBeCalled();
129142
expect(mockFlush).toBeCalled();
130143
});

0 commit comments

Comments
 (0)