tracing.js 3.8 KB

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