replayReader.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type {Crumb} from 'sentry/types/breadcrumbs';
  2. import {
  3. breadcrumbFactory,
  4. replayTimestamps,
  5. rrwebEventListFactory,
  6. spansFactory,
  7. } from 'sentry/utils/replays/replayDataUtils';
  8. import type {
  9. MemorySpanType,
  10. RecordingEvent,
  11. ReplayCrumb,
  12. ReplayError,
  13. ReplayRecord,
  14. ReplaySpan,
  15. } from 'sentry/views/replays/types';
  16. interface ReplayReaderParams {
  17. breadcrumbs: ReplayCrumb[] | undefined;
  18. errors: ReplayError[] | undefined;
  19. /**
  20. * The root Replay event, created at the start of the browser session.
  21. */
  22. replayRecord: ReplayRecord | undefined;
  23. /**
  24. * The captured data from rrweb.
  25. * Saved as N attachments that belong to the root Replay event.
  26. */
  27. rrwebEvents: RecordingEvent[] | undefined;
  28. spans: ReplaySpan[] | undefined;
  29. }
  30. type RequiredNotNull<T> = {
  31. [P in keyof T]: NonNullable<T[P]>;
  32. };
  33. export default class ReplayReader {
  34. static factory({
  35. breadcrumbs,
  36. replayRecord,
  37. errors,
  38. rrwebEvents,
  39. spans,
  40. }: ReplayReaderParams) {
  41. if (!breadcrumbs || !replayRecord || !rrwebEvents || !spans || !errors) {
  42. return null;
  43. }
  44. return new ReplayReader({breadcrumbs, replayRecord, errors, rrwebEvents, spans});
  45. }
  46. private constructor({
  47. breadcrumbs,
  48. replayRecord,
  49. errors,
  50. rrwebEvents,
  51. spans,
  52. }: RequiredNotNull<ReplayReaderParams>) {
  53. // TODO(replays): We should get correct timestamps from the backend instead
  54. // of having to fix them up here.
  55. const {startTimestampMs, endTimestampMs} = replayTimestamps(
  56. rrwebEvents,
  57. breadcrumbs,
  58. spans
  59. );
  60. replayRecord.startedAt = new Date(startTimestampMs);
  61. replayRecord.finishedAt = new Date(endTimestampMs);
  62. this.spans = spansFactory(spans);
  63. this.breadcrumbs = breadcrumbFactory(replayRecord, errors, breadcrumbs, this.spans);
  64. this.rrwebEvents = rrwebEventListFactory(replayRecord, rrwebEvents);
  65. this.replayRecord = replayRecord;
  66. }
  67. private replayRecord: ReplayRecord;
  68. private rrwebEvents: RecordingEvent[];
  69. private breadcrumbs: Crumb[];
  70. private spans: ReplaySpan[];
  71. /**
  72. * @returns Duration of Replay (milliseonds)
  73. */
  74. getDurationMs = () => {
  75. return this.replayRecord.duration * 1000;
  76. };
  77. getReplay = () => {
  78. return this.replayRecord;
  79. };
  80. getRRWebEvents = () => {
  81. return this.rrwebEvents;
  82. };
  83. getRawCrumbs = () => {
  84. return this.breadcrumbs;
  85. };
  86. getRawSpans = () => {
  87. return this.spans;
  88. };
  89. isMemorySpan = (span: ReplaySpan): span is MemorySpanType => {
  90. return span.op === 'memory';
  91. };
  92. isNetworkSpan = (span: ReplaySpan) => {
  93. return !this.isMemorySpan(span) && !span.op.includes('paint');
  94. };
  95. }