@sentry/google-cloud-serverless
Version:
Official Sentry SDK for Google Cloud Functions
66 lines (57 loc) • 2.03 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
const core = require('@sentry/core');
const node = require('@sentry/node');
const utils = require('@sentry/utils');
const INTEGRATION_NAME = 'GoogleCloudHttp';
const SETUP_CLIENTS = new WeakMap();
const _googleCloudHttpIntegration = ((options = {}) => {
const optional = options.optional || false;
return {
name: INTEGRATION_NAME,
setupOnce() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const commonModule = require('@google-cloud/common') ;
utils.fill(commonModule.Service.prototype, 'request', wrapRequestFunction);
} catch (e) {
if (!optional) {
throw e;
}
}
},
setup(client) {
SETUP_CLIENTS.set(client, true);
},
};
}) ;
/**
* Google Cloud Platform service requests tracking for RESTful APIs.
*/
const googleCloudHttpIntegration = core.defineIntegration(_googleCloudHttpIntegration);
/** Returns a wrapped function that makes a request with tracing enabled */
function wrapRequestFunction(orig) {
return function ( reqOpts, callback) {
const httpMethod = reqOpts.method || 'GET';
const span = SETUP_CLIENTS.has(core.getClient() )
? node.startInactiveSpan({
name: `${httpMethod} ${reqOpts.uri}`,
onlyIfParent: true,
op: `http.client.${identifyService(this.apiEndpoint)}`,
attributes: {
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.serverless',
},
})
: new core.SentryNonRecordingSpan();
orig.call(this, reqOpts, (...args) => {
span.end();
callback(...args);
});
};
}
/** Identifies service by its base url */
function identifyService(apiEndpoint) {
const match = apiEndpoint.match(/^https:\/\/(\w+)\.googleapis.com$/);
return match ? match[1] : apiEndpoint.replace(/^(http|https)?:\/\//, '');
}
exports.googleCloudHttpIntegration = googleCloudHttpIntegration;
//# sourceMappingURL=google-cloud-http.js.map