spanUtils.js 3.4 KB

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