Repository navigation
Expand file tree
/
Copy pathtraceData.ts
More file actions
98 lines (86 loc) · 4.48 KB
/
Copy pathtraceData.ts
File metadata and controls
98 lines (86 loc) · 4.48 KB
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
import { getAsyncContextStrategy } from '../asyncContext';
import { getMainCarrier } from '../carrier';
import type { Client } from '../client';
import { getClient, getCurrentScope, hasExternalPropagationContext } from '../currentScopes';
import { isEnabled } from '../exports';
import type { Scope } from '../scope';
import { getDynamicSamplingContextFromScope, getDynamicSamplingContextFromSpan } from '../tracing';
import { spanIsNonRecordingSpan } from '../tracing/sentryNonRecordingSpan';
import type { Span } from '../types/span';
import type { SerializedTraceData } from '../types/tracing';
import { dynamicSamplingContextToSentryBaggageHeader } from './baggage';
import { debug } from './debug-logger';
import { hasSpansEnabled } from './hasSpansEnabled';
import { getActiveSpan, spanToTraceHeader, spanToTraceparentHeader } from './spanUtils';
import { generateSentryTraceHeader, generateTraceparentHeader, TRACEPARENT_REGEXP } from './tracing';
/**
* Extracts trace propagation data from the current span or from the client's scope (via transaction or propagation
* context) and serializes it to `sentry-trace` and `baggage` values. These values can be used to propagate
* a trace via our tracing Http headers or Html `<meta>` tags.
*
* This function also applies some validation to the generated sentry-trace and baggage values to ensure that
* only valid strings are returned.
*
* When an external propagation context is registered (e.g. via the OTLP integration) and there is no active
* Sentry span, this function returns an empty object to defer outgoing request propagation to the external
* propagator (e.g. an OpenTelemetry propagator).
*
* If (@param options.propagateTraceparent) is `true`, the function will also generate a `traceparent` value,
* following the W3C traceparent header format.
*
* @returns an object with the tracing data values. The object keys are the name of the tracing key to be used as header
* or meta tag name.
*/
export function getTraceData(
options: { span?: Span; scope?: Scope; client?: Client; propagateTraceparent?: boolean } = {},
): SerializedTraceData {
const client = options.client || getClient();
if (!isEnabled() || !client) {
return {};
}
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
if (acs.getTraceData) {
return acs.getTraceData(options);
}
const scope = options.scope || getCurrentScope();
const span = options.span || getActiveSpan();
// In Tracing-without-Performance (TwP) mode, spans are non-recording placeholders that carry no
// sampling decision of their own (it's deferred). The scope is the source of truth, so we read
// the trace headers from the scope. A non-recording span in *tracing* mode (e.g. an unsampled
// child or an ignored span) is different: it represents an explicit negative decision, which
// `spanToTraceHeader` correctly encodes as `-0`, so we keep reading those from the span.
const isTwpPlaceholder = spanIsNonRecordingSpan(span) && !hasSpansEnabled(client.getOptions());
// When there's no recording span and an external propagation context is registered (e.g. OTLP
// integration), return empty to let the external propagator handle outgoing request propagation.
if (!span && hasExternalPropagationContext()) {
return {};
}
const sentryTrace = span && !isTwpPlaceholder ? spanToTraceHeader(span) : scopeToTraceHeader(scope);
const dsc = span ? getDynamicSamplingContextFromSpan(span) : getDynamicSamplingContextFromScope(client, scope);
const baggage = dynamicSamplingContextToSentryBaggageHeader(dsc);
const isValidSentryTraceHeader = TRACEPARENT_REGEXP.test(sentryTrace);
if (!isValidSentryTraceHeader) {
debug.warn('Invalid sentry-trace data. Cannot generate trace data');
return {};
}
const traceData: SerializedTraceData = {
'sentry-trace': sentryTrace,
baggage,
};
if (options.propagateTraceparent) {
traceData.traceparent = span && !isTwpPlaceholder ? spanToTraceparentHeader(span) : scopeToTraceparentHeader(scope);
}
return traceData;
}
/**
* Get a sentry-trace header value for the given scope.
*/
function scopeToTraceHeader(scope: Scope): string {
const { traceId, sampled, propagationSpanId } = scope.getPropagationContext();
return generateSentryTraceHeader(traceId, propagationSpanId, sampled);
}
function scopeToTraceparentHeader(scope: Scope): string {
const { traceId, sampled, propagationSpanId } = scope.getPropagationContext();
return generateTraceparentHeader(traceId, propagationSpanId, sampled);
}