@sentry/opentelemetry 8.41.0 → 8.42.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 (59) hide show
  1. package/build/cjs/index.js +74 -24
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.js +73 -24
  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/custom/client.d.ts +1 -2
  8. package/build/types/custom/client.d.ts.map +1 -1
  9. package/build/types/index.d.ts +1 -1
  10. package/build/types/index.d.ts.map +1 -1
  11. package/build/types/instrumentation.d.ts +3 -0
  12. package/build/types/instrumentation.d.ts.map +1 -1
  13. package/build/types/propagator.d.ts +8 -4
  14. package/build/types/propagator.d.ts.map +1 -1
  15. package/build/types/sampler.d.ts +1 -1
  16. package/build/types/sampler.d.ts.map +1 -1
  17. package/build/types/setupEventContextTrace.d.ts +1 -1
  18. package/build/types/setupEventContextTrace.d.ts.map +1 -1
  19. package/build/types/spanExporter.d.ts.map +1 -1
  20. package/build/types/trace.d.ts +1 -1
  21. package/build/types/trace.d.ts.map +1 -1
  22. package/build/types/types.d.ts +1 -1
  23. package/build/types/types.d.ts.map +1 -1
  24. package/build/types/utils/addOriginToSpan.d.ts +1 -1
  25. package/build/types/utils/addOriginToSpan.d.ts.map +1 -1
  26. package/build/types/utils/contextData.d.ts +1 -1
  27. package/build/types/utils/contextData.d.ts.map +1 -1
  28. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +1 -1
  29. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts.map +1 -1
  30. package/build/types/utils/generateSpanContextForPropagationContext.d.ts +3 -1
  31. package/build/types/utils/generateSpanContextForPropagationContext.d.ts.map +1 -1
  32. package/build/types/utils/getRequestSpanData.d.ts +1 -1
  33. package/build/types/utils/getRequestSpanData.d.ts.map +1 -1
  34. package/build/types/utils/getTraceData.d.ts +1 -1
  35. package/build/types/utils/getTraceData.d.ts.map +1 -1
  36. package/build/types/utils/makeTraceState.d.ts +1 -1
  37. package/build/types/utils/makeTraceState.d.ts.map +1 -1
  38. package/build/types/utils/mapStatus.d.ts +1 -1
  39. package/build/types/utils/mapStatus.d.ts.map +1 -1
  40. package/build/types/utils/parseSpanDescription.d.ts +1 -1
  41. package/build/types/utils/parseSpanDescription.d.ts.map +1 -1
  42. package/build/types-ts3.8/custom/client.d.ts +1 -2
  43. package/build/types-ts3.8/index.d.ts +1 -1
  44. package/build/types-ts3.8/instrumentation.d.ts +3 -0
  45. package/build/types-ts3.8/propagator.d.ts +8 -4
  46. package/build/types-ts3.8/sampler.d.ts +1 -1
  47. package/build/types-ts3.8/setupEventContextTrace.d.ts +1 -1
  48. package/build/types-ts3.8/trace.d.ts +1 -1
  49. package/build/types-ts3.8/types.d.ts +1 -1
  50. package/build/types-ts3.8/utils/addOriginToSpan.d.ts +1 -1
  51. package/build/types-ts3.8/utils/contextData.d.ts +1 -1
  52. package/build/types-ts3.8/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +1 -1
  53. package/build/types-ts3.8/utils/generateSpanContextForPropagationContext.d.ts +3 -1
  54. package/build/types-ts3.8/utils/getRequestSpanData.d.ts +1 -1
  55. package/build/types-ts3.8/utils/getTraceData.d.ts +1 -1
  56. package/build/types-ts3.8/utils/makeTraceState.d.ts +1 -1
  57. package/build/types-ts3.8/utils/mapStatus.d.ts +1 -1
  58. package/build/types-ts3.8/utils/parseSpanDescription.d.ts +1 -1
  59. package/package.json +2 -3
