dynamicSamplingContext.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { dropUndefinedKeys } from '@sentry/utils';
  2. import { DEFAULT_ENVIRONMENT } from '../constants.js';
  3. import { getClient, getCurrentScope } from '../exports.js';
  4. import { getRootSpan } from '../utils/getRootSpan.js';
  5. import { spanToJSON, spanIsSampled } from '../utils/spanUtils.js';
  6. /**
  7. * Creates a dynamic sampling context from a client.
  8. *
  9. * Dispatches the `createDsc` lifecycle hook as a side effect.
  10. */
  11. function getDynamicSamplingContextFromClient(
  12. trace_id,
  13. client,
  14. scope,
  15. ) {
  16. const options = client.getOptions();
  17. const { publicKey: public_key } = client.getDsn() || {};
  18. // TODO(v8): Remove segment from User
  19. // eslint-disable-next-line deprecation/deprecation
  20. const { segment: user_segment } = (scope && scope.getUser()) || {};
  21. const dsc = dropUndefinedKeys({
  22. environment: options.environment || DEFAULT_ENVIRONMENT,
  23. release: options.release,
  24. user_segment,
  25. public_key,
  26. trace_id,
  27. }) ;
  28. client.emit && client.emit('createDsc', dsc);
  29. return dsc;
  30. }
  31. /**
  32. * A Span with a frozen dynamic sampling context.
  33. */
  34. /**
  35. * Creates a dynamic sampling context from a span (and client and scope)
  36. *
  37. * @param span the span from which a few values like the root span name and sample rate are extracted.
  38. *
  39. * @returns a dynamic sampling context
  40. */
  41. function getDynamicSamplingContextFromSpan(span) {
  42. const client = getClient();
  43. if (!client) {
  44. return {};
  45. }
  46. // passing emit=false here to only emit later once the DSC is actually populated
  47. const dsc = getDynamicSamplingContextFromClient(spanToJSON(span).trace_id || '', client, getCurrentScope());
  48. // TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
  49. const txn = getRootSpan(span) ;
  50. if (!txn) {
  51. return dsc;
  52. }
  53. // TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
  54. // For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
  55. // @see Transaction class constructor
  56. const v7FrozenDsc = txn && txn._frozenDynamicSamplingContext;
  57. if (v7FrozenDsc) {
  58. return v7FrozenDsc;
  59. }
  60. // TODO (v8): Replace txn.metadata with txn.attributes[]
  61. // We can't do this yet because attributes aren't always set yet.
  62. // eslint-disable-next-line deprecation/deprecation
  63. const { sampleRate: maybeSampleRate, source } = txn.metadata;
  64. if (maybeSampleRate != null) {
  65. dsc.sample_rate = `${maybeSampleRate}`;
  66. }
  67. // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
  68. const jsonSpan = spanToJSON(txn);
  69. // after JSON conversion, txn.name becomes jsonSpan.description
  70. if (source && source !== 'url') {
  71. dsc.transaction = jsonSpan.description;
  72. }
  73. dsc.sampled = String(spanIsSampled(txn));
  74. client.emit && client.emit('createDsc', dsc);
  75. return dsc;
  76. }
  77. export { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan };
  78. //# sourceMappingURL=dynamicSamplingContext.js.map