spanUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const utils = require('@sentry/utils');
  3. // These are aligned with OpenTelemetry trace flags
  4. const TRACE_FLAG_NONE = 0x0;
  5. const TRACE_FLAG_SAMPLED = 0x1;
  6. /**
  7. * Convert a span to a trace context, which can be sent as the `trace` context in an event.
  8. */
  9. function spanToTraceContext(span) {
  10. const { spanId: span_id, traceId: trace_id } = span.spanContext();
  11. const { data, op, parent_span_id, status, tags, origin } = spanToJSON(span);
  12. return utils.dropUndefinedKeys({
  13. data,
  14. op,
  15. parent_span_id,
  16. span_id,
  17. status,
  18. tags,
  19. trace_id,
  20. origin,
  21. });
  22. }
  23. /**
  24. * Convert a Span to a Sentry trace header.
  25. */
  26. function spanToTraceHeader(span) {
  27. const { traceId, spanId } = span.spanContext();
  28. const sampled = spanIsSampled(span);
  29. return utils.generateSentryTraceHeader(traceId, spanId, sampled);
  30. }
  31. /**
  32. * Convert a span time input intp a timestamp in seconds.
  33. */
  34. function spanTimeInputToSeconds(input) {
  35. if (typeof input === 'number') {
  36. return ensureTimestampInSeconds(input);
  37. }
  38. if (Array.isArray(input)) {
  39. // See {@link HrTime} for the array-based time format
  40. return input[0] + input[1] / 1e9;
  41. }
  42. if (input instanceof Date) {
  43. return ensureTimestampInSeconds(input.getTime());
  44. }
  45. return utils.timestampInSeconds();
  46. }
  47. /**
  48. * Converts a timestamp to second, if it was in milliseconds, or keeps it as second.
  49. */
  50. function ensureTimestampInSeconds(timestamp) {
  51. const isMs = timestamp > 9999999999;
  52. return isMs ? timestamp / 1000 : timestamp;
  53. }
  54. /**
  55. * Convert a span to a JSON representation.
  56. * Note that all fields returned here are optional and need to be guarded against.
  57. *
  58. * Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).
  59. * This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.
  60. * And `spanToJSON` needs the Span class from `span.ts` to check here.
  61. * TODO v8: When we remove the deprecated stuff from `span.ts`, we can remove the circular dependency again.
  62. */
  63. function spanToJSON(span) {
  64. if (spanIsSpanClass(span)) {
  65. return span.getSpanJSON();
  66. }
  67. // Fallback: We also check for `.toJSON()` here...
  68. // eslint-disable-next-line deprecation/deprecation
  69. if (typeof span.toJSON === 'function') {
  70. // eslint-disable-next-line deprecation/deprecation
  71. return span.toJSON();
  72. }
  73. return {};
  74. }
  75. /**
  76. * Sadly, due to circular dependency checks we cannot actually import the Span class here and check for instanceof.
  77. * :( So instead we approximate this by checking if it has the `getSpanJSON` method.
  78. */
  79. function spanIsSpanClass(span) {
  80. return typeof (span ).getSpanJSON === 'function';
  81. }
  82. /**
  83. * Returns true if a span is sampled.
  84. * In most cases, you should just use `span.isRecording()` instead.
  85. * However, this has a slightly different semantic, as it also returns false if the span is finished.
  86. * So in the case where this distinction is important, use this method.
  87. */
  88. function spanIsSampled(span) {
  89. // We align our trace flags with the ones OpenTelemetry use
  90. // So we also check for sampled the same way they do.
  91. const { traceFlags } = span.spanContext();
  92. // eslint-disable-next-line no-bitwise
  93. return Boolean(traceFlags & TRACE_FLAG_SAMPLED);
  94. }
  95. exports.TRACE_FLAG_NONE = TRACE_FLAG_NONE;
  96. exports.TRACE_FLAG_SAMPLED = TRACE_FLAG_SAMPLED;
  97. exports.spanIsSampled = spanIsSampled;
  98. exports.spanTimeInputToSeconds = spanTimeInputToSeconds;
  99. exports.spanToJSON = spanToJSON;
  100. exports.spanToTraceContext = spanToTraceContext;
  101. exports.spanToTraceHeader = spanToTraceHeader;
  102. //# sourceMappingURL=spanUtils.js.map