11// '@google-cloud/functions-framework/build/src/functions' import is expected to be type-only so it's erased in the final .js file.
22// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here.
33import { HttpFunction } from '@google-cloud/functions-framework/build/src/functions' ;
4- import { captureException , flush , getCurrentHub , Handlers , startTransaction , withScope } from '@sentry/node' ;
4+ import { captureException , flush , getCurrentHub , Handlers , startTransaction } from '@sentry/node' ;
55import { logger , stripUrlQueryAndFragment } from '@sentry/utils' ;
66
7- import { addServerlessEventProcessor , getActiveDomain , WrapperOptions } from './general' ;
7+ import { getActiveDomain , WrapperOptions } from './general' ;
88
99type Request = Parameters < HttpFunction > [ 0 ] ;
1010type Response = Parameters < HttpFunction > [ 1 ] ;
@@ -18,21 +18,6 @@ export { Request, Response };
1818
1919const { parseRequest } = Handlers ;
2020
21- /**
22- * Capture exception with additional request information.
23- *
24- * @param e exception to be captured
25- * @param req incoming request
26- * @param options request capture options
27- */
28- function captureRequestError ( e : unknown , req : Request , options : ParseRequestOptions ) : void {
29- withScope ( scope => {
30- addServerlessEventProcessor ( scope ) ;
31- scope . addEventProcessor ( event => parseRequest ( event , req , options ) ) ;
32- captureException ( e ) ;
33- } ) ;
34- }
35-
3621/**
3722 * Wraps an HTTP function handler adding it error capture and tracing capabilities.
3823 *
@@ -58,8 +43,12 @@ export function wrapHttpFunction(
5843 op : 'gcp.function.http' ,
5944 } ) ;
6045
61- // We put the transaction on the scope so users can attach children to it
46+ // getCurrentHub() is expected to use current active domain as a carrier
47+ // since functions-framework creates a domain for each incoming request.
48+ // So adding of event processors every time should not lead to memory bloat.
6249 getCurrentHub ( ) . configureScope ( scope => {
50+ scope . addEventProcessor ( event => parseRequest ( event , req , options . parseRequestOptions ) ) ;
51+ // We put the transaction on the scope so users can attach children to it
6352 scope . setSpan ( transaction ) ;
6453 } ) ;
6554
@@ -71,7 +60,7 @@ export function wrapHttpFunction(
7160 // functions-framework creates a domain for each incoming request so we take advantage of this fact and add an error handler.
7261 // BTW this is the only way to catch any exception occured during request lifecycle.
7362 getActiveDomain ( ) . on ( 'error' , err => {
74- captureRequestError ( err , req , options . parseRequestOptions ) ;
63+ captureException ( err ) ;
7564 } ) ;
7665
7766 // eslint-disable-next-line @typescript-eslint/unbound-method
0 commit comments