@sentry/browser
Version:
Official Sentry SDK for browsers
125 lines (124 loc) • 5.54 kB
JavaScript
const require_helpers = require("../helpers.js");
const require_debug_build = require("../debug-build.js");
require("../exports.js");
let _sentry_core = require("@sentry/core");
let _sentry_conventions_attributes = require("@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 ((0, _sentry_core.getRootSpan)(span) !== span) return;
const oldPropagationContext = (0, _sentry_core.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 = (0, _sentry_core.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,
[_sentry_core.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 = (0, _sentry_core.spanToJSON)(span);
function getSampleRate() {
try {
const oldSampleRate = Number(spanJson.attributes[_sentry_core.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 (require_debug_build.DEBUG_BUILD) _sentry_core.debug.log(`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({
op: spanJson.attributes[_sentry_conventions_attributes.SENTRY_OP],
...span.spanContext()
})}\``);
span.addLink({
context: previousTraceSpanCtx,
attributes: { [_sentry_conventions_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 {
require_helpers.WINDOW.sessionStorage.setItem(PREVIOUS_TRACE_KEY, JSON.stringify(previousTraceInfo));
} catch (e) {
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn("Could not store previous trace in sessionStorage", e);
}
}
/**
* Retrieves the previous trace from sessionStorage if available.
*/
function getPreviousTraceFromSessionStorage() {
try {
const previousTraceInfo = require_helpers.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;
}
exports.PREVIOUS_TRACE_KEY = PREVIOUS_TRACE_KEY;
exports.PREVIOUS_TRACE_MAX_DURATION = PREVIOUS_TRACE_MAX_DURATION;
exports.PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE = PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE;
exports.addPreviousTraceSpanLink = addPreviousTraceSpanLink;
exports.getPreviousTraceFromSessionStorage = getPreviousTraceFromSessionStorage;
exports.linkTraces = linkTraces;
exports.spanContextSampled = spanContextSampled;
exports.storePreviousTraceInSessionStorage = storePreviousTraceInSessionStorage;
//# sourceMappingURL=linkedTraces.js.map