@sentry/opentelemetry 11.0.0-alpha.1 → 11.0.0-alpha.2

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.
@@ -170,33 +170,10 @@ function generateRemoteSpanContext({
170
170
  }
171
171
 
172
172
  const SUPPRESS_TRACING_KEY = api.createContextKey("OpenTelemetry SDK Context Key SUPPRESS_TRACING");
173
- function suppressTracing(context) {
174
- return context.setValue(SUPPRESS_TRACING_KEY, true);
175
- }
176
173
  function isTracingSuppressed(context) {
177
174
  return context.getValue(SUPPRESS_TRACING_KEY) === true;
178
175
  }
179
176
 
180
- function getSamplingDecision(spanContext) {
181
- const { traceFlags, traceState } = spanContext;
182
- const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === "1" : false;
183
- if (traceFlags === api.TraceFlags.SAMPLED) {
184
- return true;
185
- }
186
- if (sampledNotRecording) {
187
- return false;
188
- }
189
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : void 0;
190
- const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : void 0;
191
- if (dsc?.sampled === "true") {
192
- return true;
193
- }
194
- if (dsc?.sampled === "false") {
195
- return false;
196
- }
197
- return void 0;
198
- }
199
-
200
177
  class SentryTracer {
201
178
  /** @inheritdoc */
202
179
  startSpan(name, options = {}, ctx) {
@@ -212,19 +189,38 @@ class SentryTracer {
212
189
  }
213
190
  startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
214
191
  const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
215
- const ctx = typeof contextOrFn === "function" || contextOrFn === void 0 ? api.context.active() : contextOrFn;
192
+ const explicitCtx = typeof contextOrFn === "function" || contextOrFn === void 0 ? void 0 : contextOrFn;
193
+ const ctx = explicitCtx ?? api.context.active();
216
194
  const callback = typeof optionsOrFn === "function" ? optionsOrFn : typeof contextOrFn === "function" ? contextOrFn : fn;
217
- const span = this.startSpan(name, options, ctx);
195
+ const span = this.startSpan(name, options, explicitCtx);
218
196
  const capturedIsolationScope = core.getCapturedScopesOnSpan(span).isolationScope;
219
197
  const withCapturedIsolationScope = (contextToFork) => capturedIsolationScope ? contextToFork.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, capturedIsolationScope) : contextToFork;
220
- if (core.spanIsIgnored(span) && api.trace.getSpan(ctx)) {
198
+ if (core.spanIsIgnored(span) && this._hasParentSpan(options, explicitCtx)) {
221
199
  return api.context.with(withCapturedIsolationScope(ctx), () => callback(span));
222
200
  }
223
201
  return api.context.with(withCapturedIsolationScope(api.trace.setSpan(ctx, span)), () => {
202
+ if (api.trace.getSpan(api.context.active()) !== span) {
203
+ return core.withScope((scope) => {
204
+ core._INTERNAL_setSpanForScope(scope, span);
205
+ return callback(span);
206
+ });
207
+ }
224
208
  core._INTERNAL_setSpanForScope(core.getCurrentScope(), span);
225
209
  return callback(span);
226
210
  });
227
211
  }
212
+ /**
213
+ * Whether a span started with these arguments gets a parent. Mirrors the parent lookup in `startSpan`
214
+ * plus core's fallback to the scope's active span, which is what parents the span on runtimes without an
215
+ * OTel context manager.
216
+ */
217
+ _hasParentSpan(options, explicitCtx) {
218
+ if (options.root) {
219
+ return false;
220
+ }
221
+ const parentSpan = explicitCtx ? api.trace.getSpan(explicitCtx) : core.getActiveSpan();
222
+ return !!parentSpan && api.isSpanContextValid(parentSpan.spanContext());
223
+ }
228
224
  _startSentrySpan(name, options, parentSpan, hasExplicitContext) {
229
225
  const sentryOptions = {
230
226
  name,
@@ -236,36 +232,16 @@ class SentryTracer {
236
232
  sentryOptions.attributes[attributes.SENTRY_KIND] = core.spanKindToName(options.kind);
237
233
  }
238
234
  if (options.root) {
239
- return core.startNewTrace(() => core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan: null }));
240
- }
241
- if (parentSpan?.spanContext().isRemote) {
242
- return this._startRootSpanWithRemoteParent(sentryOptions, parentSpan);
235
+ return core.startNewTrace(() => core.startInactiveSpan({ ...sentryOptions, parentSpan: null }));
243
236
  }
244
237
  if (parentSpan) {
245
- return core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan });
238
+ return core.startInactiveSpan({ ...sentryOptions, parentSpan });
246
239
  }
