UNPKG

@sentry/browser

Version:
126 lines (125 loc) • 5.24 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { WINDOW } from "../helpers.js"; import { debug, defineIntegration, getCurrentScope, metrics } from "@sentry/core"; import { resolveCurrentRoute } from "@sentry/browser-utils"; import { BROWSER_BFCACHE_FRAME, BROWSER_BFCACHE_NOT_RESTORED_REASON_COUNT, BROWSER_BFCACHE_OUTCOME, BROWSER_BFCACHE_REASON, SENTRY_ORIGIN, SENTRY_SEGMENT_NAME } from "@sentry/conventions/attributes"; import { BROWSER_BFCACHE_NAVIGATION, BROWSER_BFCACHE_NOT_RESTORED, BROWSER_BFCACHE_RELOAD_DURATION } from "@sentry/conventions/metrics"; // src/integrations/bfcacheMetrics.ts const INTEGRATION_NAME = "BfcacheMetrics"; /** * Captures bfcache hit/miss counters and Chromium notRestoredReasons when available. */ const bfcacheMetricsIntegration = defineIntegration((options = {}) => { const maxReasons = _resolveMaxReasons(options.maxReasons); return { name: INTEGRATION_NAME, setupOnce() { if (!WINDOW.addEventListener || !WINDOW.performance?.getEntriesByType) { DEBUG_BUILD && 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 = 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) metrics.distribution(BROWSER_BFCACHE_RELOAD_DURATION, navigationEntry.duration, { unit: "millisecond", attributes: _withOriginAttr({ [SENTRY_SEGMENT_NAME]: routeName }) }); reasons.forEach((r) => _captureBFCacheReason(r, routeName)); } WINDOW.addEventListener("pageshow", onPageShow, true); } }; }); /** * Captures a bf navigation as a metric and records the outcome and reason count. */ function _captureBFCacheNavigation(outcome, reasonCount, routeName) { metrics.count(BROWSER_BFCACHE_NAVIGATION, 1, { attributes: _withOriginAttr({ [BROWSER_BFCACHE_OUTCOME]: outcome, [BROWSER_BFCACHE_NOT_RESTORED_REASON_COUNT]: reasonCount || void 0, [SENTRY_SEGMENT_NAME]: routeName }) }); } /** * Maps a collected reason to a metric and captures/sends it. */ function _captureBFCacheReason({ reason, frame }, routeName) { metrics.count(BROWSER_BFCACHE_NOT_RESTORED, 1, { attributes: _withOriginAttr({ [BROWSER_BFCACHE_REASON]: reason, [BROWSER_BFCACHE_FRAME]: frame, [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 resolveCurrentRoute() || getCurrentScope().getScopeData().transactionName || 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) { DEBUG_BUILD && 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_ORIGIN] = "auto.browser.bfcache"; return attributes; } export { _collectNotRestoredReasons, _getSegmentName, _resolveMaxReasons, bfcacheMetricsIntegration }; //# sourceMappingURL=bfcacheMetrics.js.map