@sentry/opentelemetry 8.9.2 → 8.10.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/cjs/index.js CHANGED
@@ -120,7 +120,7 @@ function getRequestSpanData(span) {
120
120
  return data;
121
121
  }
122
122
 
123
- function _optionalChain$9(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
123
+ function _optionalChain$8(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
124
124
 
125
125
  // Typescript complains if we do not use `...args: any[]` for the mixin, with:
126
126
  // A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
@@ -161,7 +161,7 @@ function wrapClientClass
161
161
  */
162
162
  async flush(timeout) {
163
163
  const provider = this.traceProvider;
164
- const spanProcessor = _optionalChain$9([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
164
+ const spanProcessor = _optionalChain$8([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
165
165
 
166
166
  if (spanProcessor) {
167
167
  await spanProcessor.forceFlush();
@@ -239,44 +239,25 @@ function getContextFromScope(scope) {
239
239
  return (scope )[SCOPE_CONTEXT_FIELD];
240
240
  }
241
241
 
242
- function _optionalChain$8(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
243
-
244
242
  /**
245
- * OpenTelemetry only knows about SAMPLED or NONE decision,
246
- * but for us it is important to differentiate between unset and unsampled.
247
243
  *
248
- * Both of these are identified as `traceFlags === TracegFlags.NONE`,
249
- * but we additionally look at a special trace state to differentiate between them.
244
+ * @param otelSpan Checks wheter a given OTEL Span is an http request to sentry.
245
+ * @returns boolean
250
246
  */
251
- function getSamplingDecision(spanContext) {
252
- const { traceFlags, traceState } = spanContext;
253
-
254
- const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
255
-
256
- // If trace flag is `SAMPLED`, we interpret this as sampled
257
- // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
258
- // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
259
- // to identify which it is
260
- if (traceFlags === api.TraceFlags.SAMPLED) {
261
- return true;
262
- }
263
-
264
- if (sampledNotRecording) {
247
+ function isSentryRequestSpan(span) {
248
+ if (!spanHasAttributes(span)) {
265
249
  return false;
266
250
  }
267
251
 
268
- // Fall back to DSC as a last resort, that may also contain `sampled`...
269
- const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
270
- const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
252
+ const { attributes } = span;
271
253
 
272
- if (_optionalChain$8([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
273
- return true;
274
- }
275
- if (_optionalChain$8([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
254
+ const httpUrl = attributes[semanticConventions.SEMATTRS_HTTP_URL];
255
+
256
+ if (!httpUrl) {
276
257
  return false;
277
258
  }
278
259
 
279
- return undefined;
260
+ return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
280
261
  }
281
262
 
282
263
  /**
@@ -433,94 +414,84 @@ function getSanitizedUrl(
433
414
  return { urlPath: undefined, url, query, fragment, hasRoute: false };
434
415
  }
435
416
 
436
- function _optionalChain$7(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
437
417
  /**
438
- * Creates a dynamic sampling context from a span (and client and scope)
439
- *
440
- * @param span the span from which a few values like the root span name and sample rate are extracted.
441
- *
442
- * @returns a dynamic sampling context
418
+ * Setup a DSC handler on the passed client,
419
+ * ensuring that the transaction name is inferred from the span correctly.
443
420
  */
444
- function getDynamicSamplingContextFromSpan(span) {
445
- const client = core.getClient();
446
- if (!client) {
447
- return {};
448
- }
449
-
450
- const traceState = span.spanContext().traceState;
451
- const traceStateDsc = _optionalChain$7([traceState, 'optionalAccess', _ => _.get, 'call', _2 => _2(SENTRY_TRACE_STATE_DSC)]);
452
-
453
- // If the span has a DSC, we want it to take precedence
454
- const dscOnSpan = traceStateDsc ? utils.baggageHeaderToDynamicSamplingContext(traceStateDsc) : undefined;
455
-
456
- if (dscOnSpan) {
457
- return dscOnSpan;
458
- }
459
-
460
- const dsc = core.getDynamicSamplingContextFromClient(span.spanContext().traceId, client);
421
+ function enhanceDscWithOpenTelemetryRootSpanName(client) {
422
+ client.on('createDsc', (dsc, rootSpan) => {
423
+ // We want to overwrite the transaction on the DSC that is created by default in core
424
+ // The reason for this is that we want to infer the span name, not use the initial one
425
+ // Otherwise, we'll get names like "GET" instead of e.g. "GET /foo"
426
+ // `parseSpanDescription` takes the attributes of the span into account for the name
427
+ // This mutates the passed-in DSC
428
+ if (rootSpan) {
429
+ const jsonSpan = core.spanToJSON(rootSpan);
430
+ const attributes = jsonSpan.data || {};
431
+ const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
432
+
433
+ const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
434
+ if (source !== 'url' && description) {
435
+ dsc.transaction = description;
436
+ }
437
+ }
438
+ });
439
+ }
461
440
 
462
- const attributes = spanHasAttributes(span) ? span.attributes : {};
441
+ /**
442
+ * Returns the currently active span.
443
+ */
444
+ function getActiveSpan() {
445
+ return api.trace.getActiveSpan();
446
+ }
463
447
 
464
- const sampleRate = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
465
- if (sampleRate != null) {
466
- dsc.sample_rate = `${sampleRate}`;
467
- }
448
+ /**
449
+ * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
450
+ *
451
+ * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
452
+ */
453
+ const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
468
454
 
469
- // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
470
- const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
455
+ function _optionalChain$7(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
471
456
 
472
- // If the span has no name, we assume it is non-recording and want to opt out of using any description
473
- const { description } = spanHasName(span) ? parseSpanDescription(span) : { description: '' };
457
+ /**
458
+ * OpenTelemetry only knows about SAMPLED or NONE decision,
459
+ * but for us it is important to differentiate between unset and unsampled.
460
+ *
461
+ * Both of these are identified as `traceFlags === TracegFlags.NONE`,
462
+ * but we additionally look at a special trace state to differentiate between them.
463
+ */
464
+ function getSamplingDecision(spanContext) {
465
+ const { traceFlags, traceState } = spanContext;
474
466
 
475
- if (source !== 'url' && description) {
476
- dsc.transaction = description;
477
- }
467
+ const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
478
468
 
479
- const sampled = getSamplingDecision(span.spanContext());
480
- if (sampled != null) {
481
- dsc.sampled = String(sampled);
469
+ // If trace flag is `SAMPLED`, we interpret this as sampled
470
+ // If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
471
+ // For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
472
+ // to identify which it is
473
+ if (traceFlags === api.TraceFlags.SAMPLED) {
474
+ return true;
482
475
  }
483
476
 
484
- client.emit('createDsc', dsc);
485
-
486
- return dsc;
487
- }
488
-
489
- /**
490
- *
491
- * @param otelSpan Checks wheter a given OTEL Span is an http request to sentry.
492
- * @returns boolean
493
- */
494
- function isSentryRequestSpan(span) {
495
- if (!spanHasAttributes(span)) {
477
+ if (sampledNotRecording) {
496
478
  return false;
497
479
  }
498
480
 
499
- const { attributes } = span;
500
-
501
- const httpUrl = attributes[semanticConventions.SEMATTRS_HTTP_URL];
481
+ // Fall back to DSC as a last resort, that may also contain `sampled`...
482
+ const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
483
+ const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
502
484
 
503
- if (!httpUrl) {
485
+ if (_optionalChain$7([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
486
+ return true;
487
+ }
488
+ if (_optionalChain$7([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
504
489
  return false;
505
490
  }
506
491
 
507
- return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
508
- }
509
-
510
- /**
511
- * Returns the currently active span.
512
- */
513
- function getActiveSpan() {
514
- return api.trace.getActiveSpan();
492
+ return undefined;
515
493
  }
516
494
 
517
- /**
518
- * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
519
- *
520
- * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
521
- */
522
- const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
523
-
524
495
  const setupElements = new Set();
525
496
 
526
497
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -550,7 +521,7 @@ function getPropagationContextFromSpan(span) {
550
521
  const sampled = getSamplingDecision(spanContext);
551
522
 
552
523
  // No trace state? --> Take DSC from root span
553
- const dsc = traceStateDsc || getDynamicSamplingContextFromSpan(core.getRootSpan(span));
524
+ const dsc = traceStateDsc || core.getDynamicSamplingContextFromSpan(core.getRootSpan(span));
554
525
 
555
526
  return {
556
527
  traceId,
@@ -1040,7 +1011,7 @@ function getContext(scope, forceTransaction) {
1040
1011
  // In this case, when we are forcing a transaction, we want to treat this like continuing an incoming trace
1041
1012
  // so we set the traceState according to the root span
1042
1013
  const rootSpan = core.getRootSpan(parentSpan);
1043
- const dsc = getDynamicSamplingContextFromSpan(rootSpan);
1014
+ const dsc = core.getDynamicSamplingContextFromSpan(rootSpan);
1044
1015
 
1045
1016
  const traceState = makeTraceState({
1046
1017
  dsc,
@@ -1096,7 +1067,6 @@ function suppressTracing(callback) {
1096
1067
  }
1097
1068
 
1098
1069
  function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1099
-
1100
1070
  /** Ensure the `trace` context is set on all events. */
1101
1071
  function setupEventContextTrace(client) {
1102
1072
  client.on('preprocessEvent', event => {
@@ -1132,7 +1102,7 @@ function setupEventContextTrace(client) {
1132
1102
  const rootSpan = core.getRootSpan(span);
1133
1103
 
1134
1104
  event.sdkProcessingMetadata = {
1135
- dynamicSamplingContext: getDynamicSamplingContextFromSpan(rootSpan),
1105
+ dynamicSamplingContext: core.getDynamicSamplingContextFromSpan(rootSpan),
1136
1106
  ...event.sdkProcessingMetadata,
1137
1107
  };
1138
1108
 
@@ -1419,7 +1389,14 @@ function mapStatus(span) {
1419
1389
  return { code: core.SPAN_STATUS_OK };
1420
1390
  // If the span is already marked as erroneous we return that exact status
1421
1391
  } else if (status.code === api.SpanStatusCode.ERROR) {
1422
- if (typeof status.message === 'undefined' || isStatusErrorMessageValid(status.message)) {
1392
+ if (typeof status.message === 'undefined') {
1393
+ const inferredStatus = inferStatusFromAttributes(attributes);
1394
+ if (inferredStatus) {
1395
+ return inferredStatus;
1396
+ }
1397
+ }
1398
+
1399
+ if (status.message && isStatusErrorMessageValid(status.message)) {
1423
1400
  return { code: core.SPAN_STATUS_ERROR, message: status.message };
1424
1401
  } else {
1425
1402
  return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
@@ -1427,6 +1404,22 @@ function mapStatus(span) {
1427
1404
  }
1428
1405
  }
1429
1406
 
1407
+ // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1408
+ const inferredStatus = inferStatusFromAttributes(attributes);
1409
+
1410
+ if (inferredStatus) {
1411
+ return inferredStatus;
1412
+ }
1413
+
1414
+ // We default to setting the spans status to ok.
1415
+ if (status && status.code === api.SpanStatusCode.UNSET) {
1416
+ return { code: core.SPAN_STATUS_OK };
1417
+ } else {
1418
+ return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
1419
+ }
1420
+ }
1421
+
1422
+ function inferStatusFromAttributes(attributes) {
1430
1423
  // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1431
1424
 
1432
1425
  const httpCodeAttribute = attributes[semanticConventions.SEMATTRS_HTTP_STATUS_CODE];
@@ -1439,7 +1432,7 @@ function mapStatus(span) {
1439
1432
  ? parseInt(httpCodeAttribute)
1440
1433
  : undefined;
1441
1434
 
1442
- if (numberHttpCode) {
1435
+ if (typeof numberHttpCode === 'number') {
1443
1436
  return core.getSpanStatusFromHttpCode(numberHttpCode);
1444
1437
  }
1445
1438
 
@@ -1447,12 +1440,7 @@ function mapStatus(span) {
1447
1440
  return { code: core.SPAN_STATUS_ERROR, message: canonicalGrpcErrorCodesMap[grpcCodeAttribute] || 'unknown_error' };
1448
1441
  }
1449
1442
 
1450
- // We default to setting the spans status to ok.
1451
- if (status && status.code === api.SpanStatusCode.UNSET) {
1452
- return { code: core.SPAN_STATUS_OK };
1453
- } else {
1454
- return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
1455
- }
1443
+ return undefined;
1456
1444
  }
1457
1445
 
1458
1446
  function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
@@ -1665,7 +1653,7 @@ function createTransactionForOtelSpan(span) {
1665
1653
  capturedSpanScope: capturedSpanScopes.scope,
1666
1654
  capturedSpanIsolationScope: capturedSpanScopes.isolationScope,
1667
1655
  sampleRate,
1668
- dynamicSamplingContext: getDynamicSamplingContextFromSpan(span),
1656
+ dynamicSamplingContext: core.getDynamicSamplingContextFromSpan(span ),
1669
1657
  }),
1670
1658
  },
1671
1659
  ...(source && {
@@ -2027,13 +2015,14 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2027
2015
 
2028
2016
  exports.getClient = core.getClient;
2029
2017
  exports.getCurrentHubShim = core.getCurrentHubShim;
2018
+ exports.getDynamicSamplingContextFromSpan = core.getDynamicSamplingContextFromSpan;
2030
2019
  exports.SentryPropagator = SentryPropagator;
2031
2020
  exports.SentrySampler = SentrySampler;
2032
2021
  exports.SentrySpanProcessor = SentrySpanProcessor;
2033
2022
  exports.addOpenTelemetryInstrumentation = addOpenTelemetryInstrumentation;
2034
2023
  exports.continueTrace = continueTrace;
2024
+ exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
2035
2025
  exports.getActiveSpan = getActiveSpan;
2036
- exports.getDynamicSamplingContextFromSpan = getDynamicSamplingContextFromSpan;
2037
2026
  exports.getRequestSpanData = getRequestSpanData;
2038
2027
  exports.getScopesFromContext = getScopesFromContext;
2039
2028
  exports.getSpanKind = getSpanKind;