@sentry/opentelemetry 10.47.0 → 10.48.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.
@@ -215,6 +215,20 @@ const SENTRY_TRACE_STATE_URL = 'sentry.url';
215
215
  const SENTRY_TRACE_STATE_SAMPLE_RAND = 'sentry.sample_rand';
216
216
  const SENTRY_TRACE_STATE_SAMPLE_RATE = 'sentry.sample_rate';
217
217
 
218
+ /**
219
+ * A flag marking a context as ignored because the span associated with the context
220
+ * is ignored (`ignoreSpans` filter).
221
+ */
222
+ const SENTRY_TRACE_STATE_CHILD_IGNORED = 'sentry.ignored';
223
+
224
+ /**
225
+ * A flag marking a segment span as ignored because it matched the `ignoreSpans` filter.
226
+ * Unlike `SENTRY_TRACE_STATE_CHILD_IGNORED` (used for child spans), this flag is NOT consumed
227
+ * by the context manager for re-parenting. Instead, it propagates to child spans so they
228
+ * can record the correct client report outcome (`ignored` instead of `sample_rate`).
229
+ */
230
+ const SENTRY_TRACE_STATE_SEGMENT_IGNORED = 'sentry.segment_ignored';
231
+
218
232
  const SENTRY_SCOPES_CONTEXT_KEY = api.createContextKey('sentry_scopes');
219
233
 
220
234
  const SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY = api.createContextKey('sentry_fork_isolation_scope');
@@ -1469,20 +1483,36 @@ function wrapContextManagerClass(
1469
1483
  thisArg,
1470
1484
  ...args
1471
1485
  ) {
1472
- const currentScopes = getScopesFromContext(context);
1486
+ // Remove ignored spans from context and restore the parent span so children
1487
+ // naturally parent to the grandparent instead of starting a new trace.
1488
+ // At this point, this.active() still holds the outer context (before super.with()
1489
+ // updates AsyncLocalStorage), which has the grandparent span we want to restore.
1490
+ const span = api.trace.getSpan(context);
1491
+ let effectiveContext;
1492
+ if (span?.spanContext().traceState?.get(SENTRY_TRACE_STATE_CHILD_IGNORED) === '1') {
1493
+ const contextWithoutSpan = api.trace.deleteSpan(context);
1494
+ const parentSpan = api.trace.getSpan(this.active());
1495
+ effectiveContext = parentSpan ? api.trace.setSpan(contextWithoutSpan, parentSpan) : contextWithoutSpan;
1496
+ } else {
1497
+ effectiveContext = context;
1498
+ }
1499
+
1500
+ const currentScopes = getScopesFromContext(effectiveContext);
1473
1501
  const currentScope = currentScopes?.scope || core.getCurrentScope();
1474
1502
  const currentIsolationScope = currentScopes?.isolationScope || core.getIsolationScope();
1475
1503
 
1476
- const shouldForkIsolationScope = context.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
1477
- const scope = context.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) ;
1478
- const isolationScope = context.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY) ;
1504
+ const shouldForkIsolationScope = effectiveContext.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
1505
+ const scope = effectiveContext.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) ;
1506
+ const isolationScope = effectiveContext.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY)
1507
+
1508
+ ;
1479
1509
 
1480
1510
  const newCurrentScope = scope || currentScope.clone();
1481
1511
  const newIsolationScope =
1482
1512
  isolationScope || (shouldForkIsolationScope ? currentIsolationScope.clone() : currentIsolationScope);
1483
1513
  const scopes = { scope: newCurrentScope, isolationScope: newIsolationScope };
1484
1514
 
1485
- const ctx1 = setScopesOnContext(context, scopes);
1515
+ const ctx1 = setScopesOnContext(effectiveContext, scopes);
1486
1516
 
1487
1517
  // Remove the unneeded values again
1488
1518
  const ctx2 = ctx1
@@ -2078,49 +2108,6 @@ function getData(span) {
2078
2108
  return data;
2079
2109
  }
2080
2110
 
