@sentry/opentelemetry 8.53.0 → 9.0.0-alpha.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 (31) hide show
  1. package/build/cjs/index.js +135 -178
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.js +137 -173
  4. package/build/esm/index.js.map +1 -1
  5. package/build/esm/package.json +1 -1
  6. package/build/types/constants.d.ts +2 -0
  7. package/build/types/constants.d.ts.map +1 -1
  8. package/build/types/custom/client.d.ts +4 -3
  9. package/build/types/custom/client.d.ts.map +1 -1
  10. package/build/types/index.d.ts +1 -4
  11. package/build/types/index.d.ts.map +1 -1
  12. package/build/types/propagator.d.ts +2 -4
  13. package/build/types/propagator.d.ts.map +1 -1
  14. package/build/types/sampler.d.ts +3 -1
  15. package/build/types/sampler.d.ts.map +1 -1
  16. package/build/types/spanExporter.d.ts.map +1 -1
  17. package/build/types/utils/contextData.d.ts +1 -2
  18. package/build/types/utils/contextData.d.ts.map +1 -1
  19. package/build/types-ts3.8/constants.d.ts +2 -0
  20. package/build/types-ts3.8/custom/client.d.ts +4 -3
  21. package/build/types-ts3.8/index.d.ts +1 -4
  22. package/build/types-ts3.8/propagator.d.ts +2 -4
  23. package/build/types-ts3.8/sampler.d.ts +3 -1
  24. package/build/types-ts3.8/utils/contextData.d.ts +1 -2
  25. package/package.json +12 -10
  26. package/build/types/instrumentation.d.ts +0 -10
  27. package/build/types/instrumentation.d.ts.map +0 -1
  28. package/build/types/utils/generateSpanContextForPropagationContext.d.ts +0 -10
  29. package/build/types/utils/generateSpanContextForPropagationContext.d.ts.map +0 -1
  30. package/build/types-ts3.8/instrumentation.d.ts +0 -10
  31. package/build/types-ts3.8/utils/generateSpanContextForPropagationContext.d.ts +0 -10
@@ -1,15 +1,10 @@
1
- var {
2
- _optionalChain
3
- } = require('@sentry/core');
4
-
5
- Object.defineProperty(exports, '__esModule', { value: true });
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
6
2
 
7
3
  const semanticConventions = require('@opentelemetry/semantic-conventions');
8
4
  const core = require('@sentry/core');
9
5
  const api = require('@opentelemetry/api');
10
6
  const core$1 = require('@opentelemetry/core');
11
7
  const sdkTraceBase = require('@opentelemetry/sdk-trace-base');
12
- const instrumentation = require('@opentelemetry/instrumentation');
13
8
 
14
9
  /** If this attribute is true, it means that the parent is a remote span. */
15
10
  const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
@@ -125,21 +120,20 @@ function getRequestSpanData(span) {
125
120
  data['http.fragment'] = url.hash;
126
121
  }
127
122
  }
128
- } catch (e) {
123
+ } catch {
129
124
  // ignore
130
125
  }
131
126
 
132
127
  return data;
133
128
  }
134
129
 
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
-
137
130
  // Typescript complains if we do not use `...args: any[]` for the mixin, with:
138
131
  // A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
139
132
  /* eslint-disable @typescript-eslint/no-explicit-any */
140
133
 
141
134
  /**
142
- * Wrap an Client with things we need for OpenTelemetry support.
135
+ * Wrap an Client class with things we need for OpenTelemetry support.
136
+ * Make sure that the Client class passed in is non-abstract!
143
137
  *
144
138
  * Usage:
145
139
  * const OpenTelemetryClient = getWrappedClientClass(NodeClient);
@@ -148,6 +142,7 @@ function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[
148
142
  function wrapClientClass
149
143
 
150
144
  (ClientClass) {
145
+ // @ts-expect-error We just assume that this is non-abstract, if you pass in an abstract class this would make it non-abstract
151
146
  class OpenTelemetryClient extends ClientClass {
152
147
 
153
148
  constructor(...args) {
@@ -173,7 +168,7 @@ function wrapClientClass
173
168
  */
