UNPKG

@sentry/browser

Version:
129 lines (128 loc) • 5.74 kB
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"); let _sentry_conventions_attributes = require("@sentry/conventions/attributes"); let _sentry_conventions_metrics = require("@sentry/conventions/metrics"); // src/integrations/bfcacheMetrics.ts const INTEGRATION_NAME = "BfcacheMetrics"; /** * Captures bfcache hit/miss counters and Chromium notRestoredReasons when available. */ const bfcacheMetricsIntegration = (0, _sentry_core.defineIntegration)((options = {}) => { const maxReasons = _resolveMaxReasons(options.maxReasons); return { name: INTEGRATION_NAME, setupOnce() { if (!require_helpers.WINDOW.addEventListener || !require_helpers.WINDOW.performance?.getEntriesByType) { require_debug_build.DEBUG_BUILD && _sentry_core.debug.log(`[${INTEGRATION_NAME}] Browser APIs unavailable, skipping instrumentation.`); return; } function onPageShow(event) { const routeName = _getSegmentName(); if (event.persisted) { _captureBFCacheNavigation("hit", 0, routeName); return; } const navigationEntry = require_helpers.WINDOW.performance.getEntriesByType("navigation")[0]; if (navigationEntry?.type !== "back_forward") return; const reasons = _collectNotRestoredReasons(navigationEntry.notRestoredReasons, maxReasons); _captureBFCacheNavigation("miss", reasons.length, routeName); if (typeof navigationEntry.duration === "number" && navigationEntry.duration > 0) _sentry_core.metrics.distribution(_sentry_conventions_metrics.BROWSER_BFCACHE_RELOAD_DURATION, navigationEntry.duration, { unit: "millisecond", attributes: _withOriginAttr({ [_sentry_conventions_attributes.SENTRY_SEGMENT_NAME]: routeName }) }); reasons.forEach((r) => _captureBFCacheReason(r, routeName)); } require_helpers.WINDOW.addEventListener("pageshow", onPageShow, true); } }; }); /** * Captures a bf navigation as a metric and records the outcome and reason count. */ function _captureBFCacheNavigation(outcome, reasonCount, routeName) { _sentry_core.metrics.count(_sentry_conventions_metrics.BROWSER_BFCACHE_NAVIGATION, 1, { attributes: _withOriginAttr({ [_sentry_conventions_attributes.BROWSER_BFCACHE_OUTCOME]: outcome, [_sentry_conventions_attributes.BROWSER_BFCACHE_NOT_RESTORED_REASON_COUNT]: reasonCount || void 0, [_sentry_conventions_attributes.SENTRY_SEGMENT_NAME]: routeName }) }); } /** * Maps a collected reason to a metric and captures/sends it. */ function _captureBFCacheReason({ reason, frame }, routeName) { _sentry_core.metrics.count(_sentry_conventions_metrics.BROWSER_BFCACHE_NOT_RESTORED, 1, { attributes: _withOriginAttr({ [_sentry_conventions_attributes.BROWSER_BFCACHE_REASON]: reason, [_sentry_conventions_attributes.BROWSER_BFCACHE_FRAME]: frame, [_sentry_conventions_attributes.SENTRY_SEGMENT_NAME]: routeName }) }); } /** * The segment name for a bfcache navigation. * * A registered route provider is preferred because it is parameterized, which matters more here than * elsewhere: this ends up as a metric dimension, where a raw URL is unbounded cardinality. * * Without one we fall back to the scope. A hit restore is silent to tracing (no pageload span), but the * frozen scope still holds the last transaction name a downstream SDK (Vue/React/etc.) set before the * freeze, so we reuse that. On a miss the page reloads with a fresh scope, so this is the new pageload * name. Falls back to the raw pathname when unset. * * Exported for tests only. */ function _getSegmentName() { return (0, _sentry_browser_utils.resolveCurrentRoute)() || (0, _sentry_core.getCurrentScope)().getScopeData().transactionName || require_helpers.WINDOW.location?.pathname; } /** * Resolves the configured `maxReasons` cap. Reports every reason by default and clamps values below 1 to 1, * since a cap under 1 would silently drop all reasons. * * Exported for tests only. */ function _resolveMaxReasons(maxReasons) { if (maxReasons == null) return 1e400; if (maxReasons < 1) { require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn(`[${INTEGRATION_NAME}] \`maxReasons\` must be at least 1, got ${maxReasons}. Using 1 instead.`); return 1; } return maxReasons; } /** * Flattens the (possibly nested) bfcache `notRestoredReasons` tree into a capped list of reasons. * * Exported for tests only. */ function _collectNotRestoredReasons(notRestoredReasons, maxReasons) { const reasons = []; if (!notRestoredReasons || maxReasons <= 0) return reasons; _collectReasonsFromFrame(notRestoredReasons, "top", reasons, maxReasons); return reasons; } function _collectReasonsFromFrame(frame, frameType, collectedReasons, maxReasons) { if (collectedReasons.length >= maxReasons) return; frame.reasons?.forEach((reason) => { if (collectedReasons.length >= maxReasons) return; const reasonValue = typeof reason === "string" ? reason : reason.reason; if (!reasonValue) return; collectedReasons.push({ reason: reasonValue, frame: frameType }); }); frame.children?.forEach((child) => { _collectReasonsFromFrame(child, "child", collectedReasons, maxReasons); }); } /** * Adds the origin attributes to a set of attributes, it mutates the original attributes. */ function _withOriginAttr(attributes) { attributes[_sentry_conventions_attributes.SENTRY_ORIGIN] = "auto.browser.bfcache"; return attributes; } exports._collectNotRestoredReasons = _collectNotRestoredReasons; exports._getSegmentName = _getSegmentName; exports._resolveMaxReasons = _resolveMaxReasons; exports.bfcacheMetricsIntegration = bfcacheMetricsIntegration; //# sourceMappingURL=bfcacheMetrics.js.map