-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtracing.ts
125 lines (110 loc) · 4.03 KB
/
tracing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import type { DynamicSamplingContext, PropagationContext, TraceparentData } from '../types-hoist';
import { parseSampleRate } from '../utils/parseSampleRate';
import { baggageHeaderToDynamicSamplingContext } from './baggage';
import { generateSpanId, generateTraceId } from './propagationContext';
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- RegExp is used for readability here
export const TRACEPARENT_REGEXP = new RegExp(
'^[ \\t]*' + // whitespace
'([0-9a-f]{32})?' + // trace_id
'-?([0-9a-f]{16})?' + // span_id
'-?([01])?' + // sampled
'[ \\t]*$', // whitespace
);
/**
* Extract transaction context data from a `sentry-trace` header.
*
* @param traceparent Traceparent string
*
* @returns Object containing data from the header, or undefined if traceparent string is malformed
*/
export function extractTraceparentData(traceparent?: string): TraceparentData | undefined {
if (!traceparent) {
return undefined;
}
const matches = traceparent.match(TRACEPARENT_REGEXP);
if (!matches) {
return undefined;
}
let parentSampled: boolean | undefined;
if (matches[3] === '1') {
parentSampled = true;
} else if (matches[3] === '0') {
parentSampled = false;
}
return {
traceId: matches[1],
parentSampled,
parentSpanId: matches[2],
};
}
/**
* Create a propagation context from incoming headers or
* creates a minimal new one if the headers are undefined.
*/
export function propagationContextFromHeaders(
sentryTrace: string | undefined,
baggage: string | number | boolean | string[] | null | undefined,
): PropagationContext {
const traceparentData = extractTraceparentData(sentryTrace);
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage);
if (!traceparentData?.traceId) {
return {
traceId: generateTraceId(),
sampleRand: Math.random(),
};
}
const sampleRand = getSampleRandFromTraceparentAndDsc(traceparentData, dynamicSamplingContext);
// The sample_rand on the DSC needs to be generated based on traceparent + baggage.
if (dynamicSamplingContext) {
dynamicSamplingContext.sample_rand = sampleRand.toString();
}
const { traceId, parentSpanId, parentSampled } = traceparentData;
return {
traceId,
parentSpanId,
sampled: parentSampled,
dsc: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it
sampleRand,
};
}
/**
* Create sentry-trace header from span context values.
*/
export function generateSentryTraceHeader(
traceId: string | undefined = generateTraceId(),
spanId: string | undefined = generateSpanId(),
sampled?: boolean,
): string {
let sampledString = '';
if (sampled !== undefined) {
sampledString = sampled ? '-1' : '-0';
}
return `${traceId}-${spanId}${sampledString}`;
}
/**
* 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: TraceparentData | undefined,
dsc: Partial<DynamicSamplingContext> | undefined,
): number {
// When there is an incoming sample rand use it.
const parsedSampleRand = parseSampleRate(dsc?.sample_rand);
if (parsedSampleRand !== undefined) {
return parsedSampleRand;
}
// Otherwise, if there is an incoming sampling decision + sample rate, generate a sample rand that would lead to the same sampling decision.
const parsedSampleRate = parseSampleRate(dsc?.sample_rate);
if (parsedSampleRate && traceparentData?.parentSampled !== undefined) {
return traceparentData.parentSampled
? // Returns a sample rand with positive sampling decision [0, sampleRate)
Math.random() * parsedSampleRate
: // Returns a sample rand with negative sampling decision [sampleRate, 1)
parsedSampleRate + Math.random() * (1 - parsedSampleRate);
} else {
// If nothing applies, return a random sample rand.
return Math.random();
}
}