@sentry/browser
Version:
Official Sentry SDK for browsers
198 lines (197 loc) • 7.16 kB
JavaScript
const require_debug_build = require("../debug-build.js");
const require_helpers = require("../helpers.js");
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@sentry/browser-utils");
// src/integrations/breadcrumbs.ts
/** maxStringLength gets capped to prevent 100 breadcrumbs exceeding 1MB event payload size */
const MAX_ALLOWED_STRING_LENGTH = 1024;
const INTEGRATION_NAME = "Breadcrumbs";
const _breadcrumbsIntegration = ((options = {}) => {
const _options = {
dom: true,
fetch: true,
history: true,
sentry: true,
xhr: true,
...options
};
return {
name: INTEGRATION_NAME,
setup(client) {
if (_options.dom) (0, _sentry_browser_utils.addClickKeypressInstrumentationHandler)(_getDomBreadcrumbHandler(client, _options.dom));
if (_options.xhr) (0, _sentry_browser_utils.addXhrInstrumentationHandler)(_getXhrBreadcrumbHandler(client));
if (_options.fetch) (0, _sentry_core.addFetchInstrumentationHandler)(_getFetchBreadcrumbHandler(client));
if (_options.history) (0, _sentry_browser_utils.addHistoryInstrumentationHandler)(_getHistoryBreadcrumbHandler(client));
if (_options.sentry) client.on("beforeSendEvent", _getSentryBreadcrumbHandler(client));
}
};
});
const breadcrumbsIntegration = (0, _sentry_core.defineIntegration)(_breadcrumbsIntegration);
/**
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
*/
function _getSentryBreadcrumbHandler(client) {
return function addSentryBreadcrumb(event) {
if ((0, _sentry_core.getClient)() !== client) return;
(0, _sentry_core.addBreadcrumb)({
category: `sentry.${event.type === "transaction" ? "transaction" : "event"}`,
event_id: event.event_id,
level: event.level,
message: (0, _sentry_core.getEventDescription)(event)
}, { event });
};
}
/**
* A HOC that creates a function that creates breadcrumbs from DOM API calls.
* This is a HOC so that we get access to dom options in the closure.
*/
function _getDomBreadcrumbHandler(client, dom) {
return function _innerDomBreadcrumb(handlerData) {
if ((0, _sentry_core.getClient)() !== client) return;
let target;
let componentName;
let keyAttrs = typeof dom === "object" ? dom.serializeAttribute : void 0;
let maxStringLength = typeof dom === "object" && typeof dom.maxStringLength === "number" ? dom.maxStringLength : void 0;
if (maxStringLength && maxStringLength > MAX_ALLOWED_STRING_LENGTH) {
require_debug_build.DEBUG_BUILD && _sentry_core.debug.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 = (0, _sentry_browser_utils.htmlTreeAsString)(element, {
keyAttrs,
maxStringLength
});
componentName = (0, _sentry_browser_utils.getComponentName)(element);
} catch {
target = "<unknown>";
}
if (target.length === 0) return;
const breadcrumb = {
category: `ui.${handlerData.name}`,
message: target
};
if (componentName) breadcrumb.data = { "ui.component_name": componentName };
(0, _sentry_core.addBreadcrumb)(breadcrumb, {
event: handlerData.event,
name: handlerData.name,
global: handlerData.global
});
};
}
/**
* Creates breadcrumbs from XHR API calls
*/
function _getXhrBreadcrumbHandler(client) {
return function _xhrBreadcrumb(handlerData) {
if ((0, _sentry_core.getClient)() !== client) return;
const { startTimestamp, endTimestamp } = handlerData;
const sentryXhrData = handlerData.xhr[_sentry_browser_utils.SENTRY_XHR_DATA_KEY];
if (!startTimestamp || !endTimestamp || !sentryXhrData) return;
const { method, url, status_code, body } = sentryXhrData;
const data = {
method,
url: getBreadcrumbUrl(url, client),
status_code
};
const hint = {
xhr: handlerData.xhr,
input: body,
startTimestamp,
endTimestamp
};
const breadcrumb = {
category: "xhr",
data,
type: "http",
level: (0, _sentry_core.getBreadcrumbLogLevelFromHttpStatusCode)(status_code)
};
client.emit("beforeOutgoingRequestBreadcrumb", breadcrumb, hint);
(0, _sentry_core.addBreadcrumb)(breadcrumb, hint);
};
}
/**
* Creates breadcrumbs from fetch API calls
*/
function _getFetchBreadcrumbHandler(client) {
return function _fetchBreadcrumb(handlerData) {
if ((0, _sentry_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 hint = {
data: handlerData.error,
input: handlerData.args,
startTimestamp,
endTimestamp
};
const breadcrumb = {
category: "fetch",
data: {
...handlerData.fetchData,
url: getBreadcrumbUrl(handlerData.fetchData.url, client)
},
level: "error",
type: "http"
};
client.emit("beforeOutgoingRequestBreadcrumb", breadcrumb, hint);
(0, _sentry_core.addBreadcrumb)(breadcrumb, hint);
} else {
const response = handlerData.response;
const data = {
...handlerData.fetchData,
url: getBreadcrumbUrl(handlerData.fetchData.url, client),
status_code: response?.status
};
const hint = {
input: handlerData.args,
response,
startTimestamp,
endTimestamp
};
const breadcrumb = {
category: "fetch",
data,
type: "http",
level: (0, _sentry_core.getBreadcrumbLogLevelFromHttpStatusCode)(data.status_code)
};
client.emit("beforeOutgoingRequestBreadcrumb", breadcrumb, hint);
(0, _sentry_core.addBreadcrumb)(breadcrumb, hint);
}
};
}
/**
* Creates breadcrumbs from history API calls
*/
function _getHistoryBreadcrumbHandler(client) {
return function _historyBreadcrumb(handlerData) {
if ((0, _sentry_core.getClient)() !== client) return;
let from = handlerData.from;
let to = handlerData.to;
const parsedLoc = (0, _sentry_core.parseUrl)(require_helpers.WINDOW.location.href);
let parsedFrom = from ? (0, _sentry_core.parseUrl)(from) : void 0;
const parsedTo = (0, _sentry_core.parseUrl)(to);
if (!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;
(0, _sentry_core.addBreadcrumb)({
category: "navigation",
data: {
from: (0, _sentry_core.filterCollectedUrl)(from),
to: (0, _sentry_core.filterCollectedUrl)(to)
}
});
};
}
function getBreadcrumbUrl(url, client) {
const pageUrl = url === "" ? require_helpers.WINDOW.location?.href : void 0;
return (0, _sentry_core.filterCollectedUrl)(pageUrl ? pageUrl.split("#")[0] ?? "" : url, client);
}
function _isEvent(event) {
return !!event && !!event.target;
}
exports.breadcrumbsIntegration = breadcrumbsIntegration;
//# sourceMappingURL=breadcrumbs.js.map