@sentry/opentelemetry 10.27.0 → 10.28.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.
@@ -1,5 +1,5 @@
1
1
  import { ATTR_URL_FULL, SEMATTRS_HTTP_URL, ATTR_HTTP_REQUEST_METHOD, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, ATTR_HTTP_ROUTE, ATTR_HTTP_RESPONSE_STATUS_CODE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
2
- import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, debug, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, handleCallbackErrors, spanToTraceContext, getTraceContextFromScope, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, debounce, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, sampleSpan } from '@sentry/core';
2
+ import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, debug, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, spanToTraceContext, getTraceContextFromScope, handleCallbackErrors, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, debounce, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, sampleSpan } from '@sentry/core';
3
3
  export { getClient, getDynamicSamplingContextFromSpan } from '@sentry/core';
4
4
  import * as api from '@opentelemetry/api';
5
5
  import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
@@ -962,16 +962,12 @@ function generateRemoteSpanContext({
962
962
  }
963
963
 
964
964
  /**
965
- * Wraps a function with a transaction/span and finishes the span after the function is done.
966
- * The created span is the active span and will be used as parent by other spans created inside the function
967
- * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
968
- *
969
- * If you want to create a span that is not set as active, use {@link startInactiveSpan}.
970
- *
971
- * You'll always get a span passed to the callback,
972
- * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
965
+ * Internal helper for starting spans and manual spans. See {@link startSpan} and {@link startSpanManual} for the public APIs.
966
+ * @param options - The span context options
967
+ * @param callback - The callback to execute with the span
968
+ * @param autoEnd - Whether to automatically end the span after the callback completes
973
969
  */
974
- function startSpan(options, callback) {
970
+ function _startSpan(options, callback, autoEnd) {
975
971
  const tracer = getTracer();
976
972
 
977
973
  const { name, parentSpan: customParentSpan } = options;
@@ -986,6 +982,37 @@ function startSpan(options, callback) {
986
982
 
987
983
  const spanOptions = getSpanOptions(options);
988
984
 
985
+ // If spans are not enabled, ensure we suppress tracing for the span creation
986
+ // but preserve the original context for the callback execution
987
+ // This ensures that we don't create spans when tracing is disabled which
988
+ // would otherwise be a problem for users that don't enable tracing but use
989
+ // custom OpenTelemetry setups.
990
+ if (!hasSpansEnabled()) {
991
+ const suppressedCtx = isTracingSuppressed(ctx) ? ctx : suppressTracing$1(ctx);
992
+
993
+ return context.with(suppressedCtx, () => {
994
+ return tracer.startActiveSpan(name, spanOptions, suppressedCtx, span => {
995
+ // Restore the original unsuppressed context for the callback execution
996
+ // so that custom OpenTelemetry spans maintain the correct context.
997
+ // We use activeCtx (not ctx) because ctx may be suppressed when onlyIfParent is true
998
+ // and no parent span exists. Using activeCtx ensures custom OTel spans are never
999
+ // inadvertently suppressed.
1000
+ return context.with(activeCtx, () => {
1001
+ return handleCallbackErrors(
1002
+ () => callback(span),
1003
+ () => {
1004
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
1005
+ if (spanToJSON(span).status === undefined) {
1006
+ span.setStatus({ code: SpanStatusCode.ERROR });
1007
+ }
1008
+ },
1009
+ autoEnd ? () => span.end() : undefined,
1010
+ );
1011
+ });
1012
+ });
1013
+ });
1014
+ }
1015
+
989
1016
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
990
1017
  return handleCallbackErrors(
991
1018
  () => callback(span),
@@ -995,15 +1022,29 @@ function startSpan(options, callback) {
995
1022
  span.setStatus({ code: SpanStatusCode.ERROR });
996
1023
  }
997
1024
  },
998
- () => span.end(),
1025
+ autoEnd ? () => span.end() : undefined,
999
1026
  );
1000
1027
  });
1001
1028
  });
1002
1029
  }
1003
1030
 
1031
+ /**
1032
+ * Wraps a function with a transaction/span and finishes the span after the function is done.
1033
+ * The created span is the active span and will be used as parent by other spans created inside the function
1034
+ * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
1035
+ *
1036
+ * If you want to create a span that is not set as active, use {@link startInactiveSpan}.
1037
+ *
1038
+ * You'll always get a span passed to the callback,
1039
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
1040
+ */
1041
+ function startSpan(options, callback) {
1042
+ return _startSpan(options, callback, true);
1043
+ }
1044
+
1004
1045
  /**
1005
1046
  * Similar to `Sentry.startSpan`. Wraps a function with a span, but does not finish the span
1006
- * after the function is done automatically. You'll have to call `span.end()` manually.
1047
+ * after the function is done automatically. You'll have to call `span.end()` or the `finish` function passed to the callback manually.
1007
1048
  *
1008
1049
  * The created span is the active span and will be used as parent by other spans created inside the function
1009
1050
  * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
@@ -1015,32 +1056,7 @@ function startSpanManual(
1015
1056
  options,
1016
1057
  callback,
1017
1058
  ) {
1018
- const tracer = getTracer();
1019
-
1020
- const { name, parentSpan: customParentSpan } = options;
1021
-
1022
- // If `options.parentSpan` is defined, we want to wrap the callback in `withActiveSpan`
1023
- const wrapper = getActiveSpanWrapper(customParentSpan);
1024
-
1025
- return wrapper(() => {
1026
- const activeCtx = getContext(options.scope, options.forceTransaction);
1027
- const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
1028
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
1029
-
1030
- const spanOptions = getSpanOptions(options);
1031
-
1032
- return tracer.startActiveSpan(name, spanOptions, ctx, span => {
1033
- return handleCallbackErrors(
1034
- () => callback(span, () => span.end()),
1035
- () => {
1036
- // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
1037
- if (spanToJSON(span).status === undefined) {
1038
- span.setStatus({ code: SpanStatusCode.ERROR });
1039
- }
1040
- },
1041
- );
1042
- });
1043
- });
1059
+ return _startSpan(options, span => callback(span, () => span.end()), false);
1044
1060
  }
1045
1061
 
1046
1062
  /**
@@ -1063,13 +1079,15 @@ function startInactiveSpan(options) {
1063
1079
  return wrapper(() => {
1064
1080
  const activeCtx = getContext(options.scope, options.forceTransaction);
1065
1081
  const shouldSkipSpan = options.onlyIfParent && !trace.getSpan(activeCtx);
1066
- const ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
1082
+ let ctx = shouldSkipSpan ? suppressTracing$1(activeCtx) : activeCtx;
1067
1083
 
1068
1084
  const spanOptions = getSpanOptions(options);
1069
1085
 
1070
- const span = tracer.startSpan(name, spanOptions, ctx);
1086
+ if (!hasSpansEnabled()) {
1087
+ ctx = isTracingSuppressed(ctx) ? ctx : suppressTracing$1(ctx);
1088
+ }
1071
1089
 
1072
- return span;
1090
+ return tracer.startSpan(name, spanOptions, ctx);
1073
1091
  });
1074
1092
  }
1075
1093