UNPKG

@sentry/core

Version:
122 lines (121 loc) • 5.5 kB
import { debug } from "./debug-logger.js"; import { baggageHeaderToDynamicSamplingContext } from "./baggage.js"; import { extractOrgIdFromClient } from "./dsn.js"; import { parseSampleRate } from "./parseSampleRate.js"; import { generateSpanId, generateTraceId } from "./propagationContext.js"; import { safeMathRandom } from "./randomSafeContext.js"; // src/utils/tracing.ts const TRACEPARENT_REGEXP = /* @__PURE__ */ new RegExp("^[ \\t]*([0-9a-f]{32})?-?([0-9a-f]{16})?-?([01])?[ \\t]*$"); /** * Extract transaction context data from a `sentry-trace` header. * * This is terrible naming but the function has nothing to do with the W3C traceparent header. * It can only parse the `sentry-trace` header and extract the "trace parent" data. * * @param traceparent Traceparent string * * @returns Object containing data from the header, or undefined if traceparent string is malformed */ function extractTraceparentData(traceparent) { if (!traceparent) return; const matches = traceparent.match(TRACEPARENT_REGEXP); if (!matches) return; let parentSampled; if (matches[3] === "1") parentSampled = true; else if (matches[3] === "0") parentSampled = false; return { traceId: matches[1], parentSampled, parentSpanId: matches[2] }; } /** * Whether a propagation context continues an incoming trace, rather than being the head of a new one. * * Both fields have to be checked. `parentSpanId` is unset when the incoming `sentry-trace` header * carried no span id, which the header format allows. `dsc` is unset when a remote parent arrived * without any incoming baggage. Either one on its own therefore misses a continued trace. */ function isContinuingTrace(propagationContext) { return !!propagationContext.parentSpanId || !!propagationContext.dsc; } /** * Create a propagation context from incoming headers or * creates a minimal new one if the headers are undefined. */ function propagationContextFromHeaders(sentryTrace, baggage) { const traceparentData = extractTraceparentData(sentryTrace); const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage); if (!traceparentData?.traceId) return { traceId: generateTraceId(), sampleRand: safeMathRandom() }; const sampleRand = getSampleRandFromTraceparentAndDsc(traceparentData, dynamicSamplingContext); if (dynamicSamplingContext) dynamicSamplingContext.sample_rand = sampleRand.toString(); const { traceId, parentSpanId, parentSampled } = traceparentData; return { traceId, parentSpanId, sampled: parentSampled, dsc: dynamicSamplingContext || {}, sampleRand }; } /** * Converts a W3C `traceparent` value to the `sentry-trace` format, so carriers that only hold W3C * Trace Context can be passed to `continueTrace`. */ function sentryTraceFromTraceparent(traceparent) { const [version, traceId, spanId, flags] = traceparent.split("-"); if (version !== "00" || !traceId || !spanId || !flags) return; return `${traceId}-${spanId}-${parseInt(flags, 16) % 2 === 1 ? "1" : "0"}`; } /** * Create sentry-trace header from span context values. */ function generateSentryTraceHeader(traceId = generateTraceId(), spanId = generateSpanId(), sampled) { let sampledString = ""; if (sampled !== void 0) sampledString = sampled ? "-1" : "-0"; return `${traceId}-${spanId}${sampledString}`; } /** * Creates a W3C traceparent header from the given trace and span ids. */ function generateTraceparentHeader(traceId = generateTraceId(), spanId = generateSpanId(), sampled) { return `00-${traceId}-${spanId}-${sampled ? "01" : "00"}`; } /** * Given any combination of an incoming trace, generate a sample rand based on its defined semantics. * * Read more: https://develop.sentry.dev/sdk/telemetry/traces/#propagated-random-value */ function getSampleRandFromTraceparentAndDsc(traceparentData, dsc) { const parsedSampleRand = parseSampleRate(dsc?.sample_rand); if (parsedSampleRand !== void 0) return parsedSampleRand; const parsedSampleRate = parseSampleRate(dsc?.sample_rate); if (parsedSampleRate && traceparentData?.parentSampled !== void 0) return traceparentData.parentSampled ? safeMathRandom() * parsedSampleRate : parsedSampleRate + safeMathRandom() * (1 - parsedSampleRate); else return safeMathRandom(); } /** * Determines whether a new trace should be continued based on the provided baggage org ID and the client's `strictTraceContinuation` option. * If the trace should not be continued, a new trace will be started. * * The result is dependent on the `strictTraceContinuation` option in the client. * See https://develop.sentry.dev/sdk/telemetry/traces/#stricttracecontinuation */ function shouldContinueTrace(client, baggageOrgId) { const clientOrgId = extractOrgIdFromClient(client); if (baggageOrgId && clientOrgId && baggageOrgId !== clientOrgId) { debug.log(`Won't continue trace because org IDs don't match (incoming baggage: ${baggageOrgId}, SDK options: ${clientOrgId})`); return false; } if (client.getOptions().strictTraceContinuation || false) { if (baggageOrgId && !clientOrgId || !baggageOrgId && clientOrgId) { debug.log(`Starting a new trace because strict trace continuation is enabled but one org ID is missing (incoming baggage: ${baggageOrgId}, Sentry client: ${clientOrgId})`); return false; } } return true; } export { TRACEPARENT_REGEXP, extractTraceparentData, generateSentryTraceHeader, generateTraceparentHeader, isContinuingTrace, propagationContextFromHeaders, sentryTraceFromTraceparent, shouldContinueTrace }; //# sourceMappingURL=tracing.js.map