@sentry/opentelemetry 8.46.0 → 8.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.
@@ -1,6 +1,6 @@
1
1
  import { _optionalChain } from '@sentry/core';
2
2
  import { ATTR_URL_FULL, SEMATTRS_HTTP_URL, ATTR_HTTP_REQUEST_METHOD, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, ATTR_HTTP_ROUTE, ATTR_HTTP_RESPONSE_STATUS_CODE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
3
- import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, stripUrlQueryAndFragment, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, hasTracingEnabled, dynamicSamplingContextToSentryBaggageHeader, getDynamicSamplingContextFromSpan, getRootSpan, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, generateSpanId, getCurrentScope, getDynamicSamplingContextFromScope, propagationContextFromHeaders, getIsolationScope, handleCallbackErrors, continueTrace as continueTrace$1, spanToTraceContext, getTraceContextFromScope, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, dropUndefinedKeys, getStatusMessage, spanTimeInputToSeconds, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
3
+ import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, stripUrlQueryAndFragment, spanToJSON, hasTracingEnabled, dynamicSamplingContextToSentryBaggageHeader, getDynamicSamplingContextFromSpan, getRootSpan, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, generateSpanId, getCurrentScope, getDynamicSamplingContextFromScope, propagationContextFromHeaders, getIsolationScope, handleCallbackErrors, continueTrace as continueTrace$1, spanToTraceContext, getTraceContextFromScope, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, dropUndefinedKeys, getStatusMessage, spanTimeInputToSeconds, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
4
4
  export { getClient, getCurrentHubShim, getDynamicSamplingContextFromSpan } from '@sentry/core';
5
5
  import * as api from '@opentelemetry/api';
6
6
  import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
@@ -313,12 +313,12 @@ function getSamplingDecision(spanContext) {
313
313
  /**
314
314
  * Infer the op & description for a set of name, attributes and kind of a span.
315
315
  */
316
- function inferSpanData(name, attributes, kind) {
316
+ function inferSpanData(spanName, attributes, kind) {
317
317
  // if http.method exists, this is an http request span
318
318
  // eslint-disable-next-line deprecation/deprecation
319
319
  const httpMethod = attributes[ATTR_HTTP_REQUEST_METHOD] || attributes[SEMATTRS_HTTP_METHOD];
320
320
  if (httpMethod) {
321
- return descriptionForHttpMethod({ attributes, name, kind }, httpMethod);
321
+ return descriptionForHttpMethod({ attributes, name: spanName, kind }, httpMethod);
322
322
  }
323
323
 
324
324
  // eslint-disable-next-line deprecation/deprecation
@@ -330,17 +330,18 @@ function inferSpanData(name, attributes, kind) {
330
330
  // If db.type exists then this is a database call span
331
331
  // If the Redis DB is used as a cache, the span description should not be changed
332
332
  if (dbSystem && !opIsCache) {
333
- return descriptionForDbSystem({ attributes, name });
333
+ return descriptionForDbSystem({ attributes, name: spanName });
334
334
  }
335
335
 
336
+ const customSourceOrRoute = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom' ? 'custom' : 'route';
337
+
336
338
  // If rpc.service exists then this is a rpc call span.
337
339
  // eslint-disable-next-line deprecation/deprecation
338
340
  const rpcService = attributes[SEMATTRS_RPC_SERVICE];
339
341
  if (rpcService) {
340
342
  return {
343
+ ...getUserUpdatedNameAndSource(spanName, attributes, 'route'),
341
344
  op: 'rpc',
342
- description: name,
343
- source: 'route',
344
345
  };
345
346
  }
346
347
 
@@ -349,9 +350,8 @@ function inferSpanData(name, attributes, kind) {
349
350
  const messagingSystem = attributes[SEMATTRS_MESSAGING_SYSTEM];
350
351
  if (messagingSystem) {
351
352
  return {
353
+ ...getUserUpdatedNameAndSource(spanName, attributes, customSourceOrRoute),
352
354
  op: 'message',
353
- description: name,
354
- source: 'route',
355
355
  };
356
356
  }
357
357
 
@@ -359,15 +359,22 @@ function inferSpanData(name, attributes, kind) {
359
359
  // eslint-disable-next-line deprecation/deprecation
360
360
  const faasTrigger = attributes[SEMATTRS_FAAS_TRIGGER];
361
361
  if (faasTrigger) {
362
- return { op: faasTrigger.toString(), description: name, source: 'route' };
362
+ return {
363
+ ...getUserUpdatedNameAndSource(spanName, attributes, customSourceOrRoute),
364
+ op: faasTrigger.toString(),
365
+ };
363
366
  }
364
367
 
365
- return { op: undefined, description: name, source: 'custom' };
368
+ return { op: undefined, description: spanName, source: 'custom' };
366
369
  }
367
370
 
368
371
  /**
369
372
  * Extract better op/description from an otel span.
370
373
  *
374
+ * Does not overwrite the span name if the source is already set to custom to ensure
375
+ * that user-updated span names are preserved. In this case, we only adjust the op but
376
+ * leave span description and source unchanged.
377
+ *
371
378
  * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
372
379
  */
373
380
  function parseSpanDescription(span) {
@@ -379,6 +386,21 @@ function parseSpanDescription(span) {
379
386
  }
380
387
 
381
388
  function descriptionForDbSystem({ attributes, name }) {
389
+ // if we already have a custom name, we don't overwrite it but only set the op
390
+ const userDefinedName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
391
+ if (typeof userDefinedName === 'string') {
392
+ return {
393
+ op: 'db',
394
+ description: userDefinedName,
395
+ source: (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] ) || 'custom',
396
+ };
397
+ }
398
+
399
+ // if we already have the source set to custom, we don't overwrite the span description but only set the op
400
+ if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
401
+ return { op: 'db', description: name, source: 'custom' };
402
+ }
403
+
382
404
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
383
405
  // eslint-disable-next-line deprecation/deprecation
384
406
  const statement = attributes[SEMATTRS_DB_STATEMENT];
@@ -412,7 +434,7 @@ function descriptionForHttpMethod(
412
434
  const { urlPath, url, query, fragment, hasRoute } = getSanitizedUrl(attributes, kind);
413
435
 
414
436
  if (!urlPath) {
415
- return { op: opParts.join('.'), description: name, source: 'custom' };
437
+ return { ...getUserUpdatedNameAndSource(name, attributes), op: opParts.join('.') };
416
438
  }
417
439
 
418
440
  const graphqlOperationsAttribute = attributes[SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION];
@@ -422,12 +444,12 @@ function descriptionForHttpMethod(
422
444
 
423
445
  // When the http span has a graphql operation, append it to the description
424
446
  // We add these in the graphqlIntegration
425
- const description = graphqlOperationsAttribute
447
+ const inferredDescription = graphqlOperationsAttribute
426
448
  ? `${baseDescription} (${getGraphqlOperationNamesFromAttribute(graphqlOperationsAttribute)})`
427
449
  : baseDescription;
428
450
 
429
451
  // If `httpPath` is a root path, then we can categorize the transaction source as route.
430
- const source = hasRoute || urlPath === '/' ? 'route' : 'url';
452
+ const inferredSource = hasRoute || urlPath === '/' ? 'route' : 'url';
431
453
 
432
454
  const data = {};
433
455
 
@@ -451,12 +473,21 @@ function descriptionForHttpMethod(
451
473
  const origin = attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] || 'manual';
452
474
  const isManualSpan = !`${origin}`.startsWith('auto');
453
475
 
454
- const useInferredDescription = isClientOrServerKind || !isManualSpan;
476
+ // If users (or in very rare occasions we) set the source to custom, we don't overwrite the name
477
+ const alreadyHasCustomSource = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom';
478
+ const customSpanName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
479
+
480
+ const useInferredDescription =
481
+ !alreadyHasCustomSource && customSpanName == null && (isClientOrServerKind || !isManualSpan);
482
+
483
+ const { description, source } = useInferredDescription
484
+ ? { description: inferredDescription, source: inferredSource }
485
+ : getUserUpdatedNameAndSource(name, attributes);
455
486
 
456
487
  return {
457
488
  op: opParts.join('.'),
458
- description: useInferredDescription ? description : name,
459
- source: useInferredDescription ? source : 'custom',
489
+ description,
490
+ source,
460
491
  data,
461
492
  };
462
493
  }
@@ -518,6 +549,38 @@ function getSanitizedUrl(
518
549
  return { urlPath: undefined, url, query, fragment, hasRoute: false };
519
550
  }
520
551
 
552
+ /**
553
+ * Because Otel instrumentation sometimes mutates span names via `span.updateName`, the only way
554
+ * to ensure that a user-set span name is preserved is to store it as a tmp attribute on the span.
555
+ * We delete this attribute once we're done with it when preparing the event envelope.
556
+ *
557
+ * This temp attribute always takes precedence over the original name.
558
+ *
559
+ * We also need to take care of setting the correct source. Users can always update the source
560
+ * after updating the name, so we need to respect that.
561
+ *
562
+ * @internal exported only for testing
563
+ */
564
+ function getUserUpdatedNameAndSource(
565
+ originalName,
566
+ attributes,
567
+ fallbackSource = 'custom',
568
+ )
569
+
570
+ {
571
+ const source = (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] ) || fallbackSource;
572
+ const description = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
573
+
574
+ if (description && typeof description === 'string') {
575
+ return {
576
+ description,
577
+ source,
578
+ };
579
+ }
580
+
581
+ return { description: originalName, source };
582
+ }
583
+
521
584
  /**
522
585
  * Setup a DSC handler on the passed client,
523
586
  * ensuring that the transaction name is inferred from the span correctly.
@@ -1925,6 +1988,7 @@ function removeSentryAttributes(data) {
1925
1988
  /* eslint-disable @typescript-eslint/no-dynamic-delete */
1926
1989
  delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
1927
1990
  delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE];
1991
+ delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
1928
1992
  /* eslint-enable @typescript-eslint/no-dynamic-delete */
1929
1993
 
1930
1994
  return cleanedData;