2081
- function onSpanStart(span, parentContext) {
2082
- // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
2083
- const parentSpan = api.trace.getSpan(parentContext);
2084
-
2085
- let scopes = getScopesFromContext(parentContext);
2086
-
2087
- // We need access to the parent span in order to be able to move up the span tree for breadcrumbs
2088
- if (parentSpan && !parentSpan.spanContext().isRemote) {
2089
- core.addChildSpanToSpan(parentSpan, span);
2090
- }
2091
-
2092
- // We need this in the span exporter
2093
- if (parentSpan?.spanContext().isRemote) {
2094
- span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE, true);
2095
- }
2096
-
2097
- // The root context does not have scopes stored, so we check for this specifically
2098
- // As fallback we attach the global scopes
2099
- if (parentContext === api.ROOT_CONTEXT) {
2100
- scopes = {
2101
- scope: core.getDefaultCurrentScope(),
2102
- isolationScope: core.getDefaultIsolationScope(),
2103
- };
2104
- }
2105
-
2106
- // We need the scope at time of span creation in order to apply it to the event when the span is finished
2107
- if (scopes) {
2108
- core.setCapturedScopesOnSpan(span, scopes.scope, scopes.isolationScope);
2109
- }
2110
-
2111
- core.logSpanStart(span);
2112
-
2113
- const client = core.getClient();
2114
- client?.emit('spanStart', span);
2115
- }
2116
-
2117
- function onSpanEnd(span) {
2118
- core.logSpanEnd(span);
2119
-
2120
- const client = core.getClient();
2121
- client?.emit('spanEnd', span);
2122
- }
2123
-
2124
2111
  /**
2125
2112
  * Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
2126
2113
  * the Sentry SDK.
@@ -2150,14 +2137,53 @@ class SentrySpanProcessor {
2150
2137
  * @inheritDoc
2151
2138
  */
2152
2139
  onStart(span, parentContext) {
2153
- onSpanStart(span, parentContext);
2140
+ // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
2141
+ const parentSpan = api.trace.getSpan(parentContext);
2142
+
2143
+ let scopes = getScopesFromContext(parentContext);
2144
+
2145
+ // We need access to the parent span in order to be able to move up the span tree for breadcrumbs
2146
+ if (parentSpan && !parentSpan.spanContext().isRemote) {
2147
+ core.addChildSpanToSpan(parentSpan, span);
2148
+ }
2149
+
2150
+ // We need this in the span exporter
2151
+ if (parentSpan?.spanContext().isRemote) {
2152
+ span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE, true);
2153
+ }
2154
+
2155
+ // The root context does not have scopes stored, so we check for this specifically
2156
+ // As fallback we attach the global scopes
2157
+ if (parentContext === api.ROOT_CONTEXT) {
2158
+ scopes = {
2159
+ scope: core.getDefaultCurrentScope(),
2160
+ isolationScope: core.getDefaultIsolationScope(),
2161
+ };
2162
+ }
2163
+
2164
+ // We need the scope at time of span creation in order to apply it to the event when the span is finished
2165
+ if (scopes) {
2166
+ core.setCapturedScopesOnSpan(span, scopes.scope, scopes.isolationScope);
2167
+ }
2168
+
2169
+ core.logSpanStart(span);
2170
+
2171
+ const client = core.getClient();
2172
+ client?.emit('spanStart', span);
2154
2173
  }
2155
2174
 
2156
2175
  /** @inheritDoc */
2157
2176
  onEnd(span) {
2158
- onSpanEnd(span);
2177
+ core.logSpanEnd(span);
2159
2178
 
2160
- this._exporter.export(span);
2179
+ const client = core.getClient();
2180
+ client?.emit('spanEnd', span);
2181
+
2182
+ if (client && core.hasSpanStreamingEnabled(client)) {
2183
+ client.emit('afterSpanEnd', span);
2184
+ } else {
2185
+ this._exporter.export(span);
2186
+ }
2161
2187
  }
2162
2188
  }
2163
2189
 
