@sentry/opentelemetry 8.46.0 → 8.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.
@@ -316,12 +316,12 @@ function getSamplingDecision(spanContext) {
316
316
  /**
317
317
  * Infer the op & description for a set of name, attributes and kind of a span.
318
318
  */
319
- function inferSpanData(name, attributes, kind) {
319
+ function inferSpanData(spanName, attributes, kind) {
320
320
  // if http.method exists, this is an http request span
321
321
  // eslint-disable-next-line deprecation/deprecation
322
322
  const httpMethod = attributes[semanticConventions.ATTR_HTTP_REQUEST_METHOD] || attributes[semanticConventions.SEMATTRS_HTTP_METHOD];
323
323
  if (httpMethod) {
324
- return descriptionForHttpMethod({ attributes, name, kind }, httpMethod);
324
+ return descriptionForHttpMethod({ attributes, name: spanName, kind }, httpMethod);
325
325
  }
326
326
 
327
327
  // eslint-disable-next-line deprecation/deprecation
@@ -333,17 +333,18 @@ function inferSpanData(name, attributes, kind) {
333
333
  // If db.type exists then this is a database call span
334
334
  // If the Redis DB is used as a cache, the span description should not be changed
335
335
  if (dbSystem && !opIsCache) {
336
- return descriptionForDbSystem({ attributes, name });
336
+ return descriptionForDbSystem({ attributes, name: spanName });
337
337
  }
338
338
 
339
+ const customSourceOrRoute = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom' ? 'custom' : 'route';
340
+
339
341
  // If rpc.service exists then this is a rpc call span.
340
342
  // eslint-disable-next-line deprecation/deprecation
341
343
  const rpcService = attributes[semanticConventions.SEMATTRS_RPC_SERVICE];
342
344
  if (rpcService) {
343
345
  return {
346
+ ...getUserUpdatedNameAndSource(spanName, attributes, 'route'),
344
347
  op: 'rpc',
345
- description: name,
346
- source: 'route',
347
348
  };
348
349
  }
349
350
 
@@ -352,9 +353,8 @@ function inferSpanData(name, attributes, kind) {
352
353
  const messagingSystem = attributes[semanticConventions.SEMATTRS_MESSAGING_SYSTEM];
353
354
  if (messagingSystem) {
354
355
  return {
356
+ ...getUserUpdatedNameAndSource(spanName, attributes, customSourceOrRoute),
355
357
  op: 'message',
356
- description: name,
357
- source: 'route',
358
358
  };
359
359
  }
360
360
 
@@ -362,15 +362,22 @@ function inferSpanData(name, attributes, kind) {
362
362
  // eslint-disable-next-line deprecation/deprecation
363
363
  const faasTrigger = attributes[semanticConventions.SEMATTRS_FAAS_TRIGGER];
364
364
  if (faasTrigger) {
365
- return { op: faasTrigger.toString(), description: name, source: 'route' };
365
+ return {
366
+ ...getUserUpdatedNameAndSource(spanName, attributes, customSourceOrRoute),
367
+ op: faasTrigger.toString(),
368
+ };
366
369
  }
367
370
 
368
- return { op: undefined, description: name, source: 'custom' };
371
+ return { op: undefined, description: spanName, source: 'custom' };
369
372
  }
370
373
 
371
374
  /**
372
375
  * Extract better op/description from an otel span.
373
376
  *
377
+ * Does not overwrite the span name if the source is already set to custom to ensure
378
+ * that user-updated span names are preserved. In this case, we only adjust the op but
379
+ * leave span description and source unchanged.
380
+ *
374
381
  * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
375
382
  */
376
383
  function parseSpanDescription(span) {
@@ -382,6 +389,21 @@ function parseSpanDescription(span) {
382
389
  }
383
390
 
384
391
  function descriptionForDbSystem({ attributes, name }) {
392
+ // if we already have a custom name, we don't overwrite it but only set the op
393
+ const userDefinedName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
394
+ if (typeof userDefinedName === 'string') {
395
+ return {
396
+ op: 'db',
397
+ description: userDefinedName,
398
+ source: (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] ) || 'custom',
399
+ };
400
+ }
401
+
402
+ // if we already have the source set to custom, we don't overwrite the span description but only set the op
403
+ if (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
404
+ return { op: 'db', description: name, source: 'custom' };
405
+ }
406
+
385
407
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
386
408
  // eslint-disable-next-line deprecation/deprecation
387
409
  const statement = attributes[semanticConventions.SEMATTRS_DB_STATEMENT];
@@ -415,7 +437,7 @@ function descriptionForHttpMethod(
415
437
  const { urlPath, url, query, fragment, hasRoute } = getSanitizedUrl(attributes, kind);
416
438
 
417
439
  if (!urlPath) {
418
- return { op: opParts.join('.'), description: name, source: 'custom' };
440
+ return { ...getUserUpdatedNameAndSource(name, attributes), op: opParts.join('.') };
419
441
  }
420
442
 
421
443
  const graphqlOperationsAttribute = attributes[SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION];
@@ -425,12 +447,12 @@ function descriptionForHttpMethod(
425
447
 
426
448
  // When the http span has a graphql operation, append it to the description
427
449
  // We add these in the graphqlIntegration
428
- const description = graphqlOperationsAttribute
450
+ const inferredDescription = graphqlOperationsAttribute
429
451
  ? `${baseDescription} (${getGraphqlOperationNamesFromAttribute(graphqlOperationsAttribute)})`
430
452
  : baseDescription;
431
453
 
432
454
  // If `httpPath` is a root path, then we can categorize the transaction source as route.
433
- const source = hasRoute || urlPath === '/' ? 'route' : 'url';
455
+ const inferredSource = hasRoute || urlPath === '/' ? 'route' : 'url';
434
456
 
435
457
  const data = {};
436
458
 
@@ -454,12 +476,21 @@ function descriptionForHttpMethod(
454
476
  const origin = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] || 'manual';
455
477
  const isManualSpan = !`${origin}`.startsWith('auto');
456
478
 
457
- const useInferredDescription = isClientOrServerKind || !isManualSpan;
479
+ // If users (or in very rare occasions we) set the source to custom, we don't overwrite the name
480
+ const alreadyHasCustomSource = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom';
481
+ const customSpanName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
482
+
483
+ const useInferredDescription =
484
+ !alreadyHasCustomSource && customSpanName == null && (isClientOrServerKind || !isManualSpan);
485
+
486
+ const { description, source } = useInferredDescription
487
+ ? { description: inferredDescription, source: inferredSource }
488
+ : getUserUpdatedNameAndSource(name, attributes);
458
489
 
459
490
  return {
460
491
  op: opParts.join('.'),
461
- description: useInferredDescription ? description : name,
462
- source: useInferredDescription ? source : 'custom',
492
+ description,
493
+ source,
463
494
  data,
464
495
  };
465
496
  }
@@ -521,6 +552,38 @@ function getSanitizedUrl(
521
552
  return { urlPath: undefined, url, query, fragment, hasRoute: false };
522
553
  }
523
554
 
555
+ /**
556
+ * Because Otel instrumentation sometimes mutates span names via `span.updateName`, the only way
557
+ * to ensure that a user-set span name is preserved is to store it as a tmp attribute on the span.
558
+ * We delete this attribute once we're done with it when preparing the event envelope.
559
+ *
560
+ * This temp attribute always takes precedence over the original name.
561
+ *
562
+ * We also need to take care of setting the correct source. Users can always update the source
563
+ * after updating the name, so we need to respect that.
564
+ *
565
+ * @internal exported only for testing
566
+ */
567
+ function getUserUpdatedNameAndSource(
568
+ originalName,
569
+ attributes,
570
+ fallbackSource = 'custom',
571
+ )
572
+
573
+ {
574
+ const source = (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] ) || fallbackSource;
575
+ const description = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
576
+
577
+ if (description && typeof description === 'string') {
578
+ return {
579
+ description,
580
+ source,
581
+ };
582
+ }
583
+
584
+ return { description: originalName, source };
585
+ }
586
+
524
587
  /**
525
588
  * Setup a DSC handler on the passed client,
526
589
  * ensuring that the transaction name is inferred from the span correctly.
@@ -1171,9 +1234,7 @@ function getContextForScope(scope) {
1171
1234
  * It propagates the trace as a remote span, in addition to setting it on the propagation context.
1172
1235
  */
1173
1236
  function continueTrace(options, callback) {
1174
- return core.continueTrace(options, () => {
1175
- return continueTraceAsRemoteSpan(api.context.active(), options, callback);
1176
- });
1237
+ return continueTraceAsRemoteSpan(api.context.active(), options, callback);
1177
1238
  }
1178
1239
 
1179
1240
  /**
@@ -1347,6 +1408,7 @@ function setOpenTelemetryContextAsyncContextStrategy() {
1347
1408
  getActiveSpan,
1348
1409
  suppressTracing,
1349
1410
  getTraceData,
1411
+ continueTrace,
1350
1412
  // The types here don't fully align, because our own `Span` type is narrower
1351
1413
  // than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
1352
1414
  withActiveSpan: withActiveSpan ,
@@ -1928,6 +1990,7 @@ function removeSentryAttributes(data) {
1928
1990
  /* eslint-disable @typescript-eslint/no-dynamic-delete */
1929
1991
  delete cleanedData[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
1930
1992
  delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE];
1993
+ delete cleanedData[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
1931
1994
  /* eslint-enable @typescript-eslint/no-dynamic-delete */
1932
1995
 
1933
1996
  return cleanedData;