@sentry/google-cloud-serverless
Version:
Official Sentry SDK for Google Cloud Functions
64 lines (56 loc) • 2 kB
JavaScript
import { defineIntegration, getClient, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SentryNonRecordingSpan } from '@sentry/core';
import { startInactiveSpan } from '@sentry/node';
import { fill } from '@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') ;
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 = 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(getClient() )
? startInactiveSpan({
name: `${httpMethod} ${reqOpts.uri}`,
onlyIfParent: true,
op: `http.client.${identifyService(this.apiEndpoint)}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.serverless',
},
})
: new 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)?:\/\//, '');
}
export { googleCloudHttpIntegration };
//# sourceMappingURL=google-cloud-http.js.map