UNPKG

@sentry/google-cloud-serverless

Version:
73 lines (69 loc) • 3.09 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 wrapHttpFunction(fn, wrapOptions = {}) { const wrap = (f) => utils.domainify(_wrapHttpFunction(f, wrapOptions)); let overrides; const emulatorFunc = fn.__emulator_func; if (emulatorFunc) { overrides = { __emulator_func: utils.proxyFunction(emulatorFunc, wrap) }; } return utils.proxyFunction(fn, wrap, overrides); } function _wrapHttpFunction(fn, options) { const flushTimeout = options.flushTimeout || 2e3; return (req, res) => { const reqMethod = (req.method || "").toUpperCase(); const reqUrl = core.stripUrlQueryAndFragment(req.originalUrl || req.url || ""); const sentryTrace = req.headers && core.isString(req.headers["sentry-trace"]) ? req.headers["sentry-trace"] : void 0; const baggage = req.headers?.baggage; return node.continueTrace({ sentryTrace, baggage }, () => { const normalizedRequest = core.httpRequestToRequestData(req); node.getCurrentScope().setSDKProcessingMetadata({ normalizedRequest }); const functionName = utils.getFunctionName(); const client = core.getClient(); const hasSpanStreaming = client && core.hasSpanStreamingEnabled(client); const name = hasSpanStreaming ? functionName || core.SERVERLESS_FUNCTION_SPAN_NAME_FALLBACK : `${reqMethod} ${reqUrl}`; return node.startSpanManual( { name, attributes: { [attributes.SENTRY_OP]: op.FUNCTION_GCP, [attributes.FAAS_NAME]: functionName, [attributes.FAAS_TRIGGER]: "http", [attributes.SENTRY_SEGMENT_NAME_SOURCE]: hasSpanStreaming ? "component" : "route", [attributes.SENTRY_ORIGIN]: "auto.function.serverless.gcp_http", // The method and path used to be the span name; they stay on the span so that // information survives the low-cardinality rename. [attributes.HTTP_REQUEST_METHOD]: reqMethod || void 0, [attributes.URL_PATH]: reqUrl || void 0 } }, (span) => { const _end = res.end; res.end = function(chunk, encoding, cb) { core.setHttpStatus(span, res.statusCode); span.end(); node.flush(flushTimeout).then(null, (e) => { debugBuild.DEBUG_BUILD && core.debug.error(e); }).then(() => { _end.call(this, chunk, encoding, cb); }); }; return core.handleCallbackErrors( () => fn(req, res), (err) => { node.captureException(err, (scope) => utils.markEventUnhandled(scope, "auto.function.serverless.gcp_http")); } ); } ); }); }; } exports.wrapHttpFunction = wrapHttpFunction; //# sourceMappingURL=http.js.map