@sentry/opentelemetry 8.6.0 → 8.8.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.
package/cjs/index.js CHANGED
@@ -120,7 +120,7 @@ function getRequestSpanData(span) {
120
120
  return data;
121
121
  }
122
122
 
123
- 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; }
123
+ function _optionalChain$9(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; }
124
124
 
125
125
  // Typescript complains if we do not use `...args: any[]` for the mixin, with:
126
126
  // A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
@@ -161,7 +161,7 @@ function wrapClientClass
161
161
  */
162
162
  async flush(timeout) {
163
163
  const provider = this.traceProvider;
164
- const spanProcessor = _optionalChain$7([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
164
+ const spanProcessor = _optionalChain$9([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
165
165
 
166
166
  if (spanProcessor) {
167
167
  await spanProcessor.forceFlush();
@@ -239,7 +239,7 @@ function getContextFromScope(scope) {
239
239
  return (scope )[SCOPE_CONTEXT_FIELD];
240
240
  }
241
241
 
242
- 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; }
242
+ 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; }
243
243
 
244
244
  /**
245
245
  * OpenTelemetry only knows about SAMPLED or NONE decision,
@@ -269,10 +269,10 @@ function getSamplingDecision(spanContext) {
269
269
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
270
270
  const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
271
271
 
272
- if (_optionalChain$6([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
272
+ if (_optionalChain$8([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
273
273
  return true;
274
274
  }
275
- if (_optionalChain$6([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
275
+ if (_optionalChain$8([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
276
276
  return false;
277
277
  }
278
278
 
@@ -297,9 +297,14 @@ function parseSpanDescription(span) {
297
297
  return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
298
298
  }
299
299
 
300
- // If db.type exists then this is a database call span.
301
300
  const dbSystem = attributes[semanticConventions.SEMATTRS_DB_SYSTEM];
302
- if (dbSystem) {
301
+ const opIsCache =
302
+ typeof attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'string' &&
303
+ attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP].startsWith('cache.');
304
+
305
+ // If db.type exists then this is a database call span
306
+ // If the Redis DB is used as a cache, the span description should not be changed
307
+ if (dbSystem && !opIsCache) {
303
308
  return descriptionForDbSystem({ attributes, name });
304
309
  }
305
310
 
@@ -428,7 +433,7 @@ function getSanitizedUrl(
428
433
  return { urlPath: undefined, url, query, fragment, hasRoute: false };
429
434
  }
430
435
 
431
- 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; }
436
+ 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; }
432
437
  /**
433
438
  * Creates a dynamic sampling context from a span (and client and scope)
434
439
  *
@@ -443,7 +448,7 @@ function getDynamicSamplingContextFromSpan(span) {
443
448
  }
444
449
 
445
450
  const traceState = span.spanContext().traceState;
446
- const traceStateDsc = _optionalChain$5([traceState, 'optionalAccess', _ => _.get, 'call', _2 => _2(SENTRY_TRACE_STATE_DSC)]);
451
+ const traceStateDsc = _optionalChain$7([traceState, 'optionalAccess', _ => _.get, 'call', _2 => _2(SENTRY_TRACE_STATE_DSC)]);
447
452
 
448
453
  // If the span has a DSC, we want it to take precedence
449
454
  const dscOnSpan = traceStateDsc ? utils.baggageHeaderToDynamicSamplingContext(traceStateDsc) : undefined;
@@ -528,7 +533,7 @@ function setIsSetup(element) {
528
533
  setupElements.add(element);
529
534
  }
530
535
 
531
- 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; }
536
+ 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; }
532
537
 
533
538
  /** Get the Sentry propagation context from a span context. */
534
539
  function getPropagationContextFromSpan(span) {
@@ -540,7 +545,7 @@ function getPropagationContextFromSpan(span) {
540
545
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
541
546
  const traceStateDsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
542
547
 
543
- const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) : undefined;
548
+ const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) || undefined : undefined;
544
549
 
545
550
  const sampled = getSamplingDecision(spanContext);
546
551
 
@@ -582,7 +587,7 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
582
587
  const activeSpan = api.trace.getSpan(context);
583
588
  const url = activeSpan && getCurrentURL(activeSpan);
584
589
 
585
- const tracePropagationTargets = _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
590
+ const tracePropagationTargets = _optionalChain$6([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
586
591
  if (
587
592
  typeof url === 'string' &&
588
593
  tracePropagationTargets &&
@@ -621,7 +626,7 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
621
626
  }
622
627
 
623
628
  // We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason
624
- if (traceId && traceId !== '00000000000000000000000000000000') {
629
+ if (traceId && traceId !== api.INVALID_TRACEID) {
625
630
  setter.set(carrier, SENTRY_TRACE_HEADER, utils.generateSentryTraceHeader(traceId, spanId, sampled));
626
631
  }
627
632
 
@@ -689,16 +694,14 @@ function makeTraceState({
689
694
  }
690
695
 
691
696
  ) {
692
- if (!parentSpanId && !dsc && sampled !== false) {
693
- return undefined;
694
- }
695
-
696
697
  // We store the DSC as OTEL trace state on the span context
697
698
  const dscString = dsc ? utils.dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
698
699
 
699
- const traceStateBase = parentSpanId
700
- ? new core$1.TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId)
701
- : new core$1.TraceState();
700
+ // We _always_ set the parent span ID, even if it is empty
701
+ // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
702
+ // vs the trace state was not set at all (in which case we want to do fallback handling)
703
+ // If `''`, it should be considered "no parent"
704
+ const traceStateBase = new core$1.TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
702
705
 
703
706
  const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
704
707
 
@@ -711,7 +714,7 @@ function getInjectionData(context)
711
714
 
712
715
  {
713
716
  const span = core.hasTracingEnabled() ? api.trace.getSpan(context) : undefined;
714
- const spanIsRemote = _optionalChain$4([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
717
+ const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
715
718
 
716
719
  // If we have a local span, we can just pick everything from it
717
720
  if (span && !spanIsRemote) {
@@ -728,7 +731,7 @@ function getInjectionData(context)
728
731
  }
729
732
 
730
733
  // Else we try to use the propagation context from the scope
731
- const scope = _optionalChain$4([getScopesFromContext, 'call', _8 => _8(context), 'optionalAccess', _9 => _9.scope]) || core.getCurrentScope();
734
+ const scope = _optionalChain$6([getScopesFromContext, 'call', _8 => _8(context), 'optionalAccess', _9 => _9.scope]) || core.getCurrentScope();
732
735
 
733
736
  const propagationContext = scope.getPropagationContext();
734
737
  const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, propagationContext.traceId);
@@ -746,7 +749,7 @@ function getDynamicSamplingContext(
746
749
  traceId,
747
750
  ) {
748
751
  // If we have a DSC on the propagation context, we just use it
749
- if (_optionalChain$4([propagationContext, 'optionalAccess', _10 => _10.dsc])) {
752
+ if (_optionalChain$6([propagationContext, 'optionalAccess', _10 => _10.dsc])) {
750
753
  return propagationContext.dsc;
751
754
  }
752
755
 
@@ -818,13 +821,13 @@ function getExistingBaggage(carrier) {
818
821
  * 2. Else, if the active span has no URL attribute (e.g. it is unsampled), we check a special trace state (which we set in our sampler).
819
822
  */
820
823
  function getCurrentURL(span) {
821
- const urlAttribute = _optionalChain$4([core.spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[semanticConventions.SEMATTRS_HTTP_URL]]);
824
+ const urlAttribute = _optionalChain$6([core.spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[semanticConventions.SEMATTRS_HTTP_URL]]);
822
825
  if (urlAttribute) {
823
826
  return urlAttribute;
824
827
  }
825
828
 
826
829
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
827
- const urlTraceState = _optionalChain$4([span, 'access', _14 => _14.spanContext, 'call', _15 => _15(), 'access', _16 => _16.traceState, 'optionalAccess', _17 => _17.get, 'call', _18 => _18(SENTRY_TRACE_STATE_URL)]);
830
+ const urlTraceState = _optionalChain$6([span, 'access', _14 => _14.spanContext, 'call', _15 => _15(), 'access', _16 => _16.traceState, 'optionalAccess', _17 => _17.get, 'call', _18 => _18(SENTRY_TRACE_STATE_URL)]);
828
831
  if (urlTraceState) {
829
832
  return urlTraceState;
830
833
  }
@@ -832,7 +835,7 @@ function getCurrentURL(span) {
832
835
  return undefined;
833
836
  }
834
837
 
835
- function _optionalChain$3(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; }
838
+ 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; }
836
839
 
837
840
  /**
838
841
  * Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -984,7 +987,7 @@ function ensureTimestampInMilliseconds(timestamp) {
984
987
 
985
988
  function getContext(scope, forceTransaction) {
986
989
  const ctx = getContextForScope(scope);
987
- const actualScope = _optionalChain$3([getScopesFromContext, 'call', _ => _(ctx), 'optionalAccess', _2 => _2.scope]);
990
+ const actualScope = _optionalChain$5([getScopesFromContext, 'call', _ => _(ctx), 'optionalAccess', _2 => _2.scope]);
988
991
 
989
992
  const parentSpan = api.trace.getSpan(ctx);
990
993
 
@@ -1041,7 +1044,7 @@ function getContext(scope, forceTransaction) {
1041
1044
 
1042
1045
  const traceState = makeTraceState({
1043
1046
  dsc,
1044
- parentSpanId: spanId,
1047
+ parentSpanId: spanId !== api.INVALID_SPANID ? spanId : undefined,
1045
1048
  sampled,
1046
1049
  });
1047
1050
 
@@ -1092,6 +1095,8 @@ function suppressTracing(callback) {
1092
1095
  return api.context.with(ctx, callback);
1093
1096
  }
1094
1097
 
1098
+ 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; }
1099
+
1095
1100
  /** Ensure the `trace` context is set on all events. */
1096
1101
  function setupEventContextTrace(client) {
1097
1102
  client.on('preprocessEvent', event => {
@@ -1104,12 +1109,22 @@ function setupEventContextTrace(client) {
1104
1109
 
1105
1110
  const spanContext = span.spanContext();
1106
1111
 
1112
+ // If we have a parent span id from trace state, use that ('' means no parent should be used)
1113
+ // Else, pick the one from the span
1114
+ const parentSpanIdFromTraceState = _optionalChain$4([spanContext, 'access', _ => _.traceState, 'optionalAccess', _2 => _2.get, 'call', _3 => _3(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1115
+ const parent_span_id =
1116
+ typeof parentSpanIdFromTraceState === 'string'
1117
+ ? parentSpanIdFromTraceState || undefined
1118
+ : spanHasParentId(span)
1119
+ ? span.parentSpanId
1120
+ : undefined;
1121
+
1107
1122
  // If event has already set `trace` context, use that one.
1108
1123
  event.contexts = {
1109
1124
  trace: utils.dropUndefinedKeys({
1110
1125
  trace_id: spanContext.traceId,
1111
1126
  span_id: spanContext.spanId,
1112
- parent_span_id: spanHasParentId(span) ? span.parentSpanId : undefined,
1127
+ parent_span_id,
1113
1128
  }),
1114
1129
  ...event.contexts,
1115
1130
  };
@@ -1220,7 +1235,7 @@ function setOpenTelemetryContextAsyncContextStrategy() {
1220
1235
  });
1221
1236
  }
1222
1237
 
1223
- function _optionalChain$2(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; }
1238
+ function _optionalChain$3(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; }
1224
1239
 
1225
1240
  /**
1226
1241
  * Wrap an OpenTelemetry ContextManager in a way that ensures the context is kept in sync with the Sentry Scope.
@@ -1258,8 +1273,8 @@ function wrapContextManagerClass(
1258
1273
  ...args
1259
1274
  ) {
1260
1275
  const currentScopes = getScopesFromContext(context);
1261
- const currentScope = _optionalChain$2([currentScopes, 'optionalAccess', _ => _.scope]) || core.getCurrentScope();
1262
- const currentIsolationScope = _optionalChain$2([currentScopes, 'optionalAccess', _2 => _2.isolationScope]) || core.getIsolationScope();
1276
+ const currentScope = _optionalChain$3([currentScopes, 'optionalAccess', _ => _.scope]) || core.getCurrentScope();
1277
+ const currentIsolationScope = _optionalChain$3([currentScopes, 'optionalAccess', _2 => _2.isolationScope]) || core.getIsolationScope();
1263
1278
 
1264
1279
  const shouldForkIsolationScope = context.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
1265
1280
  const scope = context.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) ;
@@ -1440,6 +1455,8 @@ function mapStatus(span) {
1440
1455
  }
1441
1456
  }
1442
1457
 
1458
+ function _optionalChain$2(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; }
1459
+
1443
1460
  const MAX_SPAN_COUNT = 1000;
1444
1461
 
1445
1462
  /**
@@ -1608,7 +1625,16 @@ function createTransactionForOtelSpan(span) {
1608
1625
  });
1609
1626
 
1610
1627
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1611
- const parent_span_id = span.parentSpanId;
1628
+
1629
+ const parentSpanIdFromTraceState = _optionalChain$2([span, 'access', _ => _.spanContext, 'call', _2 => _2(), 'access', _3 => _3.traceState, 'optionalAccess', _4 => _4.get, 'call', _5 => _5(SENTRY_TRACE_STATE_PARENT_SPAN_ID)]);
1630
+
1631
+ // If parentSpanIdFromTraceState is defined at all, we want it to take presedence
1632
+ // In that case, an empty string should be interpreted as "no parent span id",
1633
+ // even if `span.parentSpanId` is set
1634
+ // this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
1635
+ // We only want to continue the traceId in this case, but ignore the parent span
1636
+ const parent_span_id =
1637
+ typeof parentSpanIdFromTraceState === 'string' ? parentSpanIdFromTraceState || undefined : span.parentSpanId;
1612
1638
 
1613
1639
  const status = mapStatus(span);
1614
1640