@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/cjs/index.js CHANGED
@@ -821,27 +821,32 @@ function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[
821
821
  function startSpan(options, callback) {
822
822
  const tracer = getTracer();
823
823
 
824
- const { name } = options;
825
-
826
- const activeCtx = getContext(options.scope, options.forceTransaction);
827
- const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
828
- const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
829
-
830
- const spanContext = getSpanContext(options);
831
-
832
- return tracer.startActiveSpan(name, spanContext, ctx, span => {
833
- _applySentryAttributesToSpan(span, options);
834
-
835
- return core.handleCallbackErrors(
836
- () => callback(span),
837
- () => {
838
- // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
839
- if (core.spanToJSON(span).status === undefined) {
840
- span.setStatus({ code: api.SpanStatusCode.ERROR });
841
- }
842
- },
843
- () => span.end(),
844
- );
824
+ const { name, parentSpan: customParentSpan } = options;
825
+
826
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
827
+ const wrapper = getActiveSpanWrapper(customParentSpan);
828
+
829
+ return wrapper(() => {
830
+ const activeCtx = getContext(options.scope, options.forceTransaction);
831
+ const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
832
+ const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
833
+
834
+ const spanOptions = getSpanOptions(options);
835
+
836
+ return tracer.startActiveSpan(name, spanOptions, ctx, span => {
837
+ _applySentryAttributesToSpan(span, options);
838
+
839
+ return core.handleCallbackErrors(
840
+ () => callback(span),
841
+ () => {
842
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
843
+ if (core.spanToJSON(span).status === undefined) {
844
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
845
+ }
846
+ },
847
+ () => span.end(),
848
+ );
849
+ });
845
850
  });
846
851
  }
847
852
 
@@ -861,26 +866,31 @@ function startSpanManual(
861
866
  ) {
862
867
  const tracer = getTracer();
863
868
 
864
- const { name } = options;
869
+ const { name, parentSpan: customParentSpan } = options;
865
870
 
866
- const activeCtx = getContext(options.scope, options.forceTransaction);
867
- const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
868
- const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
871
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
872
+ const wrapper = getActiveSpanWrapper(customParentSpan);
869
873
 
870
- const spanContext = getSpanContext(options);
874
+ return wrapper(() => {
875
+ const activeCtx = getContext(options.scope, options.forceTransaction);
876
+ const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
877
+ const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
871
878
 
872
- return tracer.startActiveSpan(name, spanContext, ctx, span => {
873
- _applySentryAttributesToSpan(span, options);
879
+ const spanOptions = getSpanOptions(options);
874
880
 
875
- return core.handleCallbackErrors(
876
- () => callback(span, () => span.end()),
877
- () => {
878
- // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
879
- if (core.spanToJSON(span).status === undefined) {
880
- span.setStatus({ code: api.SpanStatusCode.ERROR });
881
- }
882
- },
883
- );
881
+ return tracer.startActiveSpan(name, spanOptions, ctx, span => {
882
+ _applySentryAttributesToSpan(span, options);
883
+
884
+ return core.handleCallbackErrors(
885
+ () => callback(span, () => span.end()),
886
+ () => {
887
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
888
+ if (core.spanToJSON(span).status === undefined) {
889
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
890
+ }
891
+ },
892
+ );
893
+ });
884
894
  });
885
895
  }
886
896
 
@@ -896,19 +906,24 @@ function startSpanManual(
896
906
  function startInactiveSpan(options) {
897
907
  const tracer = getTracer();
898
908
 
899
- const { name } = options;
909
+ const { name, parentSpan: customParentSpan } = options;
900
910
 
901
- const activeCtx = getContext(options.scope, options.forceTransaction);
902
- const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
903
- const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
911
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
912
+ const wrapper = getActiveSpanWrapper(customParentSpan);
904
913
 
905
- const spanContext = getSpanContext(options);
914
+ return wrapper(() => {
915
+ const activeCtx = getContext(options.scope, options.forceTransaction);
916
+ const shouldSkipSpan = options.onlyIfParent && !api.trace.getSpan(activeCtx);
917
+ const ctx = shouldSkipSpan ? core$1.suppressTracing(activeCtx) : activeCtx;
906
918
 
907
- const span = tracer.startSpan(name, spanContext, ctx);
919
+ const spanOptions = getSpanOptions(options);
908
920
 
909
- _applySentryAttributesToSpan(span, options);
921
+ const span = tracer.startSpan(name, spanOptions, ctx);
910
922
 
911
- return span;
923
+ _applySentryAttributesToSpan(span, options);
924
+
925
+ return span;
926
+ });
912
927
  }
913
928
 
914
929
  /**
@@ -938,7 +953,7 @@ function _applySentryAttributesToSpan(span, options) {
938
953
  }
939
954
  }
940
955
 
941
- function getSpanContext(options) {
956
+ function getSpanOptions(options) {
942
957
  const { startTime, attributes, kind } = options;
943
958
 
944
959
  // OTEL expects timestamps in ms, not seconds
@@ -977,7 +992,7 @@ function getContext(scope, forceTransaction) {
977
992
  sampled: propagationContext.sampled,
978
993
  });
979
994
 
980
- const spanContext = {
995
+ const spanOptions = {
981
996
  traceId: propagationContext.traceId,
982
997
  spanId: propagationContext.parentSpanId || propagationContext.spanId,
983
998
  isRemote: true,
@@ -986,7 +1001,7 @@ function getContext(scope, forceTransaction) {
986
1001
  };
987
1002
 
988
1003
  // Add remote parent span context,
989
- return api.trace.setSpanContext(ctx, spanContext);
1004
+ return api.trace.setSpanContext(ctx, spanOptions);
990
1005
  }
991
1006
 
992
1007
  // if we have no scope or client, we just return the context as-is
@@ -1019,7 +1034,7 @@ function getContext(scope, forceTransaction) {
1019
1034
  sampled,
1020
1035
  });
1021
1036
 
1022
- const spanContext = {
1037
+ const spanOptions = {
1023
1038
  traceId,
1024
1039
  spanId,
1025
1040
  isRemote: true,
@@ -1027,7 +1042,7 @@ function getContext(scope, forceTransaction) {
1027
1042
  traceState,
1028
1043
  };
1029
1044
 
1030
- const ctxWithSpanContext = api.trace.setSpanContext(ctxWithoutSpan, spanContext);
1045
+ const ctxWithSpanContext = api.trace.setSpanContext(ctxWithoutSpan, spanOptions);
1031
1046
 
1032
1047
  return ctxWithSpanContext;
1033
1048
  }
@@ -1060,6 +1075,16 @@ function continueTrace(options, callback) {
1060
1075
  });
1061
1076
  }
1062
1077
 
1078
+ function getActiveSpanWrapper(parentSpan) {
1079
+ return parentSpan
1080
+ ? (callback) => {
1081
+ // We cast this, because the OTEL Span has a few more methods than our Span interface
1082
+ // TODO: Add these missing methods to the Span interface
1083
+ return withActiveSpan(parentSpan , callback);
1084
+ }
1085
+ : (callback) => callback();
1086
+ }
1087
+
1063
1088
  /** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
1064
1089
  function suppressTracing(callback) {
1065
1090
  const ctx = core$1.suppressTracing(api.context.active());
@@ -1446,14 +1471,16 @@ function inferStatusFromAttributes(attributes) {
1446
1471
  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; }
1447
1472
 
1448
1473
  const MAX_SPAN_COUNT = 1000;
1474
+ const DEFAULT_TIMEOUT = 300; // 5 min
1449
1475
 
1450
1476
  /**
1451
1477
  * A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
1452
1478
  */
1453
1479
  class SentrySpanExporter {
1454
1480
 
1455
- constructor() {
1481
+ constructor(options) {
1456
1482
  this._finishedSpans = [];
1483
+ this._timeout = _optionalChain$2([options, 'optionalAccess', _ => _.timeout]) || DEFAULT_TIMEOUT;
1457
1484
  }
1458
1485
 
1459
1486
  /** Export a single span. */
@@ -1514,7 +1541,7 @@ class SentrySpanExporter {
1514
1541
  */
1515
1542
  _cleanupOldSpans(spans = this._finishedSpans) {
1516
1543
  this._finishedSpans = spans.filter(span => {
1517
- const shouldDrop = shouldCleanupSpan(span, 5 * 60);
1544
+ const shouldDrop = shouldCleanupSpan(span, this._timeout);
1518
1545
  DEBUG_BUILD &&
1519
1546
  shouldDrop &&
1520
1547
  utils.logger.log(
@@ -1614,7 +1641,7 @@ function createTransactionForOtelSpan(span) {
1614
1641
 
1615
1642
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
1616
1643
 
1617
- 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)]);
1644
+ 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)]);
1618
1645
 
1619
1646
  // If parentSpanIdFromTraceState is defined at all, we want it to take presedence
1620
1647
  // In that case, an empty string should be interpreted as "no parent span id",
@@ -1832,9 +1859,9 @@ function onSpanEnd(span) {
1832
1859
  */
1833
1860
  class SentrySpanProcessor {
1834
1861
 
1835
- constructor() {
1862
+ constructor(options) {
1836
1863
  setIsSetup('SentrySpanProcessor');
1837
- this._exporter = new SentrySpanExporter();
1864
+ this._exporter = new SentrySpanExporter(options);
1838
1865
  }
1839
1866
 
1840
1867
  /**