@sentry/opentelemetry 8.10.0 → 8.12.0-beta.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/esm/index.js CHANGED
@@ -818,27 +818,32 @@ function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[
818
818
  function startSpan(options, callback) {
819
819
  const tracer = getTracer();
820
820
 
821
- const { name } = options;
822
-
823
- const activeCtx = getContext(options.scope, options.forceTransaction);
824
- const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
825
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
826
-
827
- const spanContext = getSpanContext(options);
828
-
829
- return tracer.startActiveSpan(name, spanContext, ctx, span => {
830
- _applySentryAttributesToSpan(span, options);
831
-
832
- return handleCallbackErrors(
833
- () => callback(span),
834
- () => {
835
- // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
836
- if (spanToJSON(span).status === undefined) {
837
- span.setStatus({ code: SpanStatusCode.ERROR });
838
- }
839
- },
840
- () => span.end(),
841
- );
821
+ const { name, parentSpan: customParentSpan } = options;
822
+
823
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
824
+ const wrapper = getActiveSpanWrapper(customParentSpan);
825
+
826
+ return wrapper(() => {
827
+ const activeCtx = getContext(options.scope, options.forceTransaction);
828
+ const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
829
+ const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
830
+
831
+ const spanOptions = getSpanOptions(options);
832
+
833
+ return tracer.startActiveSpan(name, spanOptions, ctx, span => {
834
+ _applySentryAttributesToSpan(span, options);
835
+
836
+ return handleCallbackErrors(
837
+ () => callback(span),
838
+ () => {
839
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
840
+ if (spanToJSON(span).status === undefined) {
841
+ span.setStatus({ code: SpanStatusCode.ERROR });
842
+ }
843
+ },
844
+ () => span.end(),
845
+ );
846
+ });
842
847
  });
843
848
  }
844
849
 
@@ -858,26 +863,31 @@ function startSpanManual(
858
863
  ) {
859
864
  const tracer = getTracer();
860
865
 
861
- const { name } = options;
866
+ const { name, parentSpan: customParentSpan } = options;
862
867
 
863
- const activeCtx = getContext(options.scope, options.forceTransaction);
864
- const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
865
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
868
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
869
+ const wrapper = getActiveSpanWrapper(customParentSpan);
866
870
 
867
- const spanContext = getSpanContext(options);
871
+ return wrapper(() => {
872
+ const activeCtx = getContext(options.scope, options.forceTransaction);
873
+ const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
874
+ const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
868
875
 
869
- return tracer.startActiveSpan(name, spanContext, ctx, span => {
870
- _applySentryAttributesToSpan(span, options);
876
+ const spanOptions = getSpanOptions(options);
871
877
 
872
- return handleCallbackErrors(
873
- () => callback(span, () => span.end()),
874
- () => {
875
- // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
876
- if (spanToJSON(span).status === undefined) {
877
- span.setStatus({ code: SpanStatusCode.ERROR });
878
- }
879
- },
880
- );
878
+ return tracer.startActiveSpan(name, spanOptions, ctx, span => {
879
+ _applySentryAttributesToSpan(span, options);
880
+
881
+ return handleCallbackErrors(
882
+ () => callback(span, () => span.end()),
883
+ () => {
884
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
885
+ if (spanToJSON(span).status === undefined) {
886
+ span.setStatus({ code: SpanStatusCode.ERROR });
887
+ }
888
+ },
889
+ );
890
+ });
881
891
  });
882
892
  }
883
893
 
@@ -893,19 +903,24 @@ function startSpanManual(
893
903
  function startInactiveSpan(options) {
894
904
  const tracer = getTracer();
895
905
 
896
- const { name } = options;
906
+ const { name, parentSpan: customParentSpan } = options;
897
907
 
898
- const activeCtx = getContext(options.scope, options.forceTransaction);
899
- const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
900
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
908
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
909
+ const wrapper = getActiveSpanWrapper(customParentSpan);
901
910
 
902
- const spanContext = getSpanContext(options);
911
+ return wrapper(() => {
912
+ const activeCtx = getContext(options.scope, options.forceTransaction);
913
+ const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
914
+ const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
903
915
 
904
- const span = tracer.startSpan(name, spanContext, ctx);
916
+ const spanOptions = getSpanOptions(options);
905
917
 
906
- _applySentryAttributesToSpan(span, options);
918
+ const span = tracer.startSpan(name, spanOptions, ctx);
907
919
 
908
- return span;
920
+ _applySentryAttributesToSpan(span, options);
921
+
922
+ return span;
923
+ });
909
924
  }
910
925
 
911
926
  /**
@@ -935,7 +950,7 @@ function _applySentryAttributesToSpan(span, options) {
935
950
  }
936
951
  }
937
952
 
938
- function getSpanContext(options) {
953
+ function getSpanOptions(options) {
939
954
  const { startTime, attributes, kind } = options;
940
955
 
941
956
  // OTEL expects timestamps in ms, not seconds
@@ -974,7 +989,7 @@ function getContext(scope, forceTransaction) {
974
989
  sampled: propagationContext.sampled,
975
990
  });
976
991
 
977
- const spanContext = {
992
+ const spanOptions = {
978
993
  traceId: propagationContext.traceId,
979
994
  spanId: propagationContext.parentSpanId || propagationContext.spanId,
980
995
  isRemote: true,
@@ -983,7 +998,7 @@ function getContext(scope, forceTransaction) {
983
998
  };
984
999
 
985
1000
  // Add remote parent span context,
986
- return trace.setSpanContext(ctx, spanContext);
1001
+ return trace.setSpanContext(ctx, spanOptions);
987
1002
  }
988
1003
 
989
1004
  // if we have no scope or client, we just return the context as-is
@@ -1016,7 +1031,7 @@ function getContext(scope, forceTransaction) {
1016
1031
  sampled,
1017
1032
  });
1018
1033
 
1019
- const spanContext = {
1034
+ const spanOptions = {
1020
1035
  traceId,
1021
1036
  spanId,
1022
1037
  isRemote: true,
@@ -1024,7 +1039,7 @@ function getContext(scope, forceTransaction) {
1024
1039
  traceState,
1025
1040
  };
1026
1041
 
1027
- const ctxWithSpanContext = trace.setSpanContext(ctxWithoutSpan, spanContext);
1042
+ const ctxWithSpanContext = trace.setSpanContext(ctxWithoutSpan, spanOptions);
1028
1043
 
1029
1044
  return ctxWithSpanContext;
1030
1045
  }
@@ -1057,6 +1072,16 @@ function continueTrace(options, callback) {
1057
1072
  });
1058
1073
  }
1059
1074
 
1075
+ function getActiveSpanWrapper(parentSpan) {
1076
+ return parentSpan
1077
+ ? (callback) => {
1078
+ // We cast this, because the OTEL Span has a few more methods than our Span interface
1079
+ // TODO: Add these missing methods to the Span interface
1080
+ return withActiveSpan(parentSpan , callback);
1081
+ }
1082
+ : (callback) => callback();
1083
+ }
1084
+
1060
1085
  /** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
1061
1086
  function suppressTracing(callback) {
1062
1087
  const ctx = suppressTracing$1(context.active());
@@ -1443,14 +1468,16 @@ function inferStatusFromAttributes(attributes) {
1443
1468
  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; }
1444
1469
 
1445
1470
  const MAX_SPAN_COUNT = 1000;
1471
+ const DEFAULT_TIMEOUT = 300; // 5 min
1446
1472
 
1447
1473
  /**
1448
1474
  * A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
1449
1475
  */
1450
1476
  class SentrySpanExporter {
1451
1477
 
1452
- constructor() {
1478
+ constructor(options) {
1453
1479
  this._finishedSpans = [];
1480
+ this._timeout = _optionalChain$2([options, 'optionalAccess', _ => _.timeout]) || DEFAULT_TIMEOUT;
1454
1481
  }
1455
1482
 
1456
1483
  /** Export a single span. */
@@ -1511,7 +1538,7 @@ class SentrySpanExporter {
1511
1538
  */
1512
1539
  _cleanupOldSpans(spans = this._finishedSpans) {
1513
1540
  this._finishedSpans = spans.filter(span => {
1514
- const shouldDrop = shouldCleanupSpan(span, 5 * 60);
1541
+ const shouldDrop = shouldCleanupSpan(span, this._timeout);
1515
1542
  DEBUG_BUILD &&
1516
1543
  shouldDrop &&
1517
1544
  logger.log(
@@ -1611,7 +1638,7 @@ function createTransactionForOtelSpan(span) {
1611
1638
 
1612
1639
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1613
1640
 
1614
- 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)]);
1641
+ 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)]);
1615
1642
 
1616
1643
  // If parentSpanIdFromTraceState is defined at all, we want it to take presedence
1617
1644
  // In that case, an empty string should be interpreted as "no parent span id",
@@ -1829,9 +1856,9 @@ function onSpanEnd(span) {
1829
1856
  */
1830
1857
  class SentrySpanProcessor {
1831
1858
 
1832
- constructor() {
1859
+ constructor(options) {
1833
1860
  setIsSetup('SentrySpanProcessor');
1834
- this._exporter = new SentrySpanExporter();
1861
+ this._exporter = new SentrySpanExporter(options);
1835
1862
  }
1836
1863
 
1837
1864
  /**