174
169
  async flush(timeout) {
175
170
  const provider = this.traceProvider;
176
- const spanProcessor = _optionalChain$6([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
171
+ const spanProcessor = provider?.activeSpanProcessor;
177
172
 
178
173
  if (spanProcessor) {
179
174
  await spanProcessor.forceFlush();
@@ -207,6 +202,8 @@ const SENTRY_BAGGAGE_HEADER = 'baggage';
207
202
  const SENTRY_TRACE_STATE_DSC = 'sentry.dsc';
208
203
  const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = 'sentry.sampled_not_recording';
209
204
  const SENTRY_TRACE_STATE_URL = 'sentry.url';
205
+ const SENTRY_TRACE_STATE_SAMPLE_RAND = 'sentry.sample_rand';
206
+ const SENTRY_TRACE_STATE_SAMPLE_RATE = 'sentry.sample_rate';
210
207
 
211
208
  const SENTRY_SCOPES_CONTEXT_KEY = api.createContextKey('sentry_scopes');
212
209
 
@@ -244,8 +241,7 @@ function setContextOnScope(scope, context) {
244
241
 
245
242
  /**
246
243
  * Get the context related to a scope.
247
- * TODO v8: Use this for the `trace` functions.
248
- * */
244
+ */
249
245
  function getContextFromScope(scope) {
250
246
  return (scope )[SCOPE_CONTEXT_FIELD];
251
247
  }
@@ -273,8 +269,6 @@ function isSentryRequestSpan(span) {
273
269
  return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
274
270
  }
275
271
 
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
272
  /**
279
273
  * OpenTelemetry only knows about SAMPLED or NONE decision,
280
274
  * but for us it is important to differentiate between unset and unsampled.
@@ -303,10 +297,10 @@ function getSamplingDecision(spanContext) {
303
297
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
304
298
  const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
305
299
 
306
- if (_optionalChain$5([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
300
+ if (dsc?.sampled === 'true') {
307
301
  return true;
308
302
  }
309
- if (_optionalChain$5([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
303
+ if (dsc?.sampled === 'false') {
310
304
  return false;
311
305
  }
312
306
 
@@ -529,8 +523,8 @@ function getSanitizedUrl(
529
523
 
530
524
  const parsedUrl = typeof httpUrl === 'string' ? core.parseUrl(httpUrl) : undefined;
531
525
  const url = parsedUrl ? core.getSanitizedUrlString(parsedUrl) : undefined;
532
- const query = parsedUrl && parsedUrl.search ? parsedUrl.search : undefined;
533
- const fragment = parsedUrl && parsedUrl.hash ? parsedUrl.hash : undefined;
526
+ const query = parsedUrl?.search || undefined;
527
+ const fragment = parsedUrl?.hash || undefined;
534
528
 
535
529
  if (typeof httpRoute === 'string') {
536
530
  return { urlPath: httpRoute, url, query, fragment, hasRoute: true };
@@ -601,7 +595,7 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
601
595
  // This mutates the passed-in DSC
602
596
 
603
597
  const jsonSpan = core.spanToJSON(rootSpan);
604
- const attributes = jsonSpan.data || {};
598
+ const attributes = jsonSpan.data;
605
599
  const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
606
600
 
607
601
  const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
@@ -619,6 +613,20 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
619
613
  });
620
614
  }
621
615
 
616
+ /**
617
+ * Returns the currently active span.
618
+ */
619
+ function getActiveSpan() {
620
+ return api.trace.getActiveSpan();
621
+ }
622
+
623
+ /**
624
+ * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
625
+ *
626
+ * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
627
+ */
628
+ const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
629
+
622
630
  /**
623
631
  * Generate a TraceState for the given data.
624
632
  */
@@ -640,45 +648,6 @@ function makeTraceState({
640
648
  return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') : traceStateWithDsc;
641
649
  }
642
650
 
643
- /**
644
- * Generates a SpanContext that represents a PropagationContext.
645
- * This can be set on a `context` to make this a (virtual) active span.
646
- *
647
- * @deprecated This function is deprecated and will be removed in the next major version.
648
- */
649
- function generateSpanContextForPropagationContext(propagationContext) {
650
- // We store the DSC as OTEL trace state on the span context
651
- const traceState = makeTraceState({
652
- dsc: propagationContext.dsc,
653
- sampled: propagationContext.sampled,
654
- });
655
-
656
- const spanContext = {
657
- traceId: propagationContext.traceId,
658
- // TODO: Do not create an invalid span context here
659
- spanId: propagationContext.parentSpanId || '',
660
- isRemote: true,
661
- traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
662
- traceState,
663
- };
664
-
665
- return spanContext;
666
- }
667
-
668
- /**
669
- * Returns the currently active span.
670
- */
671
- function getActiveSpan() {
672
- return api.trace.getActiveSpan();
673
- }
674
-
675
- /**
676
- * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
677
- *
678
- * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
679
- */
680
- const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
681
-
682
651
  const setupElements = new Set();
683
652
 
684
653
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -691,33 +660,6 @@ function setIsSetup(element) {
691
660
  setupElements.add(element);
692
661
  }
693
662
 
694
- 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; }
695
-
696
- /** Get the Sentry propagation context from a span context. */
697
- function getPropagationContextFromSpan(span) {
698
- const spanContext = span.spanContext();
699
- const { traceId, spanId, traceState } = spanContext;
700
-
701
- // When we have a dsc trace state, it means this came from the incoming trace
702
- // Then this takes presedence over the root span
703
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
704
- const traceStateDsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
705
-
706
- const parentSpanId = spanHasParentId(span) ? span.parentSpanId : undefined;
707
- const sampled = getSamplingDecision(spanContext);
708
-
709
- // No trace state? --> Take DSC from root span
710
- const dsc = traceStateDsc || core.getDynamicSamplingContextFromSpan(core.getRootSpan(span));
711
-
712
- return {
713
- traceId,
714
- spanId,
715
- sampled,
716
- parentSpanId,
717
- dsc,
718
- };
719
- }
720
-
721
663
  /**
722
664
  * Injects and extracts `sentry-trace` and `baggage` headers from carriers.
723
665
  */
@@ -744,7 +686,7 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
744
686
  const activeSpan = api.trace.getSpan(context);
745
687
  const url = activeSpan && getCurrentURL(activeSpan);
746
688
 
747
- const tracePropagationTargets = _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3(), 'optionalAccess', _4 => _4.tracePropagationTargets]);
689
+ const tracePropagationTargets = core.getClient()?.getOptions()?.tracePropagationTargets;
748
690
  if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
749
691
  DEBUG_BUILD &&
750
692
  core.logger.log(
@@ -829,14 +771,14 @@ function shouldPropagateTraceForUrl(
829
771
  return true;
830
772
  }
831
773
 
832
- const cachedDecision = _optionalChain$4([decisionMap, 'optionalAccess', _5 => _5.get, 'call', _6 => _6(url)]);
774
+ const cachedDecision = decisionMap?.get(url);
833
775
  if (cachedDecision !== undefined) {
834
776
  DEBUG_BUILD && !cachedDecision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
835
777
  return cachedDecision;
836
778
  }
837
779
 
838
780
  const decision = core.stringMatchesSomePattern(url, tracePropagationTargets);
839
- _optionalChain$4([decisionMap, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(url, decision)]);
781
+ decisionMap?.set(url, decision);
840
782
 
841
783
  DEBUG_BUILD && !decision && core.logger.log(NOT_PROPAGATED_MESSAGE, url);
842
784
  return decision;
@@ -852,17 +794,15 @@ function getInjectionData(context)
852
794
 
853
795
  // If we have a remote span, the spanId should be considered as the parentSpanId, not spanId itself
854
796
  // Instead, we use a virtual (generated) spanId for propagation
855
- if (span && span.spanContext().isRemote) {
797
+ if (span?.spanContext().isRemote) {
856
798
  const spanContext = span.spanContext();
857
799
  const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(span);
858
800
 
859
801
  return {
860
802
  dynamicSamplingContext,
861
803
  traceId: spanContext.traceId,
862
- // Because this is a remote span, we do not want to propagate this directly
863
- // As otherwise things may be attached "directly" to an unrelated span
864
- spanId: core.generateSpanId(),
865
- sampled: getSamplingDecision(spanContext),
804
+ spanId: undefined,
805
+ sampled: getSamplingDecision(spanContext), // TODO: Do we need to change something here?
866
806
  };
867
807
  }
868
808
 
@@ -875,13 +815,13 @@ function getInjectionData(context)
875
815
  dynamicSamplingContext,
876
816
  traceId: spanContext.traceId,
877
817
  spanId: spanContext.spanId,
878
- sampled: getSamplingDecision(spanContext),
818
+ sampled: getSamplingDecision(spanContext), // TODO: Do we need to change something here?
879
819
  };
880
820
  }
881
821
 
882
822
  // Else we try to use the propagation context from the scope
883
823
  // The only scenario where this should happen is when we neither have a span, nor an incoming trace
884
- const scope = _optionalChain$4([getScopesFromContext, 'call', _9 => _9(context), 'optionalAccess', _10 => _10.scope]) || core.getCurrentScope();
824
+ const scope = getScopesFromContext(context)?.scope || core.getCurrentScope();
885
825
  const client = core.getClient();
886
826
 
887
827
  const propagationContext = scope.getPropagationContext();
@@ -889,9 +829,7 @@ function getInjectionData(context)
889
829
  return {
890
830
  dynamicSamplingContext,
891
831
  traceId: propagationContext.traceId,
892
- // TODO(v9): Use generateSpanId() instead
893
- // eslint-disable-next-line deprecation/deprecation
894
- spanId: propagationContext.spanId,
832
+ spanId: propagationContext.propagationSpanId,
895
833
  sampled: propagationContext.sampled,
896
834
  };
897
835
  }
@@ -952,7 +890,7 @@ function getExistingBaggage(carrier) {
952
890
  try {
953
891
  const baggage = (carrier )[SENTRY_BAGGAGE_HEADER];
954
892
  return Array.isArray(baggage) ? baggage.join(',') : baggage;
955
- } catch (e) {
893
+ } catch {
956
894
  return undefined;
957
895
  }
958
896
  }
@@ -970,13 +908,13 @@ function getCurrentURL(span) {
970
908
  const spanData = core.spanToJSON(span).data;
971
909
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
972
910
  // eslint-disable-next-line deprecation/deprecation
973
- const urlAttribute = _optionalChain$4([spanData, 'optionalAccess', _11 => _11[semanticConventions.SEMATTRS_HTTP_URL]]) || _optionalChain$4([spanData, 'optionalAccess', _12 => _12[semanticConventions.ATTR_URL_FULL]]);
974
- if (urlAttribute) {
911
+ const urlAttribute = spanData[semanticConventions.SEMATTRS_HTTP_URL] || spanData[semanticConventions.ATTR_URL_FULL];
912
+ if (typeof urlAttribute === 'string') {
975
913
  return urlAttribute;
976
914
  }
977
915
 
978
916
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
979
- 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)]);
917
+ const urlTraceState = span.spanContext().traceState?.get(SENTRY_TRACE_STATE_URL);
980
918
  if (urlTraceState) {
981
919
  return urlTraceState;
982
920
  }
@@ -1137,7 +1075,7 @@ function withActiveSpan(span, callback) {
1137
1075
 
1138
1076
  function getTracer() {
1139
1077
  const client = core.getClient();
1140
- return (client && client.tracer) || api.trace.getTracer('@sentry/opentelemetry', core.SDK_VERSION);
1078
+ return client?.tracer || api.trace.getTracer('@sentry/opentelemetry', core.SDK_VERSION);
1141
1079
  }
1142
1080
 
1143
1081
  function getSpanOptions(options) {
@@ -1415,8 +1353,6 @@ function setOpenTelemetryContextAsyncContextStrategy() {
1415
1353
  });
1416
1354
  }
1417
1355
 
1418
- 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; }
1419
-
1420
1356
  /**
1421
1357
  * Wrap an OpenTelemetry ContextManager in a way that ensures the context is kept in sync with the Sentry Scope.
1422
1358
  *
@@ -1453,8 +1389,8 @@ function wrapContextManagerClass(
1453
1389
  ...args
1454
1390
  ) {
1455
1391
  const currentScopes = getScopesFromContext(context);
1456
- const currentScope = _optionalChain$3([currentScopes, 'optionalAccess', _ => _.scope]) || core.getCurrentScope();
1457
- const currentIsolationScope = _optionalChain$3([currentScopes, 'optionalAccess', _2 => _2.isolationScope]) || core.getIsolationScope();
1392
+ const currentScope = currentScopes?.scope || core.getCurrentScope();
1393
+ const currentIsolationScope = currentScopes?.isolationScope || core.getIsolationScope();
1458
1394
 
1459
1395
  const shouldForkIsolationScope = context.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
1460
1396
  const scope = context.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) ;
@@ -1538,7 +1474,7 @@ function createOrUpdateNode(nodeMap, spanNode) {
1538
1474
  const existing = nodeMap.get(spanNode.id);
1539
1475
 
1540
1476
  // If span is already set, nothing to do here
1541
- if (existing && existing.span) {
1477
+ if (existing?.span) {
1542
1478
  return existing;
1543
1479
  }
1544
1480
 
@@ -1614,7 +1550,7 @@ function mapStatus(span) {
1614
1550
  }
1615
1551
 
1616
1552
  // We default to setting the spans status to ok.
1617
- if (status && status.code === api.SpanStatusCode.UNSET) {
1553
+ if (status?.code === api.SpanStatusCode.UNSET) {
1618
1554
  return { code: core.SPAN_STATUS_OK };
1619
1555
  } else {
1620
1556
  return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
@@ -1647,8 +1583,6 @@ function inferStatusFromAttributes(attributes) {
1647
1583
  return undefined;
1648
1584
  }
1649
1585
 
1650
- 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; }
1651
-
1652
1586
  const MAX_SPAN_COUNT = 1000;
1653
1587
  const DEFAULT_TIMEOUT = 300; // 5 min
1654
1588
 
@@ -1677,7 +1611,7 @@ class SentrySpanExporter {
1677
1611
  constructor(options
1678
1612
 
1679
1613
  ) {
1680
- this._finishedSpanBucketSize = _optionalChain$2([options, 'optionalAccess', _ => _.timeout]) || DEFAULT_TIMEOUT;
1614
+ this._finishedSpanBucketSize = options?.timeout || DEFAULT_TIMEOUT;
1681
1615
  this._finishedSpanBuckets = new Array(this._finishedSpanBucketSize).fill(undefined);
1682
1616
  this._lastCleanupTimestampInS = Math.floor(Date.now() / 1000);
1683
1617
  this._spansToBucketEntry = new WeakMap();
@@ -1899,7 +1833,6 @@ function createTransactionForOtelSpan(span) {
1899
1833
  source,
1900
1834
  },
1901
1835
  }),
1902
- _metrics_summary: core.getMetricSummaryJsonForSpan(span ),
1903
1836
  });
1904
1837
 
1905
1838
  return transactionEvent;
@@ -1949,7 +1882,6 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1949
1882
  status: core.getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1950
1883
  op,
1951
1884
  origin,
1952
- _metrics_summary: core.getMetricSummaryJsonForSpan(span ),
1953
1885
  measurements: core.timedEventsToMeasurements(span.events),
1954
1886
  });
1955
1887
 
@@ -2026,8 +1958,6 @@ function getData(span) {
2026
1958
  return data;
2027
1959
  }
2028
1960
 
2029
- function _optionalChain$1(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; }
2030
-
2031
1961
  function onSpanStart(span, parentContext) {
2032
1962
  // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
2033
1963
  const parentSpan = api.trace.getSpan(parentContext);
@@ -2040,7 +1970,7 @@ function onSpanStart(span, parentContext) {
2040
1970
  }
2041
1971
 
2042
1972
  // We need this in the span exporter
2043
- if (parentSpan && parentSpan.spanContext().isRemote) {
1973
+ if (parentSpan?.spanContext().isRemote) {
2044
1974
  span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE, true);
2045
1975
  }
2046
1976
 
@@ -2061,14 +1991,14 @@ function onSpanStart(span, parentContext) {
2061
1991
  core.logSpanStart(span);
2062
1992
 
2063
1993
  const client = core.getClient();
2064
- _optionalChain$1([client, 'optionalAccess', _ => _.emit, 'call', _2 => _2('spanStart', span)]);
1994
+ client?.emit('spanStart', span);
2065
1995
  }
2066
1996
 
2067
1997
  function onSpanEnd(span) {
2068
1998
  core.logSpanEnd(span);
2069
1999
 
2070
2000
  const client = core.getClient();
2071
- _optionalChain$1([client, 'optionalAccess', _3 => _3.emit, 'call', _4 => _4('spanEnd', span)]);
2001
+ client?.emit('spanEnd', span);
2072
2002
  }
2073
2003
 
2074
2004
  /**
@@ -2133,7 +2063,7 @@ class SentrySampler {
2133
2063
  const options = this._client.getOptions();
2134
2064
 
2135
2065
  const parentSpan = getValidSpan(context);
2136
- const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
2066
+ const parentContext = parentSpan?.spanContext();
2137
2067
 
2138
2068
  if (!core.hasTracingEnabled(options)) {
2139
2069
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
@@ -2145,7 +2075,7 @@ class SentrySampler {
2145
2075
 
2146
2076
  // If we have a http.client span that has no local parent, we never want to sample it
2147
2077
  // but we want to leave downstream sampling decisions up to the server
2148
- if (spanKind === api.SpanKind.CLIENT && maybeSpanHttpMethod && (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _3 => _3.isRemote]))) {
2078
+ if (spanKind === api.SpanKind.CLIENT && maybeSpanHttpMethod && (!parentSpan || parentContext?.isRemote)) {
2149
2079
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
2150
2080
  }
2151
2081
 
@@ -2182,40 +2112,76 @@ class SentrySampler {
2182
2112
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
2183
2113
  }
2184
2114
 
2185
- const [sampled, sampleRate] = core.sampleSpan(options, {
2186
- name: inferredSpanName,
2187
- attributes: mergedAttributes,
2188
- transactionContext: {
2189
- name: inferredSpanName,
2190
- parentSampled,
2191
- },
2192
- parentSampled,
2193
- });
2115
+ const isRootSpan = !parentSpan || parentContext?.isRemote;
2194
2116
 
2195
- const attributes = {
2196
- [core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
2197
- };
2117
+ // We only sample based on parameters (like tracesSampleRate or tracesSampler) for root spans (which is done in sampleSpan).
2118
+ // Non-root-spans simply inherit the sampling decision from their parent.
2119
+ if (isRootSpan) {
2120
+ const { isolationScope } = getScopesFromContext(context) ?? {};
2121
+
2122
+ const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
2123
+ const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
2124
+
2125
+ const sampleRand = core.parseSampleRate(dsc?.sample_rand) ?? Math.random();
2126
+
2127
+ const [sampled, sampleRate, localSampleRateWasApplied] = core.sampleSpan(
2128
+ options,
2129
+ {
2130
+ name: inferredSpanName,
2131
+ attributes: mergedAttributes,
2132
+ normalizedRequest: isolationScope?.getScopeData().sdkProcessingMetadata.normalizedRequest,
2133
+ parentSampled,
2134
+ parentSampleRate: core.parseSampleRate(dsc?.sample_rate),
2135
+ },
2136
+ sampleRand,
2137
+ );
2198
2138
 
2199
- const method = `${maybeSpanHttpMethod}`.toUpperCase();
2200
- if (method === 'OPTIONS' || method === 'HEAD') {
2201
- DEBUG_BUILD && core.logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2139
+ const method = `${maybeSpanHttpMethod}`.toUpperCase();
2140
+ if (method === 'OPTIONS' || method === 'HEAD') {
2141
+ DEBUG_BUILD && core.logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2142
+
2143
+ return {
2144
+ ...wrapSamplingDecision({
2145
+ decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
2146
+ context,
2147
+ spanAttributes,
2148
+ sampleRand,
2149
+ downstreamTraceSampleRate: 0, // we don't want to sample anything in the downstream trace either
2150
+ }),
2151
+ };
2152
+ }
2153
+
2154
+ if (
2155
+ !sampled &&
2156
+ // We check for `parentSampled === undefined` because we only want to record client reports for spans that are trace roots (ie. when there was incoming trace)
2157
+ parentSampled === undefined
2158
+ ) {
2159
+ DEBUG_BUILD && core.logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
2160
+ this._client.recordDroppedEvent('sample_rate', 'transaction');
2161
+ }
2202
2162
 
2203
2163
  return {
2204
- ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.NOT_RECORD, context, spanAttributes }),
2205
- attributes,
2164
+ ...wrapSamplingDecision({
2165
+ decision: sampled ? sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED : sdkTraceBase.SamplingDecision.NOT_RECORD,
2166
+ context,
2167
+ spanAttributes,
2168
+ sampleRand,
2169
+ downstreamTraceSampleRate: localSampleRateWasApplied ? sampleRate : undefined,
2170
+ }),
2171
+ attributes: {
2172
+ // We set the sample rate on the span when a local sample rate was applied to better understand how traces were sampled in Sentry
2173
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: localSampleRateWasApplied ? sampleRate : undefined,
2174
+ },
2206
2175
  };
2207
- }
2208
-
2209
- if (!sampled) {
2176
+ } else {
2210
2177
  return {
2211
- ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.NOT_RECORD, context, spanAttributes }),
2212
- attributes,
2178
+ ...wrapSamplingDecision({
2179
+ decision: parentSampled ? sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED : sdkTraceBase.SamplingDecision.NOT_RECORD,
2180
+ context,
2181
+ spanAttributes,
2182
+ }),
2213
2183
  };
2214
2184
  }
2215
- return {
2216
- ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED, context, spanAttributes }),
2217
- attributes,
2218
- };
2219
2185
  }
2220
2186
 
2221
2187
  /** Returns the sampler name or short description with the configuration. */
