UNPKG

@sentry/google-cloud-serverless

Version:
85 lines (76 loc) • 2.44 kB
Object.defineProperty(exports, '__esModule', { value: true }); const core = require('@sentry/core'); const node = require('@sentry/node'); const utils$1 = require('@sentry/utils'); const debugBuild = require('../debug-build.js'); const utils = require('../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 wrapCloudEventFunction( fn, wrapOptions = {}, ) { return utils.proxyFunction(fn, f => utils.domainify(_wrapCloudEventFunction(f, wrapOptions))); } function _wrapCloudEventFunction( fn, wrapOptions = {}, ) { const options = { flushTimeout: 2000, ...wrapOptions, }; return (context, callback) => { return node.startSpanManual( { name: context.type || '<unknown>', op: 'function.gcp.cloud_event', attributes: { [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component', [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event', }, }, span => { const scope = node.getCurrentScope(); scope.setContext('gcp.function.context', { ...context }); const newCallback = utils.domainify((...args) => { if (args[0] !== null && args[0] !== undefined) { node.captureException(args[0], scope => utils.markEventUnhandled(scope)); } span.end(); // eslint-disable-next-line @typescript-eslint/no-floating-promises node.flush(options.flushTimeout) .then(null, e => { debugBuild.DEBUG_BUILD && utils$1.logger.error(e); }) .then(() => { if (typeof callback === 'function') { callback(...args); } }); }); if (fn.length > 1) { return core.handleCallbackErrors( () => (fn )(context, newCallback), err => { node.captureException(err, scope => utils.markEventUnhandled(scope)); }, ); } return Promise.resolve() .then(() => (fn )(context)) .then( result => newCallback(null, result), err => newCallback(err, undefined), ); }, ); }; } exports.wrapCloudEventFunction = wrapCloudEventFunction; //# sourceMappingURL=cloud_events.js.map