@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
@@ -1,9 +1,9 @@
1
1
  import { _optionalChain } from '@sentry/core';
2
2
  import { ATTR_URL_FULL, SEMATTRS_HTTP_URL, ATTR_HTTP_REQUEST_METHOD, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, ATTR_HTTP_ROUTE, ATTR_HTTP_RESPONSE_STATUS_CODE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
3
- import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, stripUrlQueryAndFragment, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, dynamicSamplingContextToSentryBaggageHeader, baggageHeaderToDynamicSamplingContext, getDynamicSamplingContextFromSpan, getRootSpan, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, propagationContextFromHeaders, getCurrentScope, getIsolationScope, stringMatchesSomePattern, getDynamicSamplingContextFromScope, handleCallbackErrors, continueTrace as continueTrace$1, spanToTraceContext, getTraceContextFromScope, dropUndefinedKeys, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, getStatusMessage, spanTimeInputToSeconds, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, hasTracingEnabled, sampleSpan } from '@sentry/core';
3
+ import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, stripUrlQueryAndFragment, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, hasTracingEnabled, dynamicSamplingContextToSentryBaggageHeader, getDynamicSamplingContextFromSpan, getRootSpan, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, generateSpanId, getCurrentScope, getDynamicSamplingContextFromScope, propagationContextFromHeaders, getIsolationScope, handleCallbackErrors, continueTrace as continueTrace$1, spanToTraceContext, getTraceContextFromScope, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, dropUndefinedKeys, getStatusMessage, spanTimeInputToSeconds, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
4
4
  export { getClient, getCurrentHubShim, getDynamicSamplingContextFromSpan } from '@sentry/core';
5
5
  import * as api from '@opentelemetry/api';
6
- import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, INVALID_SPANID, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
6
+ import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
7
7
  import { TraceState, W3CBaggagePropagator, isTracingSuppressed, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
8
8
  import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
9
9
  import { registerInstrumentations } from '@opentelemetry/instrumentation';
@@ -129,7 +129,7 @@ function getRequestSpanData(span) {
129
129
  return data;
130
130
  }
131
131
 
132
- 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; }
132
+ 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; }
133
133
 
134
134
  // Typescript complains if we do not use `...args: any[]` for the mixin, with:
135
135
  // A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
@@ -170,7 +170,7 @@ function wrapClientClass
170
170
  */
171
171
  async flush(timeout) {
172
172
  const provider = this.traceProvider;
173
- const spanProcessor = _optionalChain$8([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
173
+ const spanProcessor = _optionalChain$6([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
174
174
 
175
175
  if (spanProcessor) {
176
176
  await spanProcessor.forceFlush();
@@ -202,7 +202,6 @@ const SENTRY_TRACE_HEADER = 'sentry-trace';
202
202
  const SENTRY_BAGGAGE_HEADER = 'baggage';
203
203
 
204
204
  const SENTRY_TRACE_STATE_DSC = 'sentry.dsc';
205
- const SENTRY_TRACE_STATE_PARENT_SPAN_ID = 'sentry.parent_span_id';
206
205
  const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = 'sentry.sampled_not_recording';
207
206
  const SENTRY_TRACE_STATE_URL = 'sentry.url';
208
207
 
@@ -271,6 +270,46 @@ function isSentryRequestSpan(span) {
271
270
  return isSentryRequestUrl(httpUrl.toString(), getClient());
272
271
  }
273
272
 
273
+ 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; }
274
+
275
+ /**
276
+ * OpenTelemetry only knows about SAMPLED or NONE decision,
277
+ * but for us it is important to differentiate between unset and unsampled.
278
+ *
279
+ * Both of these are identified as `traceFlags === TracegFlags.NONE`,
280
+ * but we additionally look at a special trace state to differentiate between them.
281
+ */
282
+ function getSamplingDecision(spanContext) {
283
+ const { traceFlags, traceState } = spanContext;
284
+
285
+ const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
286
+
287
+ // If trace flag is `SAMPLED`, we interpret this as sampled
288
+ // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
289
+ // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
290
+ // to identify which it is
291
+ if (traceFlags === TraceFlags.SAMPLED) {
292
+ return true;
293
+ }
294
+
295
+ if (sampledNotRecording) {
296
+ return false;
297
+ }
298
+
299
+ // Fall back to DSC as a last resort, that may also contain `sampled`...
300
+ const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
301
+ const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
302
+
303
+ if (_optionalChain$5([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
304
+ return true;
305
+ }
306
+ if (_optionalChain$5([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
307
+ return false;
308
+ }
309
+
310
+ return undefined;
311
+ }
312
+
274
313
  /**
275
314
  * Infer the op & description for a set of name, attributes and kind of a span.
276
315
  */
@@ -485,20 +524,31 @@ function getSanitizedUrl(
485
524
  */
486
525
  function enhanceDscWithOpenTelemetryRootSpanName(client) {
487
526
  client.on('createDsc', (dsc, rootSpan) => {
527
+ if (!rootSpan) {
528
+ return;
529
+ }
530
+
488
531
  // We want to overwrite the transaction on the DSC that is created by default in core
489
532
  // The reason for this is that we want to infer the span name, not use the initial one
490
533
  // Otherwise, we'll get names like "GET" instead of e.g. "GET /foo"
491
534
  // `parseSpanDescription` takes the attributes of the span into account for the name
492
535
  // This mutates the passed-in DSC
493
- if (rootSpan) {
494
- const jsonSpan = spanToJSON(rootSpan);
495
- const attributes = jsonSpan.data || {};
496
- const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
497
-
498
- const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
499
- if (source !== 'url' && description) {
500
- dsc.transaction = description;
501
- }
536
+
537
+ const jsonSpan = spanToJSON(rootSpan);
538
+ const attributes = jsonSpan.data || {};
539
+ const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
540
+
541
+ const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
542
+ if (source !== 'url' && description) {
543
+ dsc.transaction = description;
544
+ }
545
+
546
+ // Also ensure sampling decision is correctly inferred
547
+ // In core, we use `spanIsSampled`, which just looks at the trace flags
548
+ // but in OTEL, we use a slightly more complex logic to be able to differntiate between unsampled and deferred sampling
549
+ if (hasTracingEnabled()) {
550
+ const sampled = getSamplingDecision(rootSpan.spanContext());
551
+ dsc.sampled = sampled == undefined ? undefined : String(sampled);
502
552
  }
503
553
  });
504
554
  }
@@ -507,7 +557,6 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
507
557
  * Generate a TraceState for the given data.
508
558
  */
509
559
  function makeTraceState({
510
- parentSpanId,
511
560
  dsc,
512
561
  sampled,
513
562
  }
@@ -516,11 +565,7 @@ function makeTraceState({
516
565
  // We store the DSC as OTEL trace state on the span context
517
566
  const dscString = dsc ? dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
518
567
 
519
- // We _always_ set the parent span ID, even if it is empty
520
- // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
521
- // vs the trace state was not set at all (in which case we want to do fallback handling)
522
- // If `''`, it should be considered "no parent"
523
- const traceStateBase = new TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
568
+ const traceStateBase = new TraceState();
524
569
 
525
570
  const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
526
571
 
@@ -532,17 +577,19 @@ function makeTraceState({
532
577
  /**
533
578
  * Generates a SpanContext that represents a PropagationContext.
534
579
  * This can be set on a `context` to make this a (virtual) active span.
580
+ *
581
+ * @deprecated This function is deprecated and will be removed in the next major version.
535
582
  */
536
583
  function generateSpanContextForPropagationContext(propagationContext) {
537
584
  // We store the DSC as OTEL trace state on the span context
538
585
  const traceState = makeTraceState({
539
- parentSpanId: propagationContext.parentSpanId,
540
586
  dsc: propagationContext.dsc,
541
587
  sampled: propagationContext.sampled,
542
588
  });
543
589
 
544
590
  const spanContext = {
545
591
  traceId: propagationContext.traceId,
592
+ // TODO: Do not create an invalid span context here
546
593
  spanId: propagationContext.parentSpanId || '',
547
594
  isRemote: true,
548
595
  traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
@@ -566,46 +613,6 @@ function getActiveSpan() {
566
613
  */
567
614
  const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
568
615
 
569
- 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; }
570
-
571
- /**
572
- * OpenTelemetry only knows about SAMPLED or NONE decision,
573
- * but for us it is important to differentiate between unset and unsampled.
574
- *
575
- * Both of these are identified as `traceFlags === TracegFlags.NONE`,
576
- * but we additionally look at a special trace state to differentiate between them.
577
- */
578
- function getSamplingDecision(spanContext) {
579
- const { traceFlags, traceState } = spanContext;
580
-
581
- const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
582
-
583
- // If trace flag is `SAMPLED`, we interpret this as sampled
584
- // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
585
- // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
586
- // to identify which it is
587
- if (traceFlags === TraceFlags.SAMPLED) {
588
- return true;
589
- }
590
-
591
- if (sampledNotRecording) {
592
- return false;
593
- }
594
-
595
- // Fall back to DSC as a last resort, that may also contain `sampled`...
596
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
597
- const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
598
-
599
- if (_optionalChain$7([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
600
- return true;
601
- }
602
- if (_optionalChain$7([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
603
- return false;
604
- }
605
-
606
- return undefined;
607
- }
608
-
609
616
  const setupElements = new Set();
610
617
 
611
618
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -618,7 +625,7 @@ function setIsSetup(element) {
618
625
  setupElements.add(element);
619
626
  }
620
627
 
621
- 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; }
628
+ 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; }
622
629
 
623
630
  /** Get the Sentry propagation context from a span context. */
624
631
  function getPropagationContextFromSpan(span) {
@@ -630,8 +637,7 @@ function getPropagationContextFromSpan(span) {
630
637
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
631
638
  const traceStateDsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
632
639
 
633
- const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) || undefined : undefined;
634
-
640
+ const parentSpanId = spanHasParentId(span) ? span.parentSpanId : undefined;
635
641
  const sampled = getSamplingDecision(spanContext);
636
642
 
637
643
  // No trace state? --> Take DSC from root span
@@ -672,12 +678,8 @@ class SentryPropagator extends W3CBaggagePropagator {
672
678
  const activeSpan = trace.getSpan(context);
673
679
  const url = activeSpan && getCurrentURL(activeSpan);
674
680
 
675
- const tracePropagationTargets = _optionalChain$6([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
676
- if (
677
- typeof url === 'string' &&
678
- tracePropagationTargets &&
679
- !this._shouldInjectTraceData(tracePropagationTargets, url)
680
- ) {
681
+ const tracePropagationTargets = _optionalChain$4([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
682
+ if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
681
683
  DEBUG_BUILD &&
682
684
  logger.log(
683
685
  '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:',
@@ -731,20 +733,9 @@ class SentryPropagator extends W3CBaggagePropagator {
731
733
  : maybeSentryTraceHeader
732
734
  : undefined;
733
735
 
734
- const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
735
-
736
736
  // Add remote parent span context
737
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(context, { sentryTrace, baggage });
738
-
739
- // Also update the scope on the context (to be sure this is picked up everywhere)
740
- const scopes = getScopesFromContext(ctxWithSpanContext);
741
- const newScopes = {
742
- scope: scopes ? scopes.scope.clone() : getCurrentScope().clone(),
743
- isolationScope: scopes ? scopes.isolationScope : getIsolationScope(),
744
- };
745
- newScopes.scope.setPropagationContext(propagationContext);
746
-
747
- return setScopesOnContext(ctxWithSpanContext, newScopes);
737
+ // If there is no incoming trace, this will return the context as-is
738
+ return ensureScopesOnContext(getContextWithRemoteActiveSpan(context, { sentryTrace, baggage }));
748
739
  }
749
740
 
750
741
  /**
@@ -753,22 +744,36 @@ class SentryPropagator extends W3CBaggagePropagator {
753
744
  fields() {
754
745
  return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER];
755
746
  }
747
+ }
756
748
 
757
- /** If we want to inject trace data for a given URL. */
758
- _shouldInjectTraceData(tracePropagationTargets, url) {
759
- if (tracePropagationTargets === undefined) {
760
- return true;
761
- }
749
+ const NOT_PROPAGATED_MESSAGE =
750
+ '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:';
762
751
 
763
- const cachedDecision = this._urlMatchesTargetsMap.get(url);
764
- if (cachedDecision !== undefined) {
765
- return cachedDecision;
766
- }
752
+ /**
753
+ * Check if a given URL should be propagated to or not.
754
+ * If no url is defined, or no trace propagation targets are defined, this will always return `true`.
755
+ * You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
756
+ */
757
+ function shouldPropagateTraceForUrl(
758
+ url,
759
+ tracePropagationTargets,
760
+ decisionMap,
761
+ ) {
762
+ if (typeof url !== 'string' || !tracePropagationTargets) {
763
+ return true;
764
+ }
767
765
 
768
- const decision = stringMatchesSomePattern(url, tracePropagationTargets);
769
- this._urlMatchesTargetsMap.set(url, decision);
770
- return decision;
766
+ const cachedDecision = _optionalChain$4([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
767
+ if (cachedDecision !== undefined) {
768
+ DEBUG_BUILD && !cachedDecision && logger.log(NOT_PROPAGATED_MESSAGE, url);
769
+ return cachedDecision;
771
770
  }
771
+
772
+ const decision = stringMatchesSomePattern(url, tracePropagationTargets);
773
+ _optionalChain$4([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
774
+
775
+ DEBUG_BUILD && !decision && logger.log(NOT_PROPAGATED_MESSAGE, url);
776
+ return decision;
772
777
  }
773
778
 
774
779
  /**
@@ -778,13 +783,28 @@ function getInjectionData(context)
778
783
 
779
784
  {
780
785
  const span = trace.getSpan(context);
781
- const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
782
786
 
783
- // If we have a local span, we can just pick everything from it
784
- if (span && !spanIsRemote) {
787
+ // If we have a remote span, the spanId should be considered as the parentSpanId, not spanId itself
788
+ // Instead, we use a virtual (generated) spanId for propagation
789
+ if (span && span.spanContext().isRemote) {
785
790
  const spanContext = span.spanContext();
791
+ const dynamicSamplingContext = getDynamicSamplingContextFromSpan(span);
786
792
 
793
+ return {
794
+ dynamicSamplingContext,
795
+ traceId: spanContext.traceId,
796
+ // Because this is a remote span, we do not want to propagate this directly
797
+ // As otherwise things may be attached "directly" to an unrelated span
798
+ spanId: generateSpanId(),
799
+ sampled: getSamplingDecision(spanContext),
800
+ };
801
+ }
802
+
803
+ // If we have a local span, we just use this
804
+ if (span) {
805
+ const spanContext = span.spanContext();
787
806
  const dynamicSamplingContext = getDynamicSamplingContextFromSpan(span);
807
+
788
808
  return {
789
809
  dynamicSamplingContext,
790
810
  traceId: spanContext.traceId,
@@ -794,7 +814,8 @@ function getInjectionData(context)
794
814
  }
795
815
 
796
816
  // Else we try to use the propagation context from the scope
797
- const scope = _optionalChain$6([getScopesFromContext, 'call', _8 => _8(context), 'optionalAccess', _9 => _9.scope]) || getCurrentScope();
817
+ // The only scenario where this should happen is when we neither have a span, nor an incoming trace
818
+ const scope = _optionalChain$4([getScopesFromContext, 'call', _9 => _9(context), 'optionalAccess', _10 => _10.scope]) || getCurrentScope();
798
819
  const client = getClient();
799
820
 
800
821
  const propagationContext = scope.getPropagationContext();
@@ -802,6 +823,8 @@ function getInjectionData(context)
802
823
  return {
803
824
  dynamicSamplingContext,
804
825
  traceId: propagationContext.traceId,
826
+ // TODO(v9): Use generateSpanId() instead
827
+ // eslint-disable-next-line deprecation/deprecation
805
828
  spanId: propagationContext.spanId,
806
829
  sampled: propagationContext.sampled,
807
830
  };
@@ -813,7 +836,21 @@ function getContextWithRemoteActiveSpan(
813
836
  ) {
814
837
  const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
815
838
 
816
- const spanContext = generateSpanContextForPropagationContext(propagationContext);
839
+ const { traceId, parentSpanId, sampled, dsc } = propagationContext;
840
+
841
+ // We only want to set the virtual span if we are continuing a concrete trace
842
+ // Otherwise, we ignore the incoming trace here, e.g. if we have no trace headers
843
+ if (!parentSpanId) {
844
+ return ctx;
845
+ }
846
+
847
+ const spanContext = generateRemoteSpanContext({
848
+ traceId,
849
+ spanId: parentSpanId,
850
+ sampled,
851
+ dsc,
852
+ });
853
+
817
854
  return trace.setSpanContext(ctx, spanContext);
818
855
  }
819
856
 
@@ -826,11 +863,24 @@ function continueTraceAsRemoteSpan(
826
863
  options,
827
864
  callback,
828
865
  ) {
829
- const ctxWithSpanContext = getContextWithRemoteActiveSpan(ctx, options);
866
+ const ctxWithSpanContext = ensureScopesOnContext(getContextWithRemoteActiveSpan(ctx, options));
830
867
 
831
868
  return context.with(ctxWithSpanContext, callback);
832
869
  }
833
870
 
871
+ function ensureScopesOnContext(ctx) {
872
+ // If there are no scopes yet on the context, ensure we have them
873
+ const scopes = getScopesFromContext(ctx);
874
+ const newScopes = {
875
+ // If we have no scope here, this is most likely either the root context or a context manually derived from it
876
+ // In this case, we want to fork the current scope, to ensure we do not pollute the root scope
877
+ scope: scopes ? scopes.scope : getCurrentScope().clone(),
878
+ isolationScope: scopes ? scopes.isolationScope : getIsolationScope(),
879
+ };
880
+
881
+ return setScopesOnContext(ctx, newScopes);
882
+ }
883
+
834
884
  /** Try to get the existing baggage header so we can merge this in. */
835
885
  function getExistingBaggage(carrier) {
836
886
  try {
@@ -854,13 +904,13 @@ function getCurrentURL(span) {
854
904
  const spanData = spanToJSON(span).data;
855
905
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
856
906
  // eslint-disable-next-line deprecation/deprecation
857
- const urlAttribute = _optionalChain$6([spanData, 'optionalAccess', _10 => _10[SEMATTRS_HTTP_URL]]) || _optionalChain$6([spanData, 'optionalAccess', _11 => _11[ATTR_URL_FULL]]);
907
+ const urlAttribute = _optionalChain$4([spanData, 'optionalAccess', _11 => _11[SEMATTRS_HTTP_URL]]) || _optionalChain$4([spanData, 'optionalAccess', _12 => _12[ATTR_URL_FULL]]);
858
908
  if (urlAttribute) {
859
909
  return urlAttribute;
860
910
  }
861
911
 
862
912
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
863
- 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)]);
913
+ 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)]);
864
914
  if (urlTraceState) {
865
915
  return urlTraceState;
866
916
  }
@@ -868,7 +918,30 @@ function getCurrentURL(span) {
868
918
  return undefined;
869
919
  }
870
920
 
871
- 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; }
921
+ function generateRemoteSpanContext({
922
+ spanId,
923
+ traceId,
924
+ sampled,
925
+ dsc,
926
+ }
927
+
928
+ ) {
929
+ // We store the DSC as OTEL trace state on the span context
930
+ const traceState = makeTraceState({
931
+ dsc,
932
+ sampled,
933
+ });
934
+
935
+ const spanContext = {
936
+ traceId,
937
+ spanId,
938
+ isRemote: true,
939
+ traceFlags: sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
940
+ traceState,
941
+ };
942
+
943
+ return spanContext;
944
+ }
872
945
 
873
946
  /**
874
947
  * Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -1026,39 +1099,11 @@ function ensureTimestampInMilliseconds(timestamp) {
1026
1099
 
1027
1100
  function getContext(scope, forceTransaction) {
1028
1101
  const ctx = getContextForScope(scope);
1029
- // Note: If the context is the ROOT_CONTEXT, no scope is attached
1030
- // Thus we will not use the propagation context in this case, which is desired
1031
- const actualScope = _optionalChain$5([getScopesFromContext, 'call', _ => _(ctx), 'optionalAccess', _2 => _2.scope]);
1032
1102
  const parentSpan = trace.getSpan(ctx);
1033
1103
 
1034
- // In the case that we have no parent span, we need to "simulate" one to ensure the propagation context is correct
1104
+ // In the case that we have no parent span, we start a new trace
1105
+ // Note that if we continue a trace, we'll always have a remote parent span here anyhow
1035
1106
  if (!parentSpan) {
1036
- const client = getClient();
1037
-
1038
- if (actualScope && client) {
1039
- const propagationContext = actualScope.getPropagationContext();
1040
-
1041
- // We store the DSC as OTEL trace state on the span context
1042
- const traceState = makeTraceState({
1043
- parentSpanId: propagationContext.parentSpanId,
1044
- // Not defined yet, we want to pick this up on-demand only
1045
- dsc: undefined,
1046
- sampled: propagationContext.sampled,
1047
- });
1048
-
1049
- const spanOptions = {
1050
- traceId: propagationContext.traceId,
1051
- spanId: propagationContext.parentSpanId || propagationContext.spanId,
1052
- isRemote: true,
1053
- traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
1054
- traceState,
1055
- };
1056
-
1057
- // Add remote parent span context,
1058
- return trace.setSpanContext(ctx, spanOptions);
1059
- }
1060
-
1061
- // if we have no scope or client, we just return the context as-is
1062
1107
  return ctx;
1063
1108
  }
1064
1109
 
@@ -1084,7 +1129,6 @@ function getContext(scope, forceTransaction) {
1084
1129
 
1085
1130
  const traceState = makeTraceState({
1086
1131
  dsc,
1087
- parentSpanId: spanId !== INVALID_SPANID ? spanId : undefined,
1088
1132
  sampled,
1089
1133
  });
1090
1134
 
@@ -1162,7 +1206,6 @@ function suppressTracing(callback) {
1162
1206
  return context.with(ctx, callback);
1163
1207
  }
1164
1208
 
1165
- 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; }
1166
1209
  /** Ensure the `trace` context is set on all events. */
1167
1210
  function setupEventContextTrace(client) {
1168
1211
  client.on('preprocessEvent', event => {
@@ -1173,25 +1216,9 @@ function setupEventContextTrace(client) {
1173
1216
  return;
1174
1217
  }
1175
1218
 
1176
- const spanContext = span.spanContext();
1177
-
1178
- // If we have a parent span id from trace state, use that ('' means no parent should be used)
1179
- // Else, pick the one from the span
1180
- const parentSpanIdFromTraceState = _optionalChain$4([spanContext, 'access', _ => _.traceState, 'optionalAccess', _2 => _2.get, 'call', _3 => _3(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1181
- const parent_span_id =
1182
- typeof parentSpanIdFromTraceState === 'string'
1183
- ? parentSpanIdFromTraceState || undefined
1184
- : spanHasParentId(span)
1185
- ? span.parentSpanId
1186
- : undefined;
1187
-
1188
1219
  // If event has already set `trace` context, use that one.
1189
1220
  event.contexts = {
1190
- trace: dropUndefinedKeys({
1191
- trace_id: spanContext.traceId,
1192
- span_id: spanContext.spanId,
1193
- parent_span_id,
1194
- }),
1221
+ trace: spanToTraceContext(span),
1195
1222
  ...event.contexts,
1196
1223
  };
1197
1224
 
@@ -1756,15 +1783,12 @@ function createTransactionForOtelSpan(span) {
1756
1783
 
1757
1784
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1758
1785
 
1759
- 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)]);
1760
-
1761
1786
  // If parentSpanIdFromTraceState is defined at all, we want it to take precedence
1762
1787
  // In that case, an empty string should be interpreted as "no parent span id",
1763
1788
  // even if `span.parentSpanId` is set
1764
1789
  // this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
1765
1790
  // We only want to continue the traceId in this case, but ignore the parent span
1766
- const parent_span_id =
1767
- typeof parentSpanIdFromTraceState === 'string' ? parentSpanIdFromTraceState || undefined : span.parentSpanId;
1791
+ const parent_span_id = span.parentSpanId;
1768
1792
 
1769
1793
  const status = mapStatus(span);
1770
1794
 
@@ -2035,7 +2059,7 @@ class SentrySampler {
2035
2059
  ) {
2036
2060
  const options = this._client.getOptions();
2037
2061
 
2038
- const parentSpan = trace.getSpan(context);
2062
+ const parentSpan = getValidSpan(context);
2039
2063
  const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
2040
2064
 
2041
2065
  if (!hasTracingEnabled(options)) {
@@ -2198,9 +2222,21 @@ function getBaseTraceState(context, spanAttributes) {
2198
2222
  return traceState;
2199
2223
  }
2200
2224
 
2225
+ /**
2226
+ * If the active span is invalid, we want to ignore it as parent.
2227
+ * This aligns with how otel tracers and default samplers handle these cases.
2228
+ */
2229
+ function getValidSpan(context) {
2230
+ const span = trace.getSpan(context);
2231
+ return span && isSpanContextValid(span.spanContext()) ? span : undefined;
2232
+ }
2233
+
2201
2234
  /**
2202
2235
  * This method takes an OpenTelemetry instrumentation or
2203
2236
  * array of instrumentations and registers them with OpenTelemetry.
2237
+ *
2238
+ * @deprecated This method will be removed in the next major version of the SDK.
2239
+ * Use the `openTelemetryInstrumentations` option in `Sentry.init()` or your custom Sentry Client instead.
2204
2240
  */
2205
2241
  function addOpenTelemetryInstrumentation(...instrumentations) {
2206
2242
  registerInstrumentations({
@@ -2208,5 +2244,5 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2208
2244
  });
2209
2245
  }
2210
2246
 
2211
- export { SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION, SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, generateSpanContextForPropagationContext, getActiveSpan, getPropagationContextFromSpan, getRequestSpanData, getScopesFromContext, getSpanKind, getTraceContextForScope, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass, wrapSamplingDecision };
2247
+ export { SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION, SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, generateSpanContextForPropagationContext, getActiveSpan, getPropagationContextFromSpan, getRequestSpanData, getScopesFromContext, getSpanKind, getTraceContextForScope, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, shouldPropagateTraceForUrl, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass, wrapSamplingDecision };
2212
2248
  //# sourceMappingURL=index.js.map