@@ -2224,14 +2190,6 @@ class SentrySampler {
2224
2190
  }
2225
2191
  }
2226
2192
 
2227
- function getParentRemoteSampled(parentSpan) {
2228
- const traceId = parentSpan.spanContext().traceId;
2229
- const traceparentData = getPropagationContextFromSpan(parentSpan);
2230
-
2231
- // Only inherit sampled if `traceId` is the same
2232
- return traceparentData && traceId === traceparentData.traceId ? traceparentData.sampled : undefined;
2233
- }
2234
-
2235
2193
  function getParentSampled(parentSpan, traceId, spanName) {
2236
2194
  const parentContext = parentSpan.spanContext();
2237
2195
 
@@ -2239,7 +2197,7 @@ function getParentSampled(parentSpan, traceId, spanName) {
2239
2197
  // Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
2240
2198
  if (api.isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
2241
2199
  if (parentContext.isRemote) {
2242
- const parentSampled = getParentRemoteSampled(parentSpan);
2200
+ const parentSampled = getSamplingDecision(parentSpan.spanContext());
2243
2201
  DEBUG_BUILD &&
2244
2202
  core.logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
2245
2203
  return parentSampled;
@@ -2262,8 +2220,24 @@ function wrapSamplingDecision({
2262
2220
  decision,
2263
2221
  context,
2264
2222
  spanAttributes,
2265
- }) {
2266
- const traceState = getBaseTraceState(context, spanAttributes);
2223
+ sampleRand,
2224
+ downstreamTraceSampleRate,
2225
+ }
2226
+
2227
+ ) {
2228
+ let traceState = getBaseTraceState(context, spanAttributes);
2229
+
2230
+ // We will override the propagated sample rate downstream when
2231
+ // - the tracesSampleRate is applied
2232
+ // - the tracesSampler is invoked
2233
+ // Since unsampled OTEL spans (NonRecordingSpans) cannot hold attributes we need to store this on the (trace)context.
2234
+ if (downstreamTraceSampleRate !== undefined) {
2235
+ traceState = traceState.set(SENTRY_TRACE_STATE_SAMPLE_RATE, `${downstreamTraceSampleRate}`);
2236
+ }
2237
+
2238
+ if (sampleRand !== undefined) {
2239
+ traceState = traceState.set(SENTRY_TRACE_STATE_SAMPLE_RAND, `${sampleRand}`);
2240
+ }
2267
2241
 
2268
2242
  // If the decision is undefined, we treat it as NOT_RECORDING, but we don't propagate this decision to downstream SDKs
2269
2243
  // Which is done by not setting `SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING` traceState
@@ -2280,9 +2254,9 @@ function wrapSamplingDecision({
2280
2254
 
2281
2255
  function getBaseTraceState(context, spanAttributes) {
2282
2256
  const parentSpan = api.trace.getSpan(context);
2283
- const parentContext = _optionalChain([parentSpan, 'optionalAccess', _4 => _4.spanContext, 'call', _5 => _5()]);
2257
+ const parentContext = parentSpan?.spanContext();
2284
2258
 
2285
- let traceState = _optionalChain([parentContext, 'optionalAccess', _6 => _6.traceState]) || new core$1.TraceState();
2259
+ let traceState = parentContext?.traceState || new core$1.TraceState();
2286
2260
 
2287
2261
  // We always keep the URL on the trace state, so we can access it in the propagator
2288
2262
  // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `ATTR_HTTP_URL`, for now.
@@ -2304,32 +2278,15 @@ function getValidSpan(context) {
2304
2278
  return span && api.isSpanContextValid(span.spanContext()) ? span : undefined;
2305
2279
  }
2306
2280
 
2307
- /**
2308
- * This method takes an OpenTelemetry instrumentation or
2309
- * array of instrumentations and registers them with OpenTelemetry.
2310
- *
2311
- * @deprecated This method will be removed in the next major version of the SDK.
2312
- * Use the `openTelemetryInstrumentations` option in `Sentry.init()` or your custom Sentry Client instead.
2313
- */
2314
- function addOpenTelemetryInstrumentation(...instrumentations) {
2315
- instrumentation.registerInstrumentations({
2316
- instrumentations,
2317
- });
2318
- }
2319
-
2320
2281
  exports.getClient = core.getClient;
2321
- exports.getCurrentHubShim = core.getCurrentHubShim;
2322
2282
  exports.getDynamicSamplingContextFromSpan = core.getDynamicSamplingContextFromSpan;
2323
2283
  exports.SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION = SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION;
2324
2284
  exports.SentryPropagator = SentryPropagator;
2325
2285
  exports.SentrySampler = SentrySampler;
2326
2286
  exports.SentrySpanProcessor = SentrySpanProcessor;
2327
- exports.addOpenTelemetryInstrumentation = addOpenTelemetryInstrumentation;
2328
2287
  exports.continueTrace = continueTrace;
2329
2288
  exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
2330
- exports.generateSpanContextForPropagationContext = generateSpanContextForPropagationContext;
2331
2289
  exports.getActiveSpan = getActiveSpan;
2332
- exports.getPropagationContextFromSpan = getPropagationContextFromSpan;
2333
2290
  exports.getRequestSpanData = getRequestSpanData;
2334
2291
  exports.getScopesFromContext = getScopesFromContext;
2335
2292
  exports.getSpanKind = getSpanKind;