@sentry/opentelemetry 10.46.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.
- package/build/cjs/index.js +65 -3
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +66 -4
- package/build/esm/index.js.map +1 -1
- package/build/esm/package.json +1 -1
- package/build/types/asyncContextStrategy.d.ts.map +1 -1
- package/build/types/propagator.d.ts.map +1 -1
- package/build/types/trace.d.ts +9 -0
- package/build/types/trace.d.ts.map +1 -1
- package/build/types-ts3.8/trace.d.ts +9 -0
- package/package.json +6 -6
package/build/cjs/index.js
CHANGED
|
@@ -705,6 +705,8 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
|
|
|
705
705
|
}
|
|
706
706
|
|
|
707
707
|
const existingBaggageHeader = getExistingBaggage(carrier);
|
|
708
|
+
const existingSentryTraceHeader = getExistingSentryTrace(carrier);
|
|
709
|
+
|
|
708
710
|
let baggage = api.propagation.getBaggage(context) || api.propagation.createBaggage({});
|
|
709
711
|
|
|
710
712
|
const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context);
|
|
@@ -714,12 +716,18 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
|
|
|
714
716
|
|
|
715
717
|
if (baggageEntries) {
|
|
716
718
|
Object.entries(baggageEntries).forEach(([key, value]) => {
|
|
719
|
+
if (!existingSentryTraceHeader && key.startsWith(core.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(`${core.SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
|
|
@@ -729,7 +737,7 @@ class SentryPropagator extends core$1.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 !== api.INVALID_TRACEID) {
|
|
740
|
+
if (!existingSentryTraceHeader && traceId && traceId !== api.INVALID_TRACEID) {
|
|
733
741
|
setter.set(carrier, SENTRY_TRACE_HEADER, core.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 api.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 core.handleCallbackErrors(
|
|
993
1011
|
() => callback(span),
|
|
994
1012
|
() => {
|
|
@@ -1062,7 +1080,9 @@ function startInactiveSpan(options) {
|
|
|
1062
1080
|
ctx = core$1.isTracingSuppressed(ctx) ? ctx : core$1.suppressTracing(ctx);
|
|
1063
1081
|
}
|
|
1064
1082
|
|
|
1065
|
-
|
|
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 = api.trace.getSpan(ctx);
|
|
@@ -1183,6 +1214,36 @@ function continueTrace(options, callback) {
|
|
|
1183
1214
|
return continueTraceAsRemoteSpan(api.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 = core.generateTraceId();
|
|
1227
|
+
const spanId = core.generateSpanId();
|
|
1228
|
+
|
|
1229
|
+
const spanContext = {
|
|
1230
|
+
traceId,
|
|
1231
|
+
spanId,
|
|
1232
|
+
isRemote: true,
|
|
1233
|
+
traceFlags: api.TraceFlags.NONE,
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
const ctxWithTrace = api.trace.setSpanContext(api.context.active(), spanContext);
|
|
1237
|
+
|
|
1238
|
+
return api.context.with(ctxWithTrace, () => {
|
|
1239
|
+
core.getCurrentScope().setPropagationContext({
|
|
1240
|
+
traceId,
|
|
1241
|
+
sampleRand: core._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 ,
|