UNPKG

@sentry/google-cloud-serverless

Version:
71 lines (68 loc) • 3.03 kB
import { GCP_FUNCTION_CONTEXT_TIME, GCP_FUNCTION_CONTEXT_SPECVERSION, GCP_FUNCTION_CONTEXT_SOURCE, GCP_FUNCTION_CONTEXT_ID, GCP_FUNCTION_CONTEXT_TYPE, SENTRY_ORIGIN, SENTRY_SEGMENT_NAME_SOURCE, FAAS_TRIGGER, FAAS_NAME, SENTRY_OP } from '@sentry/conventions/attributes'; import { FUNCTION_GCP } from '@sentry/conventions/op'; import { getClient, hasSpanStreamingEnabled, SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK, handleCallbackErrors, debug } from '@sentry/core'; import { startSpanManual, getCurrentScope, captureException, flush } from '@sentry/node'; import { DEBUG_BUILD } from '../debug-build.js'; import { proxyFunction, domainify, getFunctionName, markEventUnhandled } from '../utils.js'; function wrapCloudEventFunction(fn, wrapOptions = {}) { return proxyFunction(fn, (f) => domainify(_wrapCloudEventFunction(f, wrapOptions))); } function _wrapCloudEventFunction(fn, wrapOptions = {}) { const options = { flushTimeout: 2e3, ...wrapOptions }; return (context, callback) => { const client = getClient(); const functionName = getFunctionName(); const name = client && hasSpanStreamingEnabled(client) ? functionName || SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK : context.type || "<unknown>"; return startSpanManual( { name, attributes: { [SENTRY_OP]: FUNCTION_GCP, [FAAS_NAME]: functionName, [FAAS_TRIGGER]: "cloud_event", [SENTRY_SEGMENT_NAME_SOURCE]: "component", [SENTRY_ORIGIN]: "auto.function.serverless.gcp_cloud_event", [GCP_FUNCTION_CONTEXT_TYPE]: context.type, [GCP_FUNCTION_CONTEXT_ID]: context.id, [GCP_FUNCTION_CONTEXT_SOURCE]: context.source, [GCP_FUNCTION_CONTEXT_SPECVERSION]: context.specversion, [GCP_FUNCTION_CONTEXT_TIME]: context.time } }, (span) => { const scope = getCurrentScope(); scope.setContext("gcp.function.context", { ...context }); const newCallback = domainify((...args) => { if (args[0] !== null && args[0] !== void 0) { captureException(args[0], (scope2) => markEventUnhandled(scope2, "auto.function.serverless.gcp_cloud_event")); } span.end(); flush(options.flushTimeout).then(null, (e) => { DEBUG_BUILD && debug.error(e); }).then(() => { if (typeof callback === "function") { callback(...args); } }); }); if (fn.length > 1) { return handleCallbackErrors( () => fn(context, newCallback), (err) => { captureException(err, (scope2) => markEventUnhandled(scope2, "auto.function.serverless.gcp_cloud_event")); } ); } return Promise.resolve().then(() => fn(context)).then( (result) => newCallback(null, result), (err) => newCallback(err, void 0) ); } ); }; } export { wrapCloudEventFunction }; //# sourceMappingURL=cloud_events.js.map