@sentry/google-cloud-serverless
Version:
Official Sentry SDK for Google Cloud Functions
59 lines (56 loc) • 2.3 kB
JavaScript
import { URL_FULL, URL_DOMAIN, SERVER_ADDRESS, HTTP_REQUEST_METHOD, SENTRY_ORIGIN, SENTRY_OP } from '@sentry/conventions/attributes';
import { HTTP_CLIENT } from '@sentry/conventions/op';
import { defineIntegration, fill, getClient, filterCollectedUrl, hasSpanStreamingEnabled, stripUrlQueryAndFragment, SentryNonRecordingSpan, parseStringToURLObject, isURLObjectRelative } from '@sentry/core';
import { startInactiveSpan } from '@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");
fill(commonModule.Service.prototype, "request", wrapRequestFunction);
} catch (e) {
if (!optional) {
throw e;
}
}
},
setup(client) {
SETUP_CLIENTS.set(client, true);
}
};
});
const googleCloudHttpIntegration = defineIntegration(_googleCloudHttpIntegration);
function wrapRequestFunction(orig) {
return function(reqOpts, callback) {
const httpMethod = reqOpts.method || "GET";
const client = getClient();
const serverAddress = getServerAddress(this.apiEndpoint);
const streamedName = serverAddress ? `${httpMethod} ${serverAddress}` : httpMethod;
const span = SETUP_CLIENTS.has(client) ? startInactiveSpan({
name: !!client && hasSpanStreamingEnabled(client) ? streamedName : `${httpMethod} ${stripUrlQueryAndFragment(reqOpts.uri)}`,
onlyIfParent: true,
attributes: {
[SENTRY_OP]: HTTP_CLIENT,
[SENTRY_ORIGIN]: "auto.http.serverless",
[HTTP_REQUEST_METHOD]: httpMethod,
[SERVER_ADDRESS]: serverAddress,
[URL_DOMAIN]: serverAddress,
[URL_FULL]: filterCollectedUrl(reqOpts.uri)
}
}) : new SentryNonRecordingSpan();
orig.call(this, reqOpts, (...args) => {
span.end();
callback(...args);
});
};
}
function getServerAddress(apiEndpoint) {
const url = parseStringToURLObject(apiEndpoint);
return url && !isURLObjectRelative(url) ? url.host : apiEndpoint;
}
export { googleCloudHttpIntegration };
//# sourceMappingURL=google-cloud-http.js.map