@sentry/google-cloud-serverless
Version:
Official Sentry SDK for Google Cloud Functions
73 lines (69 loc) • 2.91 kB
JavaScript
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 wrapCloudEventFunction(fn, wrapOptions = {}) {
return utils.proxyFunction(fn, (f) => utils.domainify(_wrapCloudEventFunction(f, wrapOptions)));
}
function _wrapCloudEventFunction(fn, wrapOptions = {}) {
const options = {
flushTimeout: 2e3,
...wrapOptions
};
return (context, callback) => {
const client = core.getClient();
const functionName = utils.getFunctionName();
const name = client && core.hasSpanStreamingEnabled(client) ? functionName || core.SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK : context.type || "<unknown>";
return node.startSpanManual(
{
name,
attributes: {
[attributes.SENTRY_OP]: op.FUNCTION_GCP,
[attributes.FAAS_NAME]: functionName,
[attributes.FAAS_TRIGGER]: "cloud_event",
[attributes.SENTRY_SEGMENT_NAME_SOURCE]: "component",
[attributes.SENTRY_ORIGIN]: "auto.function.serverless.gcp_cloud_event",
[attributes.GCP_FUNCTION_CONTEXT_TYPE]: context.type,
[attributes.GCP_FUNCTION_CONTEXT_ID]: context.id,
[attributes.GCP_FUNCTION_CONTEXT_SOURCE]: context.source,
[attributes.GCP_FUNCTION_CONTEXT_SPECVERSION]: context.specversion,
[attributes.GCP_FUNCTION_CONTEXT_TIME]: context.time
}
},
(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_cloud_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 > 1) {
return core.handleCallbackErrors(
() => fn(context, newCallback),
(err) => {
node.captureException(err, (scope2) => utils.markEventUnhandled(scope2, "auto.function.serverless.gcp_cloud_event"));
}
);
}
return Promise.resolve().then(() => fn(context)).then(
(result) => newCallback(null, result),
(err) => newCallback(err, void 0)
);
}
);
};
}
exports.wrapCloudEventFunction = wrapCloudEventFunction;
//# sourceMappingURL=cloud_events.js.map