UNPKG

@sentry/google-cloud-serverless

Version:
71 lines (68 loc) • 3 kB
import { GCP_FUNCTION_CONTEXT_TIMESTAMP, GCP_FUNCTION_CONTEXT_RESOURCE, GCP_FUNCTION_CONTEXT_EVENT_ID, GCP_FUNCTION_CONTEXT_EVENT_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 wrapEventFunction(fn, wrapOptions = {}) { return proxyFunction(fn, (f) => domainify(_wrapEventFunction(f, wrapOptions))); } function _wrapEventFunction(fn, wrapOptions = {}) { const options = { flushTimeout: 2e3, ...wrapOptions }; return (...eventFunctionArguments) => { const [data, context, callback] = eventFunctionArguments; const client = getClient(); const functionName = getFunctionName(); const name = client && hasSpanStreamingEnabled(client) ? functionName || SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK : context.eventType; return startSpanManual( { name, attributes: { [SENTRY_OP]: FUNCTION_GCP, [FAAS_NAME]: functionName, [FAAS_TRIGGER]: "event", [SENTRY_SEGMENT_NAME_SOURCE]: "component", [SENTRY_ORIGIN]: "auto.function.serverless.gcp_event", [GCP_FUNCTION_CONTEXT_EVENT_TYPE]: context.eventType, [GCP_FUNCTION_CONTEXT_EVENT_ID]: context.eventId, [GCP_FUNCTION_CONTEXT_RESOURCE]: context.resource, [GCP_FUNCTION_CONTEXT_TIMESTAMP]: context.timestamp } }, (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_event")); } span.end(); flush(options.flushTimeout).then(null, (e) => { DEBUG_BUILD && debug.error(e); }).then(() => { if (typeof callback === "function") { callback(...args); } }); }); if (fn.length > 2) { return handleCallbackErrors( () => fn(data, context, newCallback), (err) => { captureException(err, (scope2) => markEventUnhandled(scope2, "auto.function.serverless.gcp_event")); } ); } return Promise.resolve().then(() => fn(data, context)).then( (result) => newCallback(null, result), (err) => newCallback(err, void 0) ); } ); }; } export { wrapEventFunction }; //# sourceMappingURL=events.js.map