@sentry/opentelemetry 10.45.0 → 10.47.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, ATTR_DB_SYSTEM_NAME, 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, shouldPropagateTraceForUrl, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, generateTraceparentHeader, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, spanToTraceContext, getTraceContextFromScope, getRootSpan, handleCallbackErrors, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, _INTERNAL_safeDateNow, debounce, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, _INTERNAL_safeMathRandom, 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, shouldPropagateTraceForUrl, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, generateTraceparentHeader, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, spanToTraceContext, getTraceContextFromScope, generateTraceId, generateSpanId, _INTERNAL_safeMathRandom, getRootSpan, handleCallbackErrors, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, _INTERNAL_safeDateNow, 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, shouldPropagateTraceForUrl } 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';
@@ -705,6 +705,8 @@ class SentryPropagator extends W3CBaggagePropagator {
705
705
  }
706
706
 
707
707
  const existingBaggageHeader = getExistingBaggage(carrier);
708
+ const existingSentryTraceHeader = getExistingSentryTrace(carrier);
709
+
708
710
  let baggage = propagation.getBaggage(context) || propagation.createBaggage({});
709
711
 
710
712
  const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context);
@@ -714,12 +716,18 @@ class SentryPropagator extends W3CBaggagePropagator {
714
716
 
715
717
  if (baggageEntries) {
716
718
  Object.entries(baggageEntries).forEach(([key, value]) => {
719
+ if (!existingSentryTraceHeader && key.startsWith(SENTRY_BAGGAGE_KEY_PREFIX)) {
720
+ // Edge case: A baggage header with sentry- keys was added previously but no
721
+ // sentry-trace header. In this case we remove the old sentry-keys and add new
722
+ // ones below.
723
+ return;
724
+ }
717
725
  baggage = baggage.setEntry(key, { value });
718
726
  });
719
727
  }
720
728
  }
721
729
 
722
- if (dynamicSamplingContext) {
730
+ if (!existingSentryTraceHeader && dynamicSamplingContext) {
723
731
  baggage = Object.entries(dynamicSamplingContext).reduce((b, [dscKey, dscValue]) => {
724
732
  if (dscValue) {
725
733
  return b.setEntry(`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
@@ -729,7 +737,7 @@ class SentryPropagator extends W3CBaggagePropagator {
729
737
  }
730
738
 
731
739
  // We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason
732
- if (traceId && traceId !== INVALID_TRACEID) {
740
+ if (!existingSentryTraceHeader && traceId && traceId !== INVALID_TRACEID) {
733
741
  setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled));
734
742
 
735
743
  if (propagateTraceparent) {
@@ -884,6 +892,14 @@ function getExistingBaggage(carrier) {
884
892
  }
885
893
  }
886
894
 
895
+ function getExistingSentryTrace(carrier) {
896
+ try {
897
+ return (carrier )[SENTRY_TRACE_HEADER];
898
+ } catch {
899
+ return undefined;
900
+ }
901
+ }
902
+
887
903
  /**
888
904
  * It is pretty tricky to get access to the outgoing request URL of a request in the propagator.
889
905
  * As we only have access to the context of the span to be sent and the carrier (=headers),
@@ -967,6 +983,7 @@ function _startSpan(options, callback, autoEnd) {
967
983
 
968
984
  return context.with(suppressedCtx, () => {
969
985
  return tracer.startActiveSpan(name, spanOptions, suppressedCtx, span => {
986
+ patchSpanEnd(span);
970
987
  // Restore the original unsuppressed context for the callback execution
971
988
  // so that custom OpenTelemetry spans maintain the correct context.
972
989
  // We use activeCtx (not ctx) because ctx may be suppressed when onlyIfParent is true
@@ -989,6 +1006,7 @@ function _startSpan(options, callback, autoEnd) {
989
1006
  }
990
1007
 
991
1008
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
1009
+ patchSpanEnd(span);
992
1010
  return handleCallbackErrors(
993
1011
  () => callback(span),
994
1012
  () => {
@@ -1062,7 +1080,9 @@ function startInactiveSpan(options) {
1062
1080
  ctx = isTracingSuppressed(ctx) ? ctx : suppressTracing$1(ctx);
1063
1081
  }
1064
1082
 
1065
- return tracer.startSpan(name, spanOptions, ctx);
1083
+ const span = tracer.startSpan(name, spanOptions, ctx);
1084
+ patchSpanEnd(span);
1085
+ return span;
1066
1086
  });
1067
1087
  }
1068
1088
 
@@ -1109,6 +1129,17 @@ function ensureTimestampInMilliseconds(timestamp) {
1109
1129
  return isMs ? timestamp * 1000 : timestamp;
1110
1130
  }
1111
1131
 
1132
+ /**
1133
+ * Wraps the span's `end()` method so that numeric timestamps passed in seconds
1134
+ * are converted to milliseconds before reaching OTel's native `Span.end()`.
1135
+ */
1136
+ function patchSpanEnd(span) {
1137
+ const originalEnd = span.end.bind(span);
1138
+ span.end = (endTime) => {
1139
+ return originalEnd(typeof endTime === 'number' ? ensureTimestampInMilliseconds(endTime) : endTime);
1140
+ };
1141
+ }
1142
+
1112
1143
  function getContext(scope, forceTransaction) {
1113
1144
  const ctx = getContextForScope(scope);
1114
1145
  const parentSpan = trace.getSpan(ctx);
@@ -1183,6 +1214,36 @@ function continueTrace(options, callback) {
1183
1214
  return continueTraceAsRemoteSpan(context.active(), options, callback);
1184
1215
  }
1185
1216
 
1217
+ /**
1218
+ * Start a new trace with a unique traceId, ensuring all spans created within the callback
1219
+ * share the same traceId.
1220
+ *
1221
+ * This is a custom version of `startNewTrace` for OTEL-powered environments.
1222
+ * It injects the new traceId as a remote span context into the OTEL context, so that
1223
+ * `startInactiveSpan` and `startSpan` pick it up correctly.
1224
+ */
1225
+ function startNewTrace(callback) {
1226
+ const traceId = generateTraceId();
1227
+ const spanId = generateSpanId();
1228
+
1229
+ const spanContext = {
1230
+ traceId,
1231
+ spanId,
1232
+ isRemote: true,
1233
+ traceFlags: TraceFlags.NONE,
1234
+ };
1235
+
1236
+ const ctxWithTrace = trace.setSpanContext(context.active(), spanContext);
1237
+
1238
+ return context.with(ctxWithTrace, () => {
1239
+ getCurrentScope().setPropagationContext({
1240
+ traceId,
1241
+ sampleRand: _INTERNAL_safeMathRandom(),
1242
+ });
1243
+ return callback();
1244
+ });
1245
+ }
1246
+
1186
1247
  /**
1187
1248
  * Get the trace context for a given scope.
1188
1249
  * We have a custom implementation here because we need an OTEL-specific way to get the span from a scope.
@@ -1366,6 +1427,7 @@ function setOpenTelemetryContextAsyncContextStrategy() {
1366
1427
  suppressTracing,
1367
1428
  getTraceData,
1368
1429
  continueTrace,
1430
+ startNewTrace,
1369
1431
  // The types here don't fully align, because our own `Span` type is narrower
1370
1432
  // than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
1371
1433
  withActiveSpan: withActiveSpan ,