247
- return core._INTERNAL_startInactiveSpan({
240
+ return core.startInactiveSpan({
248
241
  ...sentryOptions,
249
242
  parentSpan: hasExplicitContext ? null : void 0
250
243
  });
251
244
  }
252
- _startRootSpanWithRemoteParent(options, parentSpan) {
253
- const { spanId, traceId, traceState } = parentSpan.spanContext();
254
- const dsc = core.getDynamicSamplingContextFromSpan(parentSpan);
255
- const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
256
- const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
257
- return core.withScope((scope) => {
258
- scope.setPropagationContext({
259
- traceId,
260
- parentSpanId: spanId,
261
- sampled: getSamplingDecision(parentSpan.spanContext()),
262
- dsc: hasIncomingDsc ? dsc : void 0,
263
- sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : core._INTERNAL_safeMathRandom()
264
- });
265
- core._INTERNAL_setSpanForScope(scope, void 0);
266
- return core._INTERNAL_startInactiveSpan({ ...options, parentSpan: null });
267
- });
268
- }
269
245
  _createNonRecordingSpan(parentSpan) {
270
246
  const traceId = parentSpan?.spanContext().traceId ?? core.getCurrentScope().getPropagationContext().traceId;
271
247
  const span = new core.SentryNonRecordingSpan({ traceId });
@@ -302,174 +278,22 @@ class SentryTracerProvider {
302
278
  }
303
279
  }
304
280
 
305
- function reconcileDscSampled(dsc, sampled) {
306
- const reconciled = { ...dsc };
307
- if (sampled === void 0) {
308
- delete reconciled.sampled;
309
- } else {
310
- reconciled.sampled = String(sampled);
311
- if (sampled === false) {
312
- delete reconciled.transaction;
313
- }
314
- }
315
- return reconciled;
316
- }
317
-
318
- function _startSpan(options, callback, autoEnd) {
319
- const tracer = getTracer();
320
- const { name, parentSpan: customParentSpan } = options;
321
- const wrapper = getActiveSpanWrapper(customParentSpan);
322
- return wrapper(() => {
323
- const activeCtx = getContext(options.scope, options.forceTransaction);
324
- const missingRequiredParent = options.onlyIfParent && !api.trace.getSpan(activeCtx);
325
- const ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
326
- if (missingRequiredParent) {
327
- core.getClient()?.recordDroppedEvent("no_parent_span", "span");
328
- }
329
- const spanOptions = getSpanOptions(options);
330
- if (!core.hasSpansEnabled()) {
331
- const suppressedCtx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
332
- return api.context.with(suppressedCtx, () => {
333
- return tracer.startActiveSpan(name, spanOptions, suppressedCtx, (span) => {
334
- patchSpanEnd(span);
335
- return api.context.with(api.trace.setSpan(activeCtx, span), () => {
336
- return core.handleCallbackErrors(
337
- () => callback(span),
338
- () => {
339
- if (core.spanToStaticSpanJSON(span).status === "ok") {
340
- span.setStatus({ code: api.SpanStatusCode.ERROR });
341
- }
342
- },
343
- autoEnd ? () => span.end() : void 0
344
- );
345
- });
346
- });
347
- });
348
- }
349
- return tracer.startActiveSpan(name, spanOptions, ctx, (span) => {
350
- patchSpanEnd(span);
351
- return core.handleCallbackErrors(
352
- () => callback(span),
353
- () => {
354
- if (core.spanToStaticSpanJSON(span).status === "ok") {
355
- span.setStatus({ code: api.SpanStatusCode.ERROR });
356
- }
357
- },
358
- autoEnd ? () => span.end() : void 0
359
- );
360
- });
361
- });
362
- }
363
- function startSpan(options, callback) {
364
- return _startSpan(options, callback, true);
365
- }
366
- function startSpanManual(options, callback) {
367
- return _startSpan(options, (span) => callback(span, () => span.end()), false);
368
- }
369
- function startInactiveSpan(options) {
370
- const tracer = getTracer();
371
- const { name, parentSpan: customParentSpan } = options;
372
- const wrapper = getActiveSpanWrapper(customParentSpan);
373
- return wrapper(() => {
374
- const activeCtx = getContext(options.scope, options.forceTransaction);
375
- const missingRequiredParent = options.onlyIfParent && !api.trace.getSpan(activeCtx);
376
- let ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
377
- if (missingRequiredParent) {
378
- core.getClient()?.recordDroppedEvent("no_parent_span", "span");
379
- }
380
- const spanOptions = getSpanOptions(options);
381
- if (!core.hasSpansEnabled()) {
382
- ctx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
383
- }
384
- const span = tracer.startSpan(name, spanOptions, ctx);
385
- patchSpanEnd(span);
386
- return span;
387
- });
388
- }
389
281
  function withActiveSpan(span, callback) {
390
282
  const newContextWithActiveSpan = span ? api.trace.setSpan(api.context.active(), span) : api.trace.deleteSpan(api.context.active());
391
- return api.context.with(newContextWithActiveSpan, () => callback(core.getCurrentScope()));
392
- }
393
- function getTracer() {
394
- const client = core.getClient();
395
- return client?.tracer || api.trace.getTracer("@sentry/opentelemetry", core.SDK_VERSION);
396
- }
397
- function getSpanOptions(options) {
398
- const { startTime, attributes, op, links } = options;
399
- const fixedStartTime = typeof startTime === "number" ? ensureTimestampInMilliseconds(startTime) : startTime;
400
- return {
401
- attributes: op ? {
402
- [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
403
- ...attributes
404
- } : attributes,
405
- links,
406
- startTime: fixedStartTime
407
- };
408
- }
409
- function ensureTimestampInMilliseconds(timestamp) {
410
- const isMs = timestamp < 9999999999;
411
- return isMs ? timestamp * 1e3 : timestamp;
412
- }
413
- function patchSpanEnd(span) {
414
- const originalEnd = span.end.bind(span);
415
- span.end = (endTime) => {
416
- return originalEnd(typeof endTime === "number" ? ensureTimestampInMilliseconds(endTime) : endTime);
417
- };
418
- }
419
- function getContext(scope, forceTransaction) {
420
- const ctx = getContextForScope(scope);
421
- const parentSpan = api.trace.getSpan(ctx);
422
- if (!parentSpan) {
423
- return ctx;
424
- }
425
- if (!forceTransaction) {
426
- return ctx;
427
- }
428
- const ctxWithoutSpan = api.trace.deleteSpan(ctx);
429
- const { spanId, traceId } = parentSpan.spanContext();
430
- const sampled = getSamplingDecision(parentSpan.spanContext());
431
- const rootSpan = core.getRootSpan(parentSpan);
432
- const rawDsc = core.getDynamicSamplingContextFromSpan(rootSpan);
433
- const hasIncomingFrozenDsc = !!rootSpan.spanContext().traceState?.get(SENTRY_TRACE_STATE_DSC);
434
- const dsc = hasIncomingFrozenDsc ? rawDsc : reconcileDscSampled(rawDsc, sampled);
435
- const traceState = makeTraceState({
436
- dsc,
437
- sampled
283
+ return api.context.with(newContextWithActiveSpan, () => {
284
+ const scope = core.getCurrentScope();
285
+ core._INTERNAL_setSpanForScope(scope, span ?? void 0);
286
+ return callback(scope);
438
287
  });
439
- const spanOptions = {
440
- traceId,
441
- spanId,
442
- isRemote: true,
443
- traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
444
- traceState
445
- };
446
- const ctxWithSpanContext = api.trace.setSpanContext(ctxWithoutSpan, spanOptions);
447
- return ctxWithSpanContext;
448
- }
449
- function getContextForScope(scope) {
450
- if (scope) {
451
- const ctx = getContextFromScope(scope);
452
- if (ctx) {
453
- return ctx;
454
- }
455
- }
456
- return api.context.active();
457
- }
458
- function getActiveSpanWrapper(parentSpan) {
459
- return parentSpan !== void 0 ? (callback) => {
460
- return withActiveSpan(parentSpan, callback);
461
- } : (callback) => callback();
462
288
  }
463
289
 
464
290
  function getActiveSpan(scope) {
465
- if (!scope) {
466
- return api.trace.getActiveSpan();
467
- }
291
+ const span = scope ? getSpanFromScope(scope) : api.trace.getActiveSpan();
292
+ return span && api.isSpanContextValid(span.spanContext()) ? span : void 0;
293
+ }
294
+ function getSpanFromScope(scope) {
468
295
  const ctx = getContextFromScope(scope);
469
- if (ctx) {
470
- return api.trace.getSpan(ctx);
471
- }
472
- return void 0;
296
+ return ctx ? api.trace.getSpan(ctx) : void 0;
473
297
  }
474
298
 
475
299
  function buildContextWithSentryScopes(context) {
@@ -717,9 +541,6 @@ function setOpenTelemetryContextAsyncContextStrategy() {
717
541
  withIsolationScope,
718
542
  getCurrentScope,
719
543
  getIsolationScope,
720
- startSpan,
721
- startSpanManual,
722
- startInactiveSpan,
723
544
  getActiveSpan,
724
545
  // The types here don't fully align, because our own `Span` type is narrower
725
546
  // than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
@@ -731,8 +552,53 @@ function setOpenTelemetryContextAsyncContextStrategy() {
731
552
  return ctxManager.getAsyncLocalStorageLookup();
732
553
  }
733
554
 
555
+ function getSamplingDecision(spanContext) {
556
+ const { traceFlags, traceState } = spanContext;
557
+ const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === "1" : false;
558
+ if (traceFlags === api.TraceFlags.SAMPLED) {
559
+ return true;
560
+ }
561
+ if (sampledNotRecording) {
562
+ return false;
563
+ }
564
+ const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : void 0;
565
+ const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : void 0;
566
+ if (dsc?.sampled === "true") {
567
+ return true;
568
+ }
569
+ if (dsc?.sampled === "false") {
570
+ return false;
571
+ }
572
+ return void 0;
573
+ }
574
+
575
+ function registerPrepareSpanScope(client) {
576
+ client.on("prepareSpanScope", (spanScope) => {
577
+ const { scope, parentSpan } = spanScope;
578
+ if (!parentSpan?.spanContext().isRemote) {
579
+ return;
580
+ }
581
+ const { spanId, traceId, traceState } = parentSpan.spanContext();
582
+ const dsc = core.getDynamicSamplingContextFromSpan(parentSpan);
583
+ const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
584
+ const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
585
+ const forkedScope = scope.clone();
586
+ forkedScope.setPropagationContext({
587
+ traceId,
588
+ parentSpanId: spanId,
589
+ sampled: getSamplingDecision(parentSpan.spanContext()),
590
+ dsc: hasIncomingDsc ? dsc : void 0,
591
+ sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : core._INTERNAL_safeMathRandom()
592
+ });
593
+ core._INTERNAL_setSpanForScope(forkedScope, void 0);
594
+ spanScope.scope = forkedScope;
595
+ spanScope.parentSpan = void 0;
596
+ });
597
+ }
598
+
734
599
  exports.SentryPropagator = SentryPropagator;
735
600
  exports.SentryTracerProvider = SentryTracerProvider;
736
601
  exports.getScopesFromContext = getScopesFromContext;
602
+ exports.registerPrepareSpanScope = registerPrepareSpanScope;
737
603
  exports.setOpenTelemetryContextAsyncContextStrategy = setOpenTelemetryContextAsyncContextStrategy;
738
604
  //# sourceMappingURL=index.js.map