@sentry/opentelemetry 8.42.0 → 8.44.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.
@@ -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
 
@@ -538,10 +583,9 @@ function makeTraceState({
538
583
  *
539
584
  * @deprecated This function is deprecated and will be removed in the next major version.
540
585
  */
541
- function generateSpanContextForPropagationContext$1(propagationContext) {
586
+ function generateSpanContextForPropagationContext(propagationContext) {
542
587
  // We store the DSC as OTEL trace state on the span context
543
588
  const traceState = makeTraceState({
544
- parentSpanId: propagationContext.parentSpanId,
545
589
  dsc: propagationContext.dsc,
546
590
  sampled: propagationContext.sampled,
547
591
  });
@@ -572,46 +616,6 @@ function getActiveSpan() {
572
616
  */
573
617
  const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
574
618
 
575
- 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; }
576
-
577
- /**
578
- * OpenTelemetry only knows about SAMPLED or NONE decision,
579
- * but for us it is important to differentiate between unset and unsampled.
580
- *
581
- * Both of these are identified as `traceFlags === TracegFlags.NONE`,
582
- * but we additionally look at a special trace state to differentiate between them.
583
- */
584
- function getSamplingDecision(spanContext) {
585
- const { traceFlags, traceState } = spanContext;
586
-
587
- const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
588
-
589
- // If trace flag is `SAMPLED`, we interpret this as sampled
590
- // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
591
- // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
592
- // to identify which it is
593
- if (traceFlags === api.TraceFlags.SAMPLED) {
594
- return true;
595
- }
596
-
597
- if (sampledNotRecording) {
598
- return false;
599
- }
600
-
601
- // Fall back to DSC as a last resort, that may also contain `sampled`...
602
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
603
- const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
604
-
605
- if (_optionalChain$7([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
606
- return true;
607
- }
608
- if (_optionalChain$7([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
609
- return false;
610
- }
611
-
612
- return undefined;
613
- }
614
-
615
619
  const setupElements = new Set();
616
620
 
617
621
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -624,7 +628,7 @@ function setIsSetup(element) {
624
628
  setupElements.add(element);
625
629
  }
626
630
 
627
- 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; }
628
632
 
629
633
  /** Get the Sentry propagation context from a span context. */
630
634
  function getPropagationContextFromSpan(span) {
@@ -636,8 +640,7 @@ function getPropagationContextFromSpan(span) {
636
640
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
637
641
  const traceStateDsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
638
642
 
639
- const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) || undefined : undefined;
640
-
643
+ const parentSpanId = spanHasParentId(span) ? span.parentSpanId : undefined;
641
644
  const sampled = getSamplingDecision(spanContext);
642
645
 
643
646
  // No trace state? --> Take DSC from root span
@@ -678,7 +681,7 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
678
681
  const activeSpan = api.trace.getSpan(context);
679
682
  const url = activeSpan && getCurrentURL(activeSpan);
680
683
 
681
- const tracePropagationTargets = _optionalChain$6([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
684
+ const tracePropagationTargets = _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
682
685
  if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
683
686
  DEBUG_BUILD &&
684
687
  core.logger.log(
@@ -733,20 +736,9 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
733
736
  : maybeSentryTraceHeader
734
737
  : undefined;
735
738
 
736
- const propagationContext = core.propagationContextFromHeaders(sentryTrace, baggage);
737
-
738
739
  // Add remote parent span context
739
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(context, { sentryTrace, baggage });
740
-
741
- // Also update the scope on the context (to be sure this is picked up everywhere)
742
- const scopes = getScopesFromContext(ctxWithSpanContext);
743
- const newScopes = {
744
- scope: scopes ? scopes.scope.clone() : core.getCurrentScope().clone(),
745
- isolationScope: scopes ? scopes.isolationScope : core.getIsolationScope(),
746
- };
747
- newScopes.scope.setPropagationContext(propagationContext);
748
-
749
- 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 }));
750
742
  }
751
743
 
752
744
  /**
@@ -774,14 +766,14 @@ function shouldPropagateTraceForUrl(
774
766
  return true;
775
767
  }
776
768
 
777
- const cachedDecision = _optionalChain$6([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
769
+ const cachedDecision = _optionalChain$4([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
778
770
  if (cachedDecision !== undefined) {
779
771
  DEBUG_BUILD && !cachedDecision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
780
772
  return cachedDecision;
781
773
  }
782
774
 
783
775
  const decision = core.stringMatchesSomePattern(url, tracePropagationTargets);
784
- _optionalChain$6([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
776
+ _optionalChain$4([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
785
777
 
786
778
  DEBUG_BUILD && !decision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
787
779
  return decision;
@@ -794,13 +786,28 @@ function getInjectionData(context)
794
786
 
795
787
  {
796
788
  const span = api.trace.getSpan(context);
797
- const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _9 => _9.spanContext, 'call', _10 => _10(), 'access', _11 => _11.isRemote]);
798
789
 
799
- // If we have a local span, we can just pick everything from it
800
- 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) {
801
793
  const spanContext = span.spanContext();
794
+ const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(span);
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
+ }
802
805
 
806
+ // If we have a local span, we just use this
807
+ if (span) {
808
+ const spanContext = span.spanContext();
803
809
  const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(span);
810
+
804
811
  return {
805
812
  dynamicSamplingContext,
806
813
  traceId: spanContext.traceId,
@@ -810,7 +817,8 @@ function getInjectionData(context)
810
817
  }
811
818
 
812
819
  // Else we try to use the propagation context from the scope
813
- const scope = _optionalChain$6([getScopesFromContext, 'call', _12 => _12(context), 'optionalAccess', _13 => _13.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();
814
822
  const client = core.getClient();
815
823
 
816
824
  const propagationContext = scope.getPropagationContext();
@@ -831,7 +839,21 @@ function getContextWithRemoteActiveSpan(
831
839
  ) {
832
840
  const propagationContext = core.propagationContextFromHeaders(sentryTrace, baggage);
833
841
 
834
- 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
+
835
857
  return api.trace.setSpanContext(ctx, spanContext);
836
858
  }
837
859
 
@@ -844,11 +866,24 @@ function continueTraceAsRemoteSpan(
844
866
  options,
845
867
  callback,
846
868
  ) {
847
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(ctx, options);
869
+ const ctxWithSpanContext = ensureScopesOnContext(getContextWithRemoteActiveSpan(ctx, options));
848
870
 
849
871
  return api.context.with(ctxWithSpanContext, callback);
850
872
  }
851
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
+
852
887
  /** Try to get the existing baggage header so we can merge this in. */
853
888
  function getExistingBaggage(carrier) {
854
889
  try {
@@ -872,13 +907,13 @@ function getCurrentURL(span) {
872
907
  const spanData = core.spanToJSON(span).data;
873
908
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
874
909
  // eslint-disable-next-line deprecation/deprecation
875
- const urlAttribute = _optionalChain$6([spanData, 'optionalAccess', _14 => _14[semanticConventions.SEMATTRS_HTTP_URL]]) || _optionalChain$6([spanData, 'optionalAccess', _15 => _15[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]]);
876
911
  if (urlAttribute) {
877
912
  return urlAttribute;
878
913
  }
879
914
 
880
915
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
881
- const urlTraceState = _optionalChain$6([span, 'access', _16 => _16.spanContext, 'call', _17 => _17(), 'access', _18 => _18.traceState, 'optionalAccess', _19 => _19.get, 'call', _20 => _20(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)]);
882
917
  if (urlTraceState) {
883
918
  return urlTraceState;
884
919
  }
@@ -886,28 +921,31 @@ function getCurrentURL(span) {
886
921
  return undefined;
887
922
  }
888
923
 
889
- // TODO: Adjust this behavior to avoid invalid spans
890
- function generateSpanContextForPropagationContext(propagationContext) {
924
+ function generateRemoteSpanContext({
925
+ spanId,
926
+ traceId,
927
+ sampled,
928
+ dsc,
929
+ }
930
+
931
+ ) {
891
932
  // We store the DSC as OTEL trace state on the span context
892
933
  const traceState = makeTraceState({
893
- parentSpanId: propagationContext.parentSpanId,
894
- dsc: propagationContext.dsc,
895
- sampled: propagationContext.sampled,
934
+ dsc,
935
+ sampled,
896
936
  });
897
937
 
898
938
  const spanContext = {
899
- traceId: propagationContext.traceId,
900
- spanId: propagationContext.parentSpanId || '',
939
+ traceId,
940
+ spanId,
901
941
  isRemote: true,
902
- traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
942
+ traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
903
943
  traceState,
904
944
  };
905
945
 
906
946
  return spanContext;
907
947
  }
908
948
 
909
- 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; }
910
-
911
949
  /**
912
950
  * Wraps a function with a transaction/span and finishes the span after the function is done.
913
951
  * The created span is the active span and will be used as parent by other spans created inside the function
@@ -1064,40 +1102,11 @@ function ensureTimestampInMilliseconds(timestamp) {
1064
1102
 
1065
1103
  function getContext(scope, forceTransaction) {
1066
1104
  const ctx = getContextForScope(scope);
1067
- // Note: If the context is the ROOT_CONTEXT, no scope is attached
1068
- // Thus we will not use the propagation context in this case, which is desired
1069
- const actualScope = _optionalChain$5([getScopesFromContext, 'call', _ => _(ctx), 'optionalAccess', _2 => _2.scope]);
1070
1105
  const parentSpan = api.trace.getSpan(ctx);
1071
1106
 
1072
- // 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
1073
1109
  if (!parentSpan) {
1074
- const client = core.getClient();
1075
-
1076
- if (actualScope && client) {
1077
- const propagationContext = actualScope.getPropagationContext();
1078
-
1079
- // We store the DSC as OTEL trace state on the span context
1080
- const traceState = makeTraceState({
1081
- parentSpanId: propagationContext.parentSpanId,
1082
- // Not defined yet, we want to pick this up on-demand only
1083
- dsc: undefined,
1084
- sampled: propagationContext.sampled,
1085
- });
1086
-
1087
- const spanOptions = {
1088
- traceId: propagationContext.traceId,
1089
- // eslint-disable-next-line deprecation/deprecation
1090
- spanId: propagationContext.parentSpanId || propagationContext.spanId,
1091
- isRemote: true,
1092
- traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
1093
- traceState,
1094
- };
1095
-
1096
- // Add remote parent span context,
1097
- return api.trace.setSpanContext(ctx, spanOptions);
1098
- }
1099
-
1100
- // if we have no scope or client, we just return the context as-is
1101
1110
  return ctx;
1102
1111
  }
1103
1112
 
@@ -1123,7 +1132,6 @@ function getContext(scope, forceTransaction) {
1123
1132
 
1124
1133
  const traceState = makeTraceState({
1125
1134
  dsc,
1126
- parentSpanId: spanId !== api.INVALID_SPANID ? spanId : undefined,
1127
1135
  sampled,
1128
1136
  });
1129
1137
 
@@ -1201,8 +1209,6 @@ function suppressTracing(callback) {
1201
1209
  return api.context.with(ctx, callback);
1202
1210
  }
1203
1211
 
1204
- 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; }
1205
-
1206
1212
  /** Ensure the `trace` context is set on all events. */
1207
1213
  function setupEventContextTrace(client) {
1208
1214
  client.on('preprocessEvent', event => {
@@ -1213,25 +1219,9 @@ function setupEventContextTrace(client) {
1213
1219
  return;
1214
1220
  }
1215
1221
 
1216
- const spanContext = span.spanContext();
1217
-
1218
- // If we have a parent span id from trace state, use that ('' means no parent should be used)
1219
- // Else, pick the one from the span
1220
- const parentSpanIdFromTraceState = _optionalChain$4([spanContext, 'access', _ => _.traceState, 'optionalAccess', _2 => _2.get, 'call', _3 => _3(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1221
- const parent_span_id =
1222
- typeof parentSpanIdFromTraceState === 'string'
1223
- ? parentSpanIdFromTraceState || undefined
1224
- : spanHasParentId(span)
1225
- ? span.parentSpanId
1226
- : undefined;
1227
-
1228
1222
  // If event has already set `trace` context, use that one.
1229
1223
  event.contexts = {
1230
- trace: core.dropUndefinedKeys({
1231
- trace_id: spanContext.traceId,
1232
- span_id: spanContext.spanId,
1233
- parent_span_id,
1234
- }),
1224
+ trace: core.spanToTraceContext(span),
1235
1225
  ...event.contexts,
1236
1226
  };
1237
1227
 
@@ -1691,7 +1681,9 @@ class SentrySpanExporter {
1691
1681
  const remainingOpenSpanCount = finishedSpans.length - sentSpanCount;
1692
1682
 
1693
1683
  DEBUG_BUILD &&
1694
- core.logger.log(`SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} unsent spans remaining`);
1684
+ core.logger.log(
1685
+ `SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} spans are waiting for their parent spans to finish`,
1686
+ );
1695
1687
 
1696
1688
  sentSpans.forEach(span => {
1697
1689
  const bucketEntry = this._spansToBucketEntry.get(span);
@@ -1779,6 +1771,7 @@ function parseSpan(span) {
1779
1771
  return { origin, op, source };
1780
1772
  }
1781
1773
 
1774
+ /** Exported only for tests. */
1782
1775
  function createTransactionForOtelSpan(span) {
1783
1776
  const { op, description, data, origin = 'manual', source } = getSpanData(span);
1784
1777
  const capturedSpanScopes = core.getCapturedScopesOnSpan(span );
@@ -1796,15 +1789,12 @@ function createTransactionForOtelSpan(span) {
1796
1789
 
1797
1790
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1798
1791
 
1799
- 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)]);
1800
-
1801
1792
  // If parentSpanIdFromTraceState is defined at all, we want it to take precedence
1802
1793
  // In that case, an empty string should be interpreted as "no parent span id",
1803
1794
  // even if `span.parentSpanId` is set
1804
1795
  // this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
1805
1796
  // We only want to continue the traceId in this case, but ignore the parent span
1806
- const parent_span_id =
1807
- typeof parentSpanIdFromTraceState === 'string' ? parentSpanIdFromTraceState || undefined : span.parentSpanId;
1797
+ const parent_span_id = span.parentSpanId;
1808
1798
 
1809
1799
  const status = mapStatus(span);
1810
1800
 
@@ -1818,12 +1808,16 @@ function createTransactionForOtelSpan(span) {
1818
1808
  status: core.getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1819
1809
  });
1820
1810
 
1821
- const transactionEvent = {
1811
+ const statusCode = attributes[semanticConventions.ATTR_HTTP_RESPONSE_STATUS_CODE];
1812
+ const responseContext = typeof statusCode === 'number' ? { response: { status_code: statusCode } } : undefined;
1813
+
1814
+ const transactionEvent = core.dropUndefinedKeys({
1822
1815
  contexts: {
1823
1816
  trace: traceContext,
1824
1817
  otel: {
1825
1818
  resource: span.resource.attributes,
1826
1819
  },
1820
+ ...responseContext,
1827
1821
  },
1828
1822
  spans: [],
1829
1823
  start_timestamp: core.spanTimeInputToSeconds(span.startTime),
@@ -1844,7 +1838,7 @@ function createTransactionForOtelSpan(span) {
1844
1838
  },
1845
1839
  }),
1846
1840
  _metrics_summary: core.getMetricSummaryJsonForSpan(span ),
1847
- };
1841
+ });
1848
1842
 
1849
1843
  return transactionEvent;
1850
1844
  }
@@ -2270,7 +2264,7 @@ exports.SentrySpanProcessor = SentrySpanProcessor;
2270
2264
  exports.addOpenTelemetryInstrumentation = addOpenTelemetryInstrumentation;
2271
2265
  exports.continueTrace = continueTrace;
2272
2266
  exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
2273
- exports.generateSpanContextForPropagationContext = generateSpanContextForPropagationContext$1;
2267
+ exports.generateSpanContextForPropagationContext = generateSpanContextForPropagationContext;
2274
2268
  exports.getActiveSpan = getActiveSpan;
2275
2269
  exports.getPropagationContextFromSpan = getPropagationContextFromSpan;
2276
2270
  exports.getRequestSpanData = getRequestSpanData;