UNPKG

@sentry/google-cloud-serverless

Version:
73 lines (69 loc) • 2.89 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const attributes = require('@sentry/conventions/attributes'); const op = require('@sentry/conventions/op'); const core = require('@sentry/core'); const node = require('@sentry/node'); const debugBuild = require('../debug-build.js'); const utils = require('../utils.js'); function wrapEventFunction(fn, wrapOptions = {}) { return utils.proxyFunction(fn, (f) => utils.domainify(_wrapEventFunction(f, wrapOptions))); } function _wrapEventFunction(fn, wrapOptions = {}) { const options = { flushTimeout: 2e3, ...wrapOptions }; return (...eventFunctionArguments) => { const [data, context, callback] = eventFunctionArguments; const client = core.getClient(); const functionName = utils.getFunctionName(); const name = client && core.hasSpanStreamingEnabled(client) ? functionName || core.SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK : context.eventType; return node.startSpanManual( { name, attributes: { [attributes.SENTRY_OP]: op.FUNCTION_GCP, [attributes.FAAS_NAME]: functionName, [attributes.FAAS_TRIGGER]: "event", [attributes.SENTRY_SEGMENT_NAME_SOURCE]: "component", [attributes.SENTRY_ORIGIN]: "auto.function.serverless.gcp_event", [attributes.GCP_FUNCTION_CONTEXT_EVENT_TYPE]: context.eventType, [attributes.GCP_FUNCTION_CONTEXT_EVENT_ID]: context.eventId, [attributes.GCP_FUNCTION_CONTEXT_RESOURCE]: context.resource, [attributes.GCP_FUNCTION_CONTEXT_TIMESTAMP]: context.timestamp } }, (span) => { const scope = node.getCurrentScope(); scope.setContext("gcp.function.context", { ...context }); const newCallback = utils.domainify((...args) => { if (args[0] !== null && args[0] !== void 0) { node.captureException(args[0], (scope2) => utils.markEventUnhandled(scope2, "auto.function.serverless.gcp_event")); } span.end(); node.flush(options.flushTimeout).then(null, (e) => { debugBuild.DEBUG_BUILD && core.debug.error(e); }).then(() => { if (typeof callback === "function") { callback(...args); } }); }); if (fn.length > 2) { return core.handleCallbackErrors( () => fn(data, context, newCallback), (err) => { node.captureException(err, (scope2) => utils.markEventUnhandled(scope2, "auto.function.serverless.gcp_event")); } ); } return Promise.resolve().then(() => fn(data, context)).then( (result) => newCallback(null, result), (err) => newCallback(err, void 0) ); } ); }; } exports.wrapEventFunction = wrapEventFunction; //# sourceMappingURL=events.js.map