@@ -2168,6 +2194,7 @@ class SentrySampler {
2168
2194
 
2169
2195
  constructor(client) {
2170
2196
  this._client = client;
2197
+ this._isSpanStreaming = core.hasSpanStreamingEnabled(client);
2171
2198
  setIsSetup('SentrySampler');
2172
2199
  }
2173
2200
 
@@ -2181,6 +2208,7 @@ class SentrySampler {
2181
2208
  _links,
2182
2209
  ) {
2183
2210
  const options = this._client.getOptions();
2211
+ const { ignoreSpans } = options;
2184
2212
 
2185
2213
  const parentSpan = getValidSpan(context);
2186
2214
  const parentContext = parentSpan?.spanContext();
@@ -2205,6 +2233,36 @@ class SentrySampler {
2205
2233
  // We only sample based on parameters (like tracesSampleRate or tracesSampler) for root spans (which is done in sampleSpan).
2206
2234
  // Non-root-spans simply inherit the sampling decision from their parent.
2207
2235
  if (!isRootSpan) {
2236
+ if (this._isSpanStreaming) {
2237
+ // `ignoreSpans` is only applied at span start for streamed spans.
2238
+ // Static spans/transactions get filtered at transaction end.
2239
+ // Likewise, we only record client outcomes for child spans when streaming
2240
+ if (parentSampled) {
2241
+ if (ignoreSpans?.length) {
2242
+ const { description: inferredChildName, op: childOp } = inferSpanData(spanName, spanAttributes, spanKind);
2243
+ if (
2244
+ core.shouldIgnoreSpan(
2245
+ { description: inferredChildName, op: spanAttributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] ?? childOp },
2246
+ ignoreSpans,
2247
+ )
2248
+ ) {
2249
+ this._client.recordDroppedEvent('ignored', 'span');
2250
+ return wrapSamplingDecision({
2251
+ decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
2252
+ context,
2253
+ spanAttributes,
2254
+ ignoredChildSpan: true,
2255
+ });
2256
+ }
2257
+ }
2258
+ }
2259
+
2260
+ if (!parentSampled) {
2261
+ const parentSegmentIgnored = parentContext?.traceState?.get(SENTRY_TRACE_STATE_SEGMENT_IGNORED) === '1';
2262
+ this._client.recordDroppedEvent(parentSegmentIgnored ? 'ignored' : 'sample_rate', 'span');
2263
+ }
2264
+ }
2265
+
2208
2266
  return wrapSamplingDecision({
2209
2267
  decision: parentSampled ? sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED : sdkTraceBase.SamplingDecision.NOT_RECORD,
2210
2268
  context,
@@ -2228,6 +2286,23 @@ class SentrySampler {
2228
2286
  mergedAttributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
2229
2287
  }
2230
2288
 
2289
+ if (
2290
+ this._isSpanStreaming &&
2291
+ ignoreSpans?.length &&
2292
+ core.shouldIgnoreSpan(
2293
+ { description: inferredSpanName, op: mergedAttributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] ?? op },
2294
+ ignoreSpans,
2295
+ )
2296
+ ) {
2297
+ this._client.recordDroppedEvent('ignored', 'span');
2298
+ return wrapSamplingDecision({
2299
+ decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
2300
+ context,
2301
+ spanAttributes,
2302
+ ignoredSegmentSpan: true,
2303
+ });
2304
+ }
2305
+
2231
2306
  const mutableSamplingDecision = { decision: true };
2232
2307
  this._client.emit(
2233
2308
  'beforeSampling',
@@ -2281,7 +2356,7 @@ class SentrySampler {
2281
2356
  parentSampled === undefined
2282
2357
  ) {
2283
2358
  DEBUG_BUILD && core.debug.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
2284
- this._client.recordDroppedEvent('sample_rate', 'transaction');
2359
+ this._client.recordDroppedEvent('sample_rate', this._isSpanStreaming ? 'span' : 'transaction');
2285
2360
  }
2286
2361
 
2287
2362
  return {
@@ -2337,6 +2412,8 @@ function wrapSamplingDecision({
2337
2412
  spanAttributes,
2338
2413
  sampleRand,
2339
2414
  downstreamTraceSampleRate,
2415
+ ignoredChildSpan,
2416
+ ignoredSegmentSpan,
2340
2417
  }
2341
2418
 
2342
2419
  ) {
@@ -2354,6 +2431,14 @@ function wrapSamplingDecision({
2354
2431
  traceState = traceState.set(SENTRY_TRACE_STATE_SAMPLE_RAND, `${sampleRand}`);
2355
2432
  }
2356
2433
 
2434
+ if (ignoredChildSpan) {
2435
+ traceState = traceState.set(SENTRY_TRACE_STATE_CHILD_IGNORED, '1');
2436
+ }
2437
+
2438
+ if (ignoredSegmentSpan) {
2439
+ traceState = traceState.set(SENTRY_TRACE_STATE_SEGMENT_IGNORED, '1');
2440
+ }
2441
+
2357
2442
  // If the decision is undefined, we treat it as NOT_RECORDING, but we don't propagate this decision to downstream SDKs
2358
2443
  // Which is done by not setting `SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING` traceState
2359
2444
  if (decision == undefined) {
@@ -2396,6 +2481,7 @@ function getValidSpan(context) {
2396
2481
  exports.getClient = core.getClient;
2397
2482
  exports.getDynamicSamplingContextFromSpan = core.getDynamicSamplingContextFromSpan;
2398
2483
  exports.shouldPropagateTraceForUrl = core.shouldPropagateTraceForUrl;
2484
+ exports.withStreamedSpan = core.withStreamedSpan;
2399
2485
  exports.SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION = SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION;
2400
2486
  exports.SentryPropagator = SentryPropagator;
2401
2487
  exports.SentrySampler = SentrySampler;