123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const core = require('@sentry/core');
- const utils = require('@sentry/utils');
- const debugBuild = require('../debug-build.js');
- const helpers = require('../helpers.js');
- const MAX_ALLOWED_STRING_LENGTH = 1024;
- const INTEGRATION_NAME = 'Breadcrumbs';
- const _breadcrumbsIntegration = ((options = {}) => {
- const _options = {
- console: true,
- dom: true,
- fetch: true,
- history: true,
- sentry: true,
- xhr: true,
- ...options,
- };
- return {
- name: INTEGRATION_NAME,
-
- setupOnce() {},
- setup(client) {
- if (_options.console) {
- utils.addConsoleInstrumentationHandler(_getConsoleBreadcrumbHandler(client));
- }
- if (_options.dom) {
- utils.addClickKeypressInstrumentationHandler(_getDomBreadcrumbHandler(client, _options.dom));
- }
- if (_options.xhr) {
- utils.addXhrInstrumentationHandler(_getXhrBreadcrumbHandler(client));
- }
- if (_options.fetch) {
- utils.addFetchInstrumentationHandler(_getFetchBreadcrumbHandler(client));
- }
- if (_options.history) {
- utils.addHistoryInstrumentationHandler(_getHistoryBreadcrumbHandler(client));
- }
- if (_options.sentry && client.on) {
- client.on('beforeSendEvent', _getSentryBreadcrumbHandler(client));
- }
- },
- };
- }) ;
- const breadcrumbsIntegration = core.defineIntegration(_breadcrumbsIntegration);
- const Breadcrumbs = core.convertIntegrationFnToClass(INTEGRATION_NAME, breadcrumbsIntegration)
- ;
- function _getSentryBreadcrumbHandler(client) {
- return function addSentryBreadcrumb(event) {
- if (core.getClient() !== client) {
- return;
- }
- core.addBreadcrumb(
- {
- category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
- event_id: event.event_id,
- level: event.level,
- message: utils.getEventDescription(event),
- },
- {
- event,
- },
- );
- };
- }
- function _getDomBreadcrumbHandler(
- client,
- dom,
- ) {
- return function _innerDomBreadcrumb(handlerData) {
- if (core.getClient() !== client) {
- return;
- }
- let target;
- let componentName;
- let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;
- let maxStringLength =
- typeof dom === 'object' && typeof dom.maxStringLength === 'number' ? dom.maxStringLength : undefined;
- if (maxStringLength && maxStringLength > MAX_ALLOWED_STRING_LENGTH) {
- debugBuild.DEBUG_BUILD &&
- utils.logger.warn(
- `\`dom.maxStringLength\` cannot exceed ${MAX_ALLOWED_STRING_LENGTH}, but a value of ${maxStringLength} was configured. Sentry will use ${MAX_ALLOWED_STRING_LENGTH} instead.`,
- );
- maxStringLength = MAX_ALLOWED_STRING_LENGTH;
- }
- if (typeof keyAttrs === 'string') {
- keyAttrs = [keyAttrs];
- }
-
- try {
- const event = handlerData.event ;
- const element = _isEvent(event) ? event.target : event;
- target = utils.htmlTreeAsString(element, { keyAttrs, maxStringLength });
- componentName = utils.getComponentName(element);
- } catch (e) {
- target = '<unknown>';
- }
- if (target.length === 0) {
- return;
- }
- const breadcrumb = {
- category: `ui.${handlerData.name}`,
- message: target,
- };
- if (componentName) {
- breadcrumb.data = { 'ui.component_name': componentName };
- }
- core.addBreadcrumb(breadcrumb, {
- event: handlerData.event,
- name: handlerData.name,
- global: handlerData.global,
- });
- };
- }
- function _getConsoleBreadcrumbHandler(client) {
- return function _consoleBreadcrumb(handlerData) {
- if (core.getClient() !== client) {
- return;
- }
- const breadcrumb = {
- category: 'console',
- data: {
- arguments: handlerData.args,
- logger: 'console',
- },
- level: utils.severityLevelFromString(handlerData.level),
- message: utils.safeJoin(handlerData.args, ' '),
- };
- if (handlerData.level === 'assert') {
- if (handlerData.args[0] === false) {
- breadcrumb.message = `Assertion failed: ${utils.safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`;
- breadcrumb.data.arguments = handlerData.args.slice(1);
- } else {
-
- return;
- }
- }
- core.addBreadcrumb(breadcrumb, {
- input: handlerData.args,
- level: handlerData.level,
- });
- };
- }
- function _getXhrBreadcrumbHandler(client) {
- return function _xhrBreadcrumb(handlerData) {
- if (core.getClient() !== client) {
- return;
- }
- const { startTimestamp, endTimestamp } = handlerData;
- const sentryXhrData = handlerData.xhr[utils.SENTRY_XHR_DATA_KEY];
-
- if (!startTimestamp || !endTimestamp || !sentryXhrData) {
- return;
- }
- const { method, url, status_code, body } = sentryXhrData;
- const data = {
- method,
- url,
- status_code,
- };
- const hint = {
- xhr: handlerData.xhr,
- input: body,
- startTimestamp,
- endTimestamp,
- };
- core.addBreadcrumb(
- {
- category: 'xhr',
- data,
- type: 'http',
- },
- hint,
- );
- };
- }
- function _getFetchBreadcrumbHandler(client) {
- return function _fetchBreadcrumb(handlerData) {
- if (core.getClient() !== client) {
- return;
- }
- const { startTimestamp, endTimestamp } = handlerData;
-
- if (!endTimestamp) {
- return;
- }
- if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') {
-
- return;
- }
- if (handlerData.error) {
- const data = handlerData.fetchData;
- const hint = {
- data: handlerData.error,
- input: handlerData.args,
- startTimestamp,
- endTimestamp,
- };
- core.addBreadcrumb(
- {
- category: 'fetch',
- data,
- level: 'error',
- type: 'http',
- },
- hint,
- );
- } else {
- const response = handlerData.response ;
- const data = {
- ...handlerData.fetchData,
- status_code: response && response.status,
- };
- const hint = {
- input: handlerData.args,
- response,
- startTimestamp,
- endTimestamp,
- };
- core.addBreadcrumb(
- {
- category: 'fetch',
- data,
- type: 'http',
- },
- hint,
- );
- }
- };
- }
- function _getHistoryBreadcrumbHandler(client) {
- return function _historyBreadcrumb(handlerData) {
- if (core.getClient() !== client) {
- return;
- }
- let from = handlerData.from;
- let to = handlerData.to;
- const parsedLoc = utils.parseUrl(helpers.WINDOW.location.href);
- let parsedFrom = from ? utils.parseUrl(from) : undefined;
- const parsedTo = utils.parseUrl(to);
-
- if (!parsedFrom || !parsedFrom.path) {
- parsedFrom = parsedLoc;
- }
-
-
- if (parsedLoc.protocol === parsedTo.protocol && parsedLoc.host === parsedTo.host) {
- to = parsedTo.relative;
- }
- if (parsedLoc.protocol === parsedFrom.protocol && parsedLoc.host === parsedFrom.host) {
- from = parsedFrom.relative;
- }
- core.addBreadcrumb({
- category: 'navigation',
- data: {
- from,
- to,
- },
- });
- };
- }
- function _isEvent(event) {
- return !!event && !!(event ).target;
- }
- exports.Breadcrumbs = Breadcrumbs;
- exports.breadcrumbsIntegration = breadcrumbsIntegration;
|