UNPKG

@sentry/google-cloud-serverless

Version:
61 lines (57 loc) • 2.26 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 INTEGRATION_NAME = "GoogleCloudHttp"; const SETUP_CLIENTS = /* @__PURE__ */ new WeakMap(); const _googleCloudHttpIntegration = ((options = {}) => { const optional = options.optional || false; return { name: INTEGRATION_NAME, setupOnce() { try { const commonModule = require("@google-cloud/common"); core.fill(commonModule.Service.prototype, "request", wrapRequestFunction); } catch (e) { if (!optional) { throw e; } } }, setup(client) { SETUP_CLIENTS.set(client, true); } }; }); const googleCloudHttpIntegration = core.defineIntegration(_googleCloudHttpIntegration); function wrapRequestFunction(orig) { return function(reqOpts, callback) { const httpMethod = reqOpts.method || "GET"; const client = core.getClient(); const serverAddress = getServerAddress(this.apiEndpoint); const streamedName = serverAddress ? `${httpMethod} ${serverAddress}` : httpMethod; const span = SETUP_CLIENTS.has(client) ? node.startInactiveSpan({ name: !!client && core.hasSpanStreamingEnabled(client) ? streamedName : `${httpMethod} ${core.stripUrlQueryAndFragment(reqOpts.uri)}`, onlyIfParent: true, attributes: { [attributes.SENTRY_OP]: op.HTTP_CLIENT, [attributes.SENTRY_ORIGIN]: "auto.http.serverless", [attributes.HTTP_REQUEST_METHOD]: httpMethod, [attributes.SERVER_ADDRESS]: serverAddress, [attributes.URL_DOMAIN]: serverAddress, [attributes.URL_FULL]: core.filterCollectedUrl(reqOpts.uri) } }) : new core.SentryNonRecordingSpan(); orig.call(this, reqOpts, (...args) => { span.end(); callback(...args); }); }; } function getServerAddress(apiEndpoint) { const url = core.parseStringToURLObject(apiEndpoint); return url && !core.isURLObjectRelative(url) ? url.host : apiEndpoint; } exports.googleCloudHttpIntegration = googleCloudHttpIntegration; //# sourceMappingURL=google-cloud-http.js.map