@sentry/browser
Version:
Official Sentry SDK for browsers
138 lines (134 loc) • 5.39 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const browser = require('@sentry/core/browser');
const debugBuild = require('../debug-build.js');
const helpers = require('../helpers.js');
require('@sentry-internal/browser-utils');
require('../stack-parsers.js');
require('../integrations/breadcrumbs.js');
require('../integrations/browserapierrors.js');
require('../integrations/browsersession.js');
require('../integrations/culturecontext.js');
require('../integrations/globalhandlers.js');
require('../integrations/httpcontext.js');
require('../integrations/linkederrors.js');
require('../integrations/spotlight.js');
const PREVIOUS_TRACE_MAX_DURATION = 3600;
const PREVIOUS_TRACE_KEY = "sentry_previous_trace";
const PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE = "sentry.previous_trace";
function linkTraces(client, {
linkPreviousTrace,
consistentTraceSampling
}) {
const useSessionStorage = linkPreviousTrace === "session-storage";
let inMemoryPreviousTraceInfo = useSessionStorage ? getPreviousTraceFromSessionStorage() : void 0;
client.on("spanStart", (span) => {
if (browser.getRootSpan(span) !== span) {
return;
}
const oldPropagationContext = browser.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 = browser.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,
[browser.SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]: inMemoryPreviousTraceInfo.sampleRate
};
});
}
}
function addPreviousTraceSpanLink(previousTraceInfo, span, oldPropagationContext) {
const spanJson = browser.spanToJSON(span);
function getSampleRate() {
try {
return Number(oldPropagationContext.dsc?.sample_rate) ?? Number(spanJson.data?.[browser.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]);
} 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 <= PREVIOUS_TRACE_MAX_DURATION) {
if (debugBuild.DEBUG_BUILD) {
browser.debug.log(
`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({
op: spanJson.op,
...span.spanContext()
})}\``
);
}
span.addLink({
context: previousTraceSpanCtx,
attributes: {
[browser.SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE]: "previous_trace"
}
});
span.setAttribute(
PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE,
`${previousTraceSpanCtx.traceId}-${previousTraceSpanCtx.spanId}-${spanContextSampled(previousTraceSpanCtx) ? 1 : 0}`
);
}
return updatedPreviousTraceInfo;
}
function storePreviousTraceInSessionStorage(previousTraceInfo) {
try {
helpers.WINDOW.sessionStorage.setItem(PREVIOUS_TRACE_KEY, JSON.stringify(previousTraceInfo));
} catch (e) {
debugBuild.DEBUG_BUILD && browser.debug.warn("Could not store previous trace in sessionStorage", e);
}
}
function getPreviousTraceFromSessionStorage() {
try {
const previousTraceInfo = helpers.WINDOW.sessionStorage?.getItem(PREVIOUS_TRACE_KEY);
return JSON.parse(previousTraceInfo);
} catch {
return void 0;
}
}
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