@sentry/opentelemetry 8.41.0 → 8.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/build/cjs/index.js +193 -156
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.js +195 -159
  4. package/build/esm/index.js.map +1 -1
  5. package/build/esm/package.json +1 -1
  6. package/build/types/asyncContextStrategy.d.ts.map +1 -1
  7. package/build/types/constants.d.ts +0 -1
  8. package/build/types/constants.d.ts.map +1 -1
  9. package/build/types/custom/client.d.ts +1 -2
  10. package/build/types/custom/client.d.ts.map +1 -1
  11. package/build/types/index.d.ts +1 -1
  12. package/build/types/index.d.ts.map +1 -1
  13. package/build/types/instrumentation.d.ts +3 -0
  14. package/build/types/instrumentation.d.ts.map +1 -1
  15. package/build/types/propagator.d.ts +8 -4
  16. package/build/types/propagator.d.ts.map +1 -1
  17. package/build/types/sampler.d.ts +1 -1
  18. package/build/types/sampler.d.ts.map +1 -1
  19. package/build/types/setupEventContextTrace.d.ts +1 -1
  20. package/build/types/setupEventContextTrace.d.ts.map +1 -1
  21. package/build/types/spanExporter.d.ts.map +1 -1
  22. package/build/types/trace.d.ts +1 -1
  23. package/build/types/trace.d.ts.map +1 -1
  24. package/build/types/types.d.ts +1 -1
  25. package/build/types/types.d.ts.map +1 -1
  26. package/build/types/utils/addOriginToSpan.d.ts +1 -1
  27. package/build/types/utils/addOriginToSpan.d.ts.map +1 -1
  28. package/build/types/utils/contextData.d.ts +1 -1
  29. package/build/types/utils/contextData.d.ts.map +1 -1
  30. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +1 -1
  31. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts.map +1 -1
  32. package/build/types/utils/generateSpanContextForPropagationContext.d.ts +3 -1
  33. package/build/types/utils/generateSpanContextForPropagationContext.d.ts.map +1 -1
  34. package/build/types/utils/getRequestSpanData.d.ts +1 -1
  35. package/build/types/utils/getRequestSpanData.d.ts.map +1 -1
  36. package/build/types/utils/getTraceData.d.ts +1 -1
  37. package/build/types/utils/getTraceData.d.ts.map +1 -1
  38. package/build/types/utils/makeTraceState.d.ts +2 -3
  39. package/build/types/utils/makeTraceState.d.ts.map +1 -1
  40. package/build/types/utils/mapStatus.d.ts +1 -1
  41. package/build/types/utils/mapStatus.d.ts.map +1 -1
  42. package/build/types/utils/parseSpanDescription.d.ts +1 -1
  43. package/build/types/utils/parseSpanDescription.d.ts.map +1 -1
  44. package/build/types-ts3.8/constants.d.ts +0 -1
  45. package/build/types-ts3.8/custom/client.d.ts +1 -2
  46. package/build/types-ts3.8/index.d.ts +1 -1
  47. package/build/types-ts3.8/instrumentation.d.ts +3 -0
  48. package/build/types-ts3.8/propagator.d.ts +8 -4
  49. package/build/types-ts3.8/sampler.d.ts +1 -1
  50. package/build/types-ts3.8/setupEventContextTrace.d.ts +1 -1
  51. package/build/types-ts3.8/trace.d.ts +1 -1
  52. package/build/types-ts3.8/types.d.ts +1 -1
  53. package/build/types-ts3.8/utils/addOriginToSpan.d.ts +1 -1
  54. package/build/types-ts3.8/utils/contextData.d.ts +1 -1
  55. package/build/types-ts3.8/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +1 -1
  56. package/build/types-ts3.8/utils/generateSpanContextForPropagationContext.d.ts +3 -1
  57. package/build/types-ts3.8/utils/getRequestSpanData.d.ts +1 -1
  58. package/build/types-ts3.8/utils/getTraceData.d.ts +1 -1
  59. package/build/types-ts3.8/utils/makeTraceState.d.ts +2 -3
  60. package/build/types-ts3.8/utils/mapStatus.d.ts +1 -1
  61. package/build/types-ts3.8/utils/parseSpanDescription.d.ts +1 -1
  62. package/package.json +10 -11
@@ -132,7 +132,7 @@ function getRequestSpanData(span) {
132
132
  return data;
133
133
  }
134
134
 
135
- function _optionalChain$8(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
135
+ function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
136
136
 
137
137
  // Typescript complains if we do not use `...args: any[]` for the mixin, with:
138
138
  // A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
@@ -173,7 +173,7 @@ function wrapClientClass
173
173
  */
174
174
  async flush(timeout) {
175
175
  const provider = this.traceProvider;
176
- const spanProcessor = _optionalChain$8([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
176
+ const spanProcessor = _optionalChain$6([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
177
177
 
178
178
  if (spanProcessor) {
179
179
  await spanProcessor.forceFlush();
@@ -205,7 +205,6 @@ const SENTRY_TRACE_HEADER = 'sentry-trace';
205
205
  const SENTRY_BAGGAGE_HEADER = 'baggage';
206
206
 
207
207
  const SENTRY_TRACE_STATE_DSC = 'sentry.dsc';
208
- const SENTRY_TRACE_STATE_PARENT_SPAN_ID = 'sentry.parent_span_id';
209
208
  const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = 'sentry.sampled_not_recording';
210
209
  const SENTRY_TRACE_STATE_URL = 'sentry.url';
211
210
 
@@ -274,6 +273,46 @@ function isSentryRequestSpan(span) {
274
273
  return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
275
274
  }
276
275
 
276
+ function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
277
+
278
+ /**
279
+ * OpenTelemetry only knows about SAMPLED or NONE decision,
280
+ * but for us it is important to differentiate between unset and unsampled.
281
+ *
282
+ * Both of these are identified as `traceFlags === TracegFlags.NONE`,
283
+ * but we additionally look at a special trace state to differentiate between them.
284
+ */
285
+ function getSamplingDecision(spanContext) {
286
+ const { traceFlags, traceState } = spanContext;
287
+
288
+ const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
289
+
290
+ // If trace flag is `SAMPLED`, we interpret this as sampled
291
+ // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
292
+ // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
293
+ // to identify which it is
294
+ if (traceFlags === api.TraceFlags.SAMPLED) {
295
+ return true;
296
+ }
297
+
298
+ if (sampledNotRecording) {
299
+ return false;
300
+ }
301
+
302
+ // Fall back to DSC as a last resort, that may also contain `sampled`...
303
+ const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
304
+ const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
305
+
306
+ if (_optionalChain$5([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
307
+ return true;
308
+ }
309
+ if (_optionalChain$5([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
310
+ return false;
311
+ }
312
+
313
+ return undefined;
314
+ }
315
+
277
316
  /**
278
317
  * Infer the op & description for a set of name, attributes and kind of a span.
279
318
  */
@@ -488,20 +527,31 @@ function getSanitizedUrl(
488
527
  */
489
528
  function enhanceDscWithOpenTelemetryRootSpanName(client) {
490
529
  client.on('createDsc', (dsc, rootSpan) => {
530
+ if (!rootSpan) {
531
+ return;
532
+ }
533
+
491
534
  // We want to overwrite the transaction on the DSC that is created by default in core
492
535
  // The reason for this is that we want to infer the span name, not use the initial one
493
536
  // Otherwise, we'll get names like "GET" instead of e.g. "GET /foo"
494
537
  // `parseSpanDescription` takes the attributes of the span into account for the name
495
538
  // This mutates the passed-in DSC
496
- if (rootSpan) {
497
- const jsonSpan = core.spanToJSON(rootSpan);
498
- const attributes = jsonSpan.data || {};
499
- const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
500
-
501
- const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
502
- if (source !== 'url' && description) {
503
- dsc.transaction = description;
504
- }
539
+
540
+ const jsonSpan = core.spanToJSON(rootSpan);
541
+ const attributes = jsonSpan.data || {};
542
+ const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
543
+
544
+ const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
545
+ if (source !== 'url' && description) {
546
+ dsc.transaction = description;
547
+ }
548
+
549
+ // Also ensure sampling decision is correctly inferred
550
+ // In core, we use `spanIsSampled`, which just looks at the trace flags
551
+ // but in OTEL, we use a slightly more complex logic to be able to differntiate between unsampled and deferred sampling
552
+ if (core.hasTracingEnabled()) {
553
+ const sampled = getSamplingDecision(rootSpan.spanContext());
554
+ dsc.sampled = sampled == undefined ? undefined : String(sampled);
505
555
  }
506
556
  });
507
557
  }
@@ -510,7 +560,6 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
510
560
  * Generate a TraceState for the given data.
511
561
  */
512
562
  function makeTraceState({
513
- parentSpanId,
514
563
  dsc,
515
564
  sampled,
516
565
  }
@@ -519,11 +568,7 @@ function makeTraceState({
519
568
  // We store the DSC as OTEL trace state on the span context
520
569
  const dscString = dsc ? core.dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
521
570
 
522
- // We _always_ set the parent span ID, even if it is empty
523
- // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
524
- // vs the trace state was not set at all (in which case we want to do fallback handling)
525
- // If `''`, it should be considered "no parent"
526
- const traceStateBase = new core$1.TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
571
+ const traceStateBase = new core$1.TraceState();
527
572
 
528
573
  const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
529
574
 
@@ -535,17 +580,19 @@ function makeTraceState({
535
580
  /**
536
581
  * Generates a SpanContext that represents a PropagationContext.
537
582
  * This can be set on a `context` to make this a (virtual) active span.
583
+ *
584
+ * @deprecated This function is deprecated and will be removed in the next major version.
538
585
  */
539
586
  function generateSpanContextForPropagationContext(propagationContext) {
540
587
  // We store the DSC as OTEL trace state on the span context
541
588
  const traceState = makeTraceState({
542
- parentSpanId: propagationContext.parentSpanId,
543
589
  dsc: propagationContext.dsc,
544
590
  sampled: propagationContext.sampled,
545
591
  });
546
592
 
547
593
  const spanContext = {
548
594
  traceId: propagationContext.traceId,
595
+ // TODO: Do not create an invalid span context here
549
596
  spanId: propagationContext.parentSpanId || '',
550
597
  isRemote: true,
551
598
  traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
@@ -569,46 +616,6 @@ function getActiveSpan() {
569
616
  */
570
617
  const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
571
618
 
572
- function _optionalChain$7(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
573
-
574
- /**
575
- * OpenTelemetry only knows about SAMPLED or NONE decision,
576
- * but for us it is important to differentiate between unset and unsampled.
577
- *
578
- * Both of these are identified as `traceFlags === TracegFlags.NONE`,
579
- * but we additionally look at a special trace state to differentiate between them.
580
- */
581
- function getSamplingDecision(spanContext) {
582
- const { traceFlags, traceState } = spanContext;
583
-
584
- const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
585
-
586
- // If trace flag is `SAMPLED`, we interpret this as sampled
587
- // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
588
- // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
589
- // to identify which it is
590
- if (traceFlags === api.TraceFlags.SAMPLED) {
591
- return true;
592
- }
593
-
594
- if (sampledNotRecording) {
595
- return false;
596
- }
597
-
598
- // Fall back to DSC as a last resort, that may also contain `sampled`...
599
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
600
- const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
601
-
602
- if (_optionalChain$7([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
603
- return true;
604
- }
605
- if (_optionalChain$7([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
606
- return false;
607
- }
608
-
609
- return undefined;
610
- }
611
-
612
619
  const setupElements = new Set();
613
620
 
614
621
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -621,7 +628,7 @@ function setIsSetup(element) {
621
628
  setupElements.add(element);
622
629
  }
623
630
 
624
- function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
631
+ function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
625
632
 
626
633
  /** Get the Sentry propagation context from a span context. */
627
634
  function getPropagationContextFromSpan(span) {
@@ -633,8 +640,7 @@ function getPropagationContextFromSpan(span) {
633
640
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
634
641
  const traceStateDsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
635
642
 
636
- const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) || undefined : undefined;
637
-
643
+ const parentSpanId = spanHasParentId(span) ? span.parentSpanId : undefined;
638
644
  const sampled = getSamplingDecision(spanContext);
639
645
 
640
646
  // No trace state? --> Take DSC from root span
@@ -675,12 +681,8 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
675
681
  const activeSpan = api.trace.getSpan(context);
676
682
  const url = activeSpan && getCurrentURL(activeSpan);
677
683
 
678
- const tracePropagationTargets = _optionalChain$6([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
679
- if (
680
- typeof url === 'string' &&
681
- tracePropagationTargets &&
682
- !this._shouldInjectTraceData(tracePropagationTargets, url)
683
- ) {
684
+ const tracePropagationTargets = _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
685
+ if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
684
686
  DEBUG_BUILD &&
685
687
  core.logger.log(
686
688
  '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:',
@@ -734,20 +736,9 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
734
736
  : maybeSentryTraceHeader
735
737
  : undefined;
736
738
 
737
- const propagationContext = core.propagationContextFromHeaders(sentryTrace, baggage);
738
-
739
739
  // Add remote parent span context
740
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(context, { sentryTrace, baggage });
741
-
742
- // Also update the scope on the context (to be sure this is picked up everywhere)
743
- const scopes = getScopesFromContext(ctxWithSpanContext);
744
- const newScopes = {
745
- scope: scopes ? scopes.scope.clone() : core.getCurrentScope().clone(),
746
- isolationScope: scopes ? scopes.isolationScope : core.getIsolationScope(),
747
- };
748
- newScopes.scope.setPropagationContext(propagationContext);
749
-
750
- return setScopesOnContext(ctxWithSpanContext, newScopes);
740
+ // If there is no incoming trace, this will return the context as-is
741
+ return ensureScopesOnContext(getContextWithRemoteActiveSpan(context, { sentryTrace, baggage }));
751
742
  }
752
743
 
753
744
  /**
@@ -756,22 +747,36 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
756
747
  fields() {
757
748
  return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER];
758
749
  }
750
+ }
759
751
 
760
- /** If we want to inject trace data for a given URL. */
761
- _shouldInjectTraceData(tracePropagationTargets, url) {
762
- if (tracePropagationTargets === undefined) {
763
- return true;
764
- }
752
+ const NOT_PROPAGATED_MESSAGE =
753
+ '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:';
765
754
 
766
- const cachedDecision = this._urlMatchesTargetsMap.get(url);
767
- if (cachedDecision !== undefined) {
768
- return cachedDecision;
769
- }
755
+ /**
756
+ * Check if a given URL should be propagated to or not.
757
+ * If no url is defined, or no trace propagation targets are defined, this will always return `true`.
758
+ * You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
759
+ */
760
+ function shouldPropagateTraceForUrl(
761
+ url,
762
+ tracePropagationTargets,
763
+ decisionMap,
764
+ ) {
765
+ if (typeof url !== 'string' || !tracePropagationTargets) {
766
+ return true;
767
+ }
770
768
 
771
- const decision = core.stringMatchesSomePattern(url, tracePropagationTargets);
772
- this._urlMatchesTargetsMap.set(url, decision);
773
- return decision;
769
+ const cachedDecision = _optionalChain$4([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
770
+ if (cachedDecision !== undefined) {
771
+ DEBUG_BUILD && !cachedDecision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
772
+ return cachedDecision;
774
773
  }
774
+
775
+ const decision = core.stringMatchesSomePattern(url, tracePropagationTargets);
776
+ _optionalChain$4([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
777
+
778
+ DEBUG_BUILD && !decision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
779
+ return decision;
775
780
  }
776
781
 
777
782
  /**
@@ -781,13 +786,28 @@ function getInjectionData(context)
781
786
 
782
787
  {
783
788
  const span = api.trace.getSpan(context);
784
- const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
785
789
 
786
- // If we have a local span, we can just pick everything from it
787
- if (span && !spanIsRemote) {
790
+ // If we have a remote span, the spanId should be considered as the parentSpanId, not spanId itself
791
+ // Instead, we use a virtual (generated) spanId for propagation
792
+ if (span && span.spanContext().isRemote) {
788
793
  const spanContext = span.spanContext();
794
+ const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(span);
789
795
 
796
+ return {
797
+ dynamicSamplingContext,
798
+ traceId: spanContext.traceId,
799
+ // Because this is a remote span, we do not want to propagate this directly
800
+ // As otherwise things may be attached "directly" to an unrelated span
801
+ spanId: core.generateSpanId(),
802
+ sampled: getSamplingDecision(spanContext),
803
+ };
804
+ }
805
+
806
+ // If we have a local span, we just use this
807
+ if (span) {
808
+ const spanContext = span.spanContext();
790
809
  const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(span);
810
+
791
811
  return {
792
812
  dynamicSamplingContext,
793
813
  traceId: spanContext.traceId,
@@ -797,7 +817,8 @@ function getInjectionData(context)
797
817
  }
798
818
 
799
819
  // Else we try to use the propagation context from the scope
800
- const scope = _optionalChain$6([getScopesFromContext, 'call', _8 => _8(context), 'optionalAccess', _9 => _9.scope]) || core.getCurrentScope();
820
+ // The only scenario where this should happen is when we neither have a span, nor an incoming trace
821
+ const scope = _optionalChain$4([getScopesFromContext, 'call', _9 => _9(context), 'optionalAccess', _10 => _10.scope]) || core.getCurrentScope();
801
822
  const client = core.getClient();
802
823
 
803
824
  const propagationContext = scope.getPropagationContext();
@@ -805,6 +826,8 @@ function getInjectionData(context)
805
826
  return {
806
827
  dynamicSamplingContext,
807
828
  traceId: propagationContext.traceId,
829
+ // TODO(v9): Use generateSpanId() instead
830
+ // eslint-disable-next-line deprecation/deprecation
808
831
  spanId: propagationContext.spanId,
809
832
  sampled: propagationContext.sampled,
810
833
  };
@@ -816,7 +839,21 @@ function getContextWithRemoteActiveSpan(
816
839
  ) {
817
840
  const propagationContext = core.propagationContextFromHeaders(sentryTrace, baggage);
818
841
 
819
- const spanContext = generateSpanContextForPropagationContext(propagationContext);
842
+ const { traceId, parentSpanId, sampled, dsc } = propagationContext;
843
+
844
+ // We only want to set the virtual span if we are continuing a concrete trace
845
+ // Otherwise, we ignore the incoming trace here, e.g. if we have no trace headers
846
+ if (!parentSpanId) {
847
+ return ctx;
848
+ }
849
+
850
+ const spanContext = generateRemoteSpanContext({
851
+ traceId,
852
+ spanId: parentSpanId,
853
+ sampled,
854
+ dsc,
855
+ });
856
+
820
857
  return api.trace.setSpanContext(ctx, spanContext);
821
858
  }
822
859
 
@@ -829,11 +866,24 @@ function continueTraceAsRemoteSpan(
829
866
  options,
830
867
  callback,
831
868
  ) {
832
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(ctx, options);
869
+ const ctxWithSpanContext = ensureScopesOnContext(getContextWithRemoteActiveSpan(ctx, options));
833
870
 
834
871
  return api.context.with(ctxWithSpanContext, callback);
835
872
  }
836
873
 
874
+ function ensureScopesOnContext(ctx) {
875
+ // If there are no scopes yet on the context, ensure we have them
876
+ const scopes = getScopesFromContext(ctx);
877
+ const newScopes = {
878
+ // If we have no scope here, this is most likely either the root context or a context manually derived from it
879
+ // In this case, we want to fork the current scope, to ensure we do not pollute the root scope
880
+ scope: scopes ? scopes.scope : core.getCurrentScope().clone(),
881
+ isolationScope: scopes ? scopes.isolationScope : core.getIsolationScope(),
882
+ };
883
+
884
+ return setScopesOnContext(ctx, newScopes);
885
+ }
886
+
837
887
  /** Try to get the existing baggage header so we can merge this in. */
838
888
  function getExistingBaggage(carrier) {
839
889
  try {
@@ -857,13 +907,13 @@ function getCurrentURL(span) {
857
907
  const spanData = core.spanToJSON(span).data;
858
908
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
859
909
  // eslint-disable-next-line deprecation/deprecation
860
- const urlAttribute = _optionalChain$6([spanData, 'optionalAccess', _10 => _10[semanticConventions.SEMATTRS_HTTP_URL]]) || _optionalChain$6([spanData, 'optionalAccess', _11 => _11[semanticConventions.ATTR_URL_FULL]]);
910
+ const urlAttribute = _optionalChain$4([spanData, 'optionalAccess', _11 => _11[semanticConventions.SEMATTRS_HTTP_URL]]) || _optionalChain$4([spanData, 'optionalAccess', _12 => _12[semanticConventions.ATTR_URL_FULL]]);
861
911
  if (urlAttribute) {
862
912
  return urlAttribute;
863
913
  }
864
914
 
865
915
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
866
- const urlTraceState = _optionalChain$6([span, 'access', _12 => _12.spanContext, 'call', _13 => _13(), 'access', _14 => _14.traceState, 'optionalAccess', _15 => _15.get, 'call', _16 => _16(SENTRY_TRACE_STATE_URL)]);
916
+ const urlTraceState = _optionalChain$4([span, 'access', _13 => _13.spanContext, 'call', _14 => _14(), 'access', _15 => _15.traceState, 'optionalAccess', _16 => _16.get, 'call', _17 => _17(SENTRY_TRACE_STATE_URL)]);
867
917
  if (urlTraceState) {
868
918
  return urlTraceState;
869
919
  }
@@ -871,7 +921,30 @@ function getCurrentURL(span) {
871
921
  return undefined;
872
922
  }
873
923
 
874
- function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
924
+ function generateRemoteSpanContext({
925
+ spanId,
926
+ traceId,
927
+ sampled,
928
+ dsc,
929
+ }
930
+
931
+ ) {
932
+ // We store the DSC as OTEL trace state on the span context
933
+ const traceState = makeTraceState({
934
+ dsc,
935
+ sampled,
936
+ });
937
+
938
+ const spanContext = {
939
+ traceId,
940
+ spanId,
941
+ isRemote: true,
942
+ traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
943
+ traceState,
944
+ };
945
+
946
+ return spanContext;
947
+ }
875
948
 
876
949
  /**
877
950
  * Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -1029,39 +1102,11 @@ function ensureTimestampInMilliseconds(timestamp) {
1029
1102
 
1030
1103
  function getContext(scope, forceTransaction) {
1031
1104
  const ctx = getContextForScope(scope);
1032
- // Note: If the context is the ROOT_CONTEXT, no scope is attached
1033
- // Thus we will not use the propagation context in this case, which is desired
1034
- const actualScope = _optionalChain$5([getScopesFromContext, 'call', _ => _(ctx), 'optionalAccess', _2 => _2.scope]);
1035
1105
  const parentSpan = api.trace.getSpan(ctx);
1036
1106
 
1037
- // In the case that we have no parent span, we need to "simulate" one to ensure the propagation context is correct
1107
+ // In the case that we have no parent span, we start a new trace
1108
+ // Note that if we continue a trace, we'll always have a remote parent span here anyhow
1038
1109
  if (!parentSpan) {
1039
- const client = core.getClient();
1040
-
1041
- if (actualScope && client) {
1042
- const propagationContext = actualScope.getPropagationContext();
1043
-
1044
- // We store the DSC as OTEL trace state on the span context
1045
- const traceState = makeTraceState({
1046
- parentSpanId: propagationContext.parentSpanId,
1047
- // Not defined yet, we want to pick this up on-demand only
1048
- dsc: undefined,
1049
- sampled: propagationContext.sampled,
1050
- });
1051
-
1052
- const spanOptions = {
1053
- traceId: propagationContext.traceId,
1054
- spanId: propagationContext.parentSpanId || propagationContext.spanId,
1055
- isRemote: true,
1056
- traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
1057
- traceState,
1058
- };
1059
-
1060
- // Add remote parent span context,
1061
- return api.trace.setSpanContext(ctx, spanOptions);
1062
- }
1063
-
1064
- // if we have no scope or client, we just return the context as-is
1065
1110
  return ctx;
1066
1111
  }
1067
1112
 
@@ -1087,7 +1132,6 @@ function getContext(scope, forceTransaction) {
1087
1132
 
1088
1133
  const traceState = makeTraceState({
1089
1134
  dsc,
1090
- parentSpanId: spanId !== api.INVALID_SPANID ? spanId : undefined,
1091
1135
  sampled,
1092
1136
  });
1093
1137
 
@@ -1165,7 +1209,6 @@ function suppressTracing(callback) {
1165
1209
  return api.context.with(ctx, callback);
1166
1210
  }
1167
1211
 
1168
- function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1169
1212
  /** Ensure the `trace` context is set on all events. */
1170
1213
  function setupEventContextTrace(client) {
1171
1214
  client.on('preprocessEvent', event => {
@@ -1176,25 +1219,9 @@ function setupEventContextTrace(client) {
1176
1219
  return;
1177
1220
  }
1178
1221
 
1179
- const spanContext = span.spanContext();
1180
-
1181
- // If we have a parent span id from trace state, use that ('' means no parent should be used)
1182
- // Else, pick the one from the span
1183
- const parentSpanIdFromTraceState = _optionalChain$4([spanContext, 'access', _ => _.traceState, 'optionalAccess', _2 => _2.get, 'call', _3 => _3(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1184
- const parent_span_id =
1185
- typeof parentSpanIdFromTraceState === 'string'
1186
- ? parentSpanIdFromTraceState || undefined
1187
- : spanHasParentId(span)
1188
- ? span.parentSpanId
1189
- : undefined;
1190
-
1191
1222
  // If event has already set `trace` context, use that one.
1192
1223
  event.contexts = {
1193
- trace: core.dropUndefinedKeys({
1194
- trace_id: spanContext.traceId,
1195
- span_id: spanContext.spanId,
1196
- parent_span_id,
1197
- }),
1224
+ trace: core.spanToTraceContext(span),
1198
1225
  ...event.contexts,
1199
1226
  };
1200
1227
 
@@ -1759,15 +1786,12 @@ function createTransactionForOtelSpan(span) {
1759
1786
 
1760
1787
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1761
1788
 
1762
- const parentSpanIdFromTraceState = _optionalChain$2([span, 'access', _2 => _2.spanContext, 'call', _3 => _3(), 'access', _4 => _4.traceState, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1763
-
1764
1789
  // If parentSpanIdFromTraceState is defined at all, we want it to take precedence
1765
1790
  // In that case, an empty string should be interpreted as "no parent span id",
1766
1791
  // even if `span.parentSpanId` is set
1767
1792
  // this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
1768
1793
  // We only want to continue the traceId in this case, but ignore the parent span
1769
- const parent_span_id =
1770
- typeof parentSpanIdFromTraceState === 'string' ? parentSpanIdFromTraceState || undefined : span.parentSpanId;
1794
+ const parent_span_id = span.parentSpanId;
1771
1795
 
1772
1796
  const status = mapStatus(span);
1773
1797
 
@@ -2038,7 +2062,7 @@ class SentrySampler {
2038
2062
  ) {
2039
2063
  const options = this._client.getOptions();
2040
2064
 
2041
- const parentSpan = api.trace.getSpan(context);
2065
+ const parentSpan = getValidSpan(context);
2042
2066
  const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
2043
2067
 
2044
2068
  if (!core.hasTracingEnabled(options)) {
@@ -2201,9 +2225,21 @@ function getBaseTraceState(context, spanAttributes) {
2201
2225
  return traceState;
2202
2226
  }
2203
2227
 
2228
+ /**
2229
+ * If the active span is invalid, we want to ignore it as parent.
2230
+ * This aligns with how otel tracers and default samplers handle these cases.
2231
+ */
2232
+ function getValidSpan(context) {
2233
+ const span = api.trace.getSpan(context);
2234
+ return span && api.isSpanContextValid(span.spanContext()) ? span : undefined;
2235
+ }
2236
+
2204
2237
  /**
2205
2238
  * This method takes an OpenTelemetry instrumentation or
2206
2239
  * array of instrumentations and registers them with OpenTelemetry.
2240
+ *
2241
+ * @deprecated This method will be removed in the next major version of the SDK.
2242
+ * Use the `openTelemetryInstrumentations` option in `Sentry.init()` or your custom Sentry Client instead.
2207
2243
  */
2208
2244
  function addOpenTelemetryInstrumentation(...instrumentations) {
2209
2245
  instrumentation.registerInstrumentations({
@@ -2232,6 +2268,7 @@ exports.isSentryRequestSpan = isSentryRequestSpan;
2232
2268
  exports.openTelemetrySetupCheck = openTelemetrySetupCheck;
2233
2269
  exports.setOpenTelemetryContextAsyncContextStrategy = setOpenTelemetryContextAsyncContextStrategy;
2234
2270
  exports.setupEventContextTrace = setupEventContextTrace;
2271
+ exports.shouldPropagateTraceForUrl = shouldPropagateTraceForUrl;
2235
2272
  exports.spanHasAttributes = spanHasAttributes;
2236
2273
  exports.spanHasEvents = spanHasEvents;
2237
2274
  exports.spanHasKind = spanHasKind;