UNPKG

@sentry/google-cloud-serverless

Version:
86 lines (77 loc) • 2.48 kB
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, handleCallbackErrors } from '@sentry/core'; import { startSpanManual, getCurrentScope, captureException, flush } from '@sentry/node'; import { logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../debug-build.js'; import { proxyFunction, domainify, markEventUnhandled } from '../utils.js'; /** * Wraps an event function handler adding it error capture and tracing capabilities. * * @param fn Event handler * @param options Options * @returns Event handler */ function wrapEventFunction( fn, wrapOptions = {}, ) { return proxyFunction(fn, f => domainify(_wrapEventFunction(f, wrapOptions))); } /** */ function _wrapEventFunction( fn, wrapOptions = {}, ) { const options = { flushTimeout: 2000, ...wrapOptions, }; return (...eventFunctionArguments) => { const [data, context, callback] = eventFunctionArguments; return startSpanManual( { name: context.eventType, op: 'function.gcp.event', attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component', [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_event', }, }, span => { const scope = getCurrentScope(); scope.setContext('gcp.function.context', { ...context }); const newCallback = domainify((...args) => { if (args[0] !== null && args[0] !== undefined) { captureException(args[0], scope => markEventUnhandled(scope)); } span.end(); // eslint-disable-next-line @typescript-eslint/no-floating-promises flush(options.flushTimeout) .then(null, e => { DEBUG_BUILD && logger.error(e); }) .then(() => { if (typeof callback === 'function') { callback(...args); } }); }); if (fn.length > 2) { return handleCallbackErrors( () => (fn )(data, context, newCallback), err => { captureException(err, scope => markEventUnhandled(scope)); }, ); } return Promise.resolve() .then(() => (fn )(data, context)) .then( result => newCallback(null, result), err => newCallback(err, undefined), ); }, ); }; } export { wrapEventFunction }; //# sourceMappingURL=events.js.map