hydrateSpans.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import invariant from 'invariant';
  2. import isValidDate from 'sentry/utils/date/isValidDate';
  3. import type {RawSpanFrame, SpanFrame} from 'sentry/utils/replays/types';
  4. import {isSpanFrame} from 'sentry/utils/replays/types';
  5. import type {ReplayRecord} from 'sentry/views/replays/types';
  6. export default function hydrateSpans(
  7. replayRecord: ReplayRecord,
  8. spanFrames: RawSpanFrame[]
  9. ): SpanFrame[] {
  10. const startTimestampMs = replayRecord.started_at.getTime();
  11. return spanFrames
  12. .map((frame: RawSpanFrame) => {
  13. try {
  14. const start = new Date(frame.startTimestamp * 1000);
  15. const end = new Date(frame.endTimestamp * 1000);
  16. invariant(isValidDate(start), 'spanFrame.startTimestamp is invalid');
  17. invariant(isValidDate(end), 'spanFrame.endTimestamp is invalid');
  18. return {
  19. ...frame,
  20. endTimestamp: end,
  21. endTimestampMs: end.getTime(),
  22. offsetMs: Math.abs(start.getTime() - startTimestampMs),
  23. startTimestamp: start,
  24. timestampMs: start.getTime(),
  25. // TODO: do we need this added as well?
  26. // id: `${span.description ?? span.op}-${span.startTimestamp}-${span.endTimestamp}`,
  27. };
  28. } catch {
  29. return undefined;
  30. }
  31. })
  32. .filter(isSpanFrame);
  33. }