@@ -532,8 +532,10 @@ function makeTraceState({
532
532
  /**
533
533
  * Generates a SpanContext that represents a PropagationContext.
534
534
  * This can be set on a `context` to make this a (virtual) active span.
535
+ *
536
+ * @deprecated This function is deprecated and will be removed in the next major version.
535
537
  */
536
- function generateSpanContextForPropagationContext(propagationContext) {
538
+ function generateSpanContextForPropagationContext$1(propagationContext) {
537
539
  // We store the DSC as OTEL trace state on the span context
538
540
  const traceState = makeTraceState({
539
541
  parentSpanId: propagationContext.parentSpanId,
@@ -543,6 +545,7 @@ function generateSpanContextForPropagationContext(propagationContext) {
543
545
 
544
546
  const spanContext = {
545
547
  traceId: propagationContext.traceId,
548
+ // TODO: Do not create an invalid span context here
546
549
  spanId: propagationContext.parentSpanId || '',
547
550
  isRemote: true,
548
551
  traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
@@ -673,11 +676,7 @@ class SentryPropagator extends W3CBaggagePropagator {
673
676
  const url = activeSpan && getCurrentURL(activeSpan);
674
677
 
675
678
  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
- ) {
679
+ if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
681
680
  DEBUG_BUILD &&
682
681
  logger.log(
683
682
  '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:',
@@ -753,22 +752,36 @@ class SentryPropagator extends W3CBaggagePropagator {
753
752
  fields() {
754
753
  return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER];
755
754
  }
755
+ }
756
756
 
757
- /** If we want to inject trace data for a given URL. */
758
- _shouldInjectTraceData(tracePropagationTargets, url) {
759
- if (tracePropagationTargets === undefined) {
760
- return true;
761
- }
757
+ const NOT_PROPAGATED_MESSAGE =
758
+ '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:';
762
759
 
763
- const cachedDecision = this._urlMatchesTargetsMap.get(url);
764
- if (cachedDecision !== undefined) {
765
- return cachedDecision;
766
- }
760
+ /**
761
+ * Check if a given URL should be propagated to or not.
762
+ * If no url is defined, or no trace propagation targets are defined, this will always return `true`.
763
+ * You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
764
+ */
765
+ function shouldPropagateTraceForUrl(
766
+ url,
767
+ tracePropagationTargets,
768
+ decisionMap,
769
+ ) {
770
+ if (typeof url !== 'string' || !tracePropagationTargets) {
771
+ return true;
772
+ }
767
773
 
768
- const decision = stringMatchesSomePattern(url, tracePropagationTargets);
769
- this._urlMatchesTargetsMap.set(url, decision);
770
- return decision;
774
+ const cachedDecision = _optionalChain$6([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
775
+ if (cachedDecision !== undefined) {
776
+ DEBUG_BUILD && !cachedDecision && logger.log(NOT_PROPAGATED_MESSAGE, url);
777
+ return cachedDecision;
771
778
  }
779
+
780
+ const decision = stringMatchesSomePattern(url, tracePropagationTargets);
781
+ _optionalChain$6([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
782
+
783
+ DEBUG_BUILD && !decision && logger.log(NOT_PROPAGATED_MESSAGE, url);
784
+ return decision;
772
785
  }
773
786
 
774
787
  /**
@@ -778,7 +791,7 @@ function getInjectionData(context)
778
791
 
779
792
  {
780
793
  const span = trace.getSpan(context);
781
- const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
794
+ const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _9 => _9.spanContext, 'call', _10 => _10(), 'access', _11 => _11.isRemote]);
782
795
 
783
796
  // If we have a local span, we can just pick everything from it
784
797
  if (span && !spanIsRemote) {
@@ -794,7 +807,7 @@ function getInjectionData(context)
794
807
  }
795
808
 
796
809
  // 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();
810
+ const scope = _optionalChain$6([getScopesFromContext, 'call', _12 => _12(context), 'optionalAccess', _13 => _13.scope]) || getCurrentScope();
798
811
  const client = getClient();
799
812
 
800
813
  const propagationContext = scope.getPropagationContext();
@@ -802,6 +815,8 @@ function getInjectionData(context)
802
815
  return {
803
816
  dynamicSamplingContext,
804
817
  traceId: propagationContext.traceId,
818
+ // TODO(v9): Use generateSpanId() instead
819
+ // eslint-disable-next-line deprecation/deprecation
805
820
  spanId: propagationContext.spanId,
806
821
  sampled: propagationContext.sampled,
807
822
  };
@@ -854,13 +869,13 @@ function getCurrentURL(span) {
854
869
  const spanData = spanToJSON(span).data;
855
870
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
856
871
  // 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]]);
872
+ const urlAttribute = _optionalChain$6([spanData, 'optionalAccess', _14 => _14[SEMATTRS_HTTP_URL]]) || _optionalChain$6([spanData, 'optionalAccess', _15 => _15[ATTR_URL_FULL]]);
858
873
  if (urlAttribute) {
859
874
  return urlAttribute;
860
875
  }
861
876
 
862
877
  // 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)]);
878
+ 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)]);
864
879
  if (urlTraceState) {
865
880
  return urlTraceState;
866
881
  }
@@ -868,6 +883,26 @@ function getCurrentURL(span) {
868
883
  return undefined;
869
884
  }
870
885
 
886
+ // TODO: Adjust this behavior to avoid invalid spans
887
+ function generateSpanContextForPropagationContext(propagationContext) {
888
+ // We store the DSC as OTEL trace state on the span context
889
+ const traceState = makeTraceState({
890
+ parentSpanId: propagationContext.parentSpanId,
891
+ dsc: propagationContext.dsc,
892
+ sampled: propagationContext.sampled,
893
+ });
894
+
895
+ const spanContext = {
896
+ traceId: propagationContext.traceId,
897
+ spanId: propagationContext.parentSpanId || '',
898
+ isRemote: true,
899
+ traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
900
+ traceState,
901
+ };
902
+
903
+ return spanContext;
904
+ }
905
+
871
906
  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; }
872
907
 
873
908
  /**
@@ -1048,6 +1083,7 @@ function getContext(scope, forceTransaction) {
1048
1083
 
1049
1084
  const spanOptions = {
1050
1085
  traceId: propagationContext.traceId,
1086
+ // eslint-disable-next-line deprecation/deprecation
1051
1087
  spanId: propagationContext.parentSpanId || propagationContext.spanId,
1052
1088
  isRemote: true,
1053
1089
  traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
@@ -1163,6 +1199,7 @@ function suppressTracing(callback) {
1163
1199
  }
1164
1200
 
1165
1201
  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; }
1202
+
1166
1203
  /** Ensure the `trace` context is set on all events. */
1167
1204
  function setupEventContextTrace(client) {
1168
1205
  client.on('preprocessEvent', event => {
@@ -2035,7 +2072,7 @@ class SentrySampler {
2035
2072
  ) {
2036
2073
  const options = this._client.getOptions();
2037
2074
 
2038
- const parentSpan = trace.getSpan(context);
2075
+ const parentSpan = getValidSpan(context);
2039
2076
  const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
2040
2077
 
2041
2078
  if (!hasTracingEnabled(options)) {
@@ -2198,9 +2235,21 @@ function getBaseTraceState(context, spanAttributes) {
2198
2235
  return traceState;
2199
2236
  }
2200
2237
 
2238
+ /**
2239
+ * If the active span is invalid, we want to ignore it as parent.
2240
+ * This aligns with how otel tracers and default samplers handle these cases.
2241
+ */
2242
+ function getValidSpan(context) {
2243
+ const span = trace.getSpan(context);
2244
+ return span && isSpanContextValid(span.spanContext()) ? span : undefined;
2245
+ }
2246
+
2201
2247
  /**
2202
2248
  * This method takes an OpenTelemetry instrumentation or
2203
2249
  * array of instrumentations and registers them with OpenTelemetry.
2250
+ *
2251
+ * @deprecated This method will be removed in the next major version of the SDK.
2252
+ * Use the `openTelemetryInstrumentations` option in `Sentry.init()` or your custom Sentry Client instead.
2204
2253
  */
2205
2254
  function addOpenTelemetryInstrumentation(...instrumentations) {
2206
2255
  registerInstrumentations({
@@ -2208,5 +2257,5 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2208
2257
  });
2209
2258
  }
2210
2259
 
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 };
2260
+ export { SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION, SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, generateSpanContextForPropagationContext$1 as 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
2261
  //# sourceMappingURL=index.js.map