(
options: { span?: Span; scope?: Scope; client?: Client; propagateTraceparent?: boolean } = {},
)
| 33 | * or meta tag name. |
| 34 | */ |
| 35 | export function getTraceData( |
| 36 | options: { span?: Span; scope?: Scope; client?: Client; propagateTraceparent?: boolean } = {}, |
| 37 | ): SerializedTraceData { |
| 38 | const client = options.client || getClient(); |
| 39 | if (!isEnabled() || !client) { |
| 40 | return {}; |
| 41 | } |
| 42 | |
| 43 | const carrier = getMainCarrier(); |
| 44 | const acs = getAsyncContextStrategy(carrier); |
| 45 | if (acs.getTraceData) { |
| 46 | return acs.getTraceData(options); |
| 47 | } |
| 48 | |
| 49 | const scope = options.scope || getCurrentScope(); |
| 50 | const span = options.span || getActiveSpan(); |
| 51 | |
| 52 | // In Tracing-without-Performance (TwP) mode, spans are non-recording placeholders that carry no |
| 53 | // sampling decision of their own (it's deferred). The scope is the source of truth, so we read |
| 54 | // the trace headers from the scope. A non-recording span in *tracing* mode (e.g. an unsampled |
| 55 | // child or an ignored span) is different: it represents an explicit negative decision, which |
| 56 | // `spanToTraceHeader` correctly encodes as `-0`, so we keep reading those from the span. |
| 57 | const isTwpPlaceholder = spanIsNonRecordingSpan(span) && !hasSpansEnabled(client.getOptions()); |
| 58 | |
| 59 | // When there's no recording span and an external propagation context is registered (e.g. OTLP |
| 60 | // integration), return empty to let the external propagator handle outgoing request propagation. |
| 61 | if (!span && hasExternalPropagationContext()) { |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | const sentryTrace = span && !isTwpPlaceholder ? spanToTraceHeader(span) : scopeToTraceHeader(scope); |
| 66 | const dsc = span ? getDynamicSamplingContextFromSpan(span) : getDynamicSamplingContextFromScope(client, scope); |
| 67 | const baggage = dynamicSamplingContextToSentryBaggageHeader(dsc); |
| 68 | |
| 69 | const isValidSentryTraceHeader = TRACEPARENT_REGEXP.test(sentryTrace); |
| 70 | if (!isValidSentryTraceHeader) { |
| 71 | debug.warn('Invalid sentry-trace data. Cannot generate trace data'); |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | const traceData: SerializedTraceData = { |
| 76 | 'sentry-trace': sentryTrace, |
| 77 | baggage, |
| 78 | }; |
| 79 | |
| 80 | if (options.propagateTraceparent) { |
| 81 | traceData.traceparent = span && !isTwpPlaceholder ? spanToTraceparentHeader(span) : scopeToTraceparentHeader(scope); |
| 82 | } |
| 83 | |
| 84 | return traceData; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get a sentry-trace header value for the given scope. |
no test coverage detected