@sentry/opentelemetry 8.10.0 → 8.11.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;
907
+
908
+ // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
909
+ const wrapper = getActiveSpanWrapper(customParentSpan);
897
910
 
898
- const activeCtx = getContext(options.scope, options.forceTransaction);
899
- const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
900
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
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;
901
915
 
902
- const spanContext = getSpanContext(options);
916
+ const spanOptions = getSpanOptions(options);
903
917
 
904
- const span = tracer.startSpan(name, spanContext, ctx);
918
+ const span = tracer.startSpan(name, spanOptions, ctx);
905
919
 
906
- _applySentryAttributesToSpan(span, options);
920
+ _applySentryAttributesToSpan(span, options);
907
921
 
908
- return span;
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());