tracing.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { baggageHeaderToDynamicSamplingContext } from './baggage.js';
  2. import { uuid4 } from './misc.js';
  3. // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- RegExp is used for readability here
  4. const TRACEPARENT_REGEXP = new RegExp(
  5. '^[ \\t]*' + // whitespace
  6. '([0-9a-f]{32})?' + // trace_id
  7. '-?([0-9a-f]{16})?' + // span_id
  8. '-?([01])?' + // sampled
  9. '[ \\t]*$', // whitespace
  10. );
  11. /**
  12. * Extract transaction context data from a `sentry-trace` header.
  13. *
  14. * @param traceparent Traceparent string
  15. *
  16. * @returns Object containing data from the header, or undefined if traceparent string is malformed
  17. */
  18. function extractTraceparentData(traceparent) {
  19. if (!traceparent) {
  20. return undefined;
  21. }
  22. const matches = traceparent.match(TRACEPARENT_REGEXP);
  23. if (!matches) {
  24. return undefined;
  25. }
  26. let parentSampled;
  27. if (matches[3] === '1') {
  28. parentSampled = true;
  29. } else if (matches[3] === '0') {
  30. parentSampled = false;
  31. }
  32. return {
  33. traceId: matches[1],
  34. parentSampled,
  35. parentSpanId: matches[2],
  36. };
  37. }
  38. /**
  39. * Create tracing context from incoming headers.
  40. *
  41. * @deprecated Use `propagationContextFromHeaders` instead.
  42. */
  43. // TODO(v8): Remove this function
  44. function tracingContextFromHeaders(
  45. sentryTrace,
  46. baggage,
  47. )
  48. {
  49. const traceparentData = extractTraceparentData(sentryTrace);
  50. const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage);
  51. const { traceId, parentSpanId, parentSampled } = traceparentData || {};
  52. if (!traceparentData) {
  53. return {
  54. traceparentData,
  55. dynamicSamplingContext: undefined,
  56. propagationContext: {
  57. traceId: traceId || uuid4(),
  58. spanId: uuid4().substring(16),
  59. },
  60. };
  61. } else {
  62. return {
  63. traceparentData,
  64. dynamicSamplingContext: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it
  65. propagationContext: {
  66. traceId: traceId || uuid4(),
  67. parentSpanId: parentSpanId || uuid4().substring(16),
  68. spanId: uuid4().substring(16),
  69. sampled: parentSampled,
  70. dsc: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it
  71. },
  72. };
  73. }
  74. }
  75. /**
  76. * Create a propagation context from incoming headers.
  77. */
  78. function propagationContextFromHeaders(
  79. sentryTrace,
  80. baggage,
  81. ) {
  82. const traceparentData = extractTraceparentData(sentryTrace);
  83. const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage);
  84. const { traceId, parentSpanId, parentSampled } = traceparentData || {};
  85. if (!traceparentData) {
  86. return {
  87. traceId: traceId || uuid4(),
  88. spanId: uuid4().substring(16),
  89. };
  90. } else {
  91. return {
  92. traceId: traceId || uuid4(),
  93. parentSpanId: parentSpanId || uuid4().substring(16),
  94. spanId: uuid4().substring(16),
  95. sampled: parentSampled,
  96. dsc: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it
  97. };
  98. }
  99. }
  100. /**
  101. * Create sentry-trace header from span context values.
  102. */
  103. function generateSentryTraceHeader(
  104. traceId = uuid4(),
  105. spanId = uuid4().substring(16),
  106. sampled,
  107. ) {
  108. let sampledString = '';
  109. if (sampled !== undefined) {
  110. sampledString = sampled ? '-1' : '-0';
  111. }
  112. return `${traceId}-${spanId}${sampledString}`;
  113. }
  114. export { TRACEPARENT_REGEXP, extractTraceparentData, generateSentryTraceHeader, propagationContextFromHeaders, tracingContextFromHeaders };
  115. //# sourceMappingURL=tracing.js.map