UNPKG

@sentry/browser

Version:
118 lines (117 loc) • 5.11 kB
import { WINDOW } from "../helpers.js"; import { DEBUG_BUILD } from "../debug-build.js"; import "../exports.js"; import { SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, debug, getCurrentScope, getRootSpan, spanToJSON } from "@sentry/core"; import { SENTRY_LINK_TYPE, SENTRY_OP } from "@sentry/conventions/attributes"; // src/tracing/linkedTraces.ts const PREVIOUS_TRACE_MAX_DURATION = 3600; const PREVIOUS_TRACE_KEY = "sentry_previous_trace"; const PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE = "sentry.previous_trace"; /** * Takes care of linking traces and applying the (consistent) sampling behavoiour based on the passed options * @param options - options for linking traces and consistent trace sampling (@see BrowserTracingOptions) * @param client - Sentry client */ function linkTraces(client, { linkPreviousTrace, consistentTraceSampling }) { const useSessionStorage = linkPreviousTrace === "session-storage"; let inMemoryPreviousTraceInfo = useSessionStorage ? getPreviousTraceFromSessionStorage() : void 0; client.on("spanStart", (span) => { if (getRootSpan(span) !== span) return; const oldPropagationContext = getCurrentScope().getPropagationContext(); inMemoryPreviousTraceInfo = addPreviousTraceSpanLink(inMemoryPreviousTraceInfo, span, oldPropagationContext); if (useSessionStorage) storePreviousTraceInSessionStorage(inMemoryPreviousTraceInfo); }); let isFirstTraceOnPageload = true; if (consistentTraceSampling) client.on("beforeSampling", (mutableSamplingContextData) => { if (!inMemoryPreviousTraceInfo) return; const scope = getCurrentScope(); const currentPropagationContext = scope.getPropagationContext(); if (isFirstTraceOnPageload && currentPropagationContext.parentSpanId) { isFirstTraceOnPageload = false; return; } scope.setPropagationContext({ ...currentPropagationContext, dsc: { ...currentPropagationContext.dsc, sample_rate: String(inMemoryPreviousTraceInfo.sampleRate), sampled: String(spanContextSampled(inMemoryPreviousTraceInfo.spanContext)) }, sampleRand: inMemoryPreviousTraceInfo.sampleRand }); mutableSamplingContextData.parentSampled = spanContextSampled(inMemoryPreviousTraceInfo.spanContext); mutableSamplingContextData.parentSampleRate = inMemoryPreviousTraceInfo.sampleRate; mutableSamplingContextData.spanAttributes = { ...mutableSamplingContextData.spanAttributes, [SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]: inMemoryPreviousTraceInfo.sampleRate }; }); } /** * Adds a previous_trace span link to the passed span if the passed * previousTraceInfo is still valid. * * @returns the updated previous trace info (based on the current span/trace) to * be used on the next call */ function addPreviousTraceSpanLink(previousTraceInfo, span, oldPropagationContext) { const spanJson = spanToJSON(span); function getSampleRate() { try { const oldSampleRate = Number(spanJson.attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE] ?? oldPropagationContext.dsc?.sample_rate); return Number.isNaN(oldSampleRate) ? 0 : oldSampleRate; } catch { return 0; } } const updatedPreviousTraceInfo = { spanContext: span.spanContext(), startTimestamp: spanJson.start_timestamp, sampleRate: getSampleRate(), sampleRand: oldPropagationContext.sampleRand }; if (!previousTraceInfo) return updatedPreviousTraceInfo; const previousTraceSpanCtx = previousTraceInfo.spanContext; if (previousTraceSpanCtx.traceId === spanJson.trace_id) return previousTraceInfo; if (Date.now() / 1e3 - previousTraceInfo.startTimestamp <= 3600) { if (DEBUG_BUILD) debug.log(`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({ op: spanJson.attributes[SENTRY_OP], ...span.spanContext() })}\``); span.addLink({ context: previousTraceSpanCtx, attributes: { [SENTRY_LINK_TYPE]: "previous_trace" } }); span.setAttribute(PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE, `${previousTraceSpanCtx.traceId}-${previousTraceSpanCtx.spanId}-${spanContextSampled(previousTraceSpanCtx) ? 1 : 0}`); } return updatedPreviousTraceInfo; } /** * Stores @param previousTraceInfo in sessionStorage. */ function storePreviousTraceInSessionStorage(previousTraceInfo) { try { WINDOW.sessionStorage.setItem(PREVIOUS_TRACE_KEY, JSON.stringify(previousTraceInfo)); } catch (e) { DEBUG_BUILD && debug.warn("Could not store previous trace in sessionStorage", e); } } /** * Retrieves the previous trace from sessionStorage if available. */ function getPreviousTraceFromSessionStorage() { try { const previousTraceInfo = WINDOW.sessionStorage?.getItem(PREVIOUS_TRACE_KEY); return JSON.parse(previousTraceInfo); } catch { return; } } /** * see {@link import('@sentry/core').spanIsSampled} */ function spanContextSampled(ctx) { return ctx.traceFlags === 1; } export { PREVIOUS_TRACE_KEY, PREVIOUS_TRACE_MAX_DURATION, PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE, addPreviousTraceSpanLink, getPreviousTraceFromSessionStorage, linkTraces, spanContextSampled, storePreviousTraceInSessionStorage }; //# sourceMappingURL=linkedTraces.js.map