replayReader.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. export 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. replayRecord,
  57. rrwebEvents,
  58. breadcrumbs,
  59. spans
  60. );
  61. replayRecord.startedAt = new Date(startTimestampMs);
  62. replayRecord.finishedAt = new Date(endTimestampMs);
  63. this.spans = spansFactory(spans);
  64. this.breadcrumbs = breadcrumbFactory(replayRecord, errors, breadcrumbs, this.spans);
  65. this.rrwebEvents = rrwebEventListFactory(replayRecord, rrwebEvents);
  66. this.replayRecord = replayRecord;
  67. }
  68. private replayRecord: ReplayRecord;
  69. private rrwebEvents: RecordingEvent[];
  70. private breadcrumbs: Crumb[];
  71. private spans: ReplaySpan[];
  72. /**
  73. * @returns Duration of Replay (milliseonds)
  74. */
  75. getDurationMs = () => {
  76. return this.replayRecord.duration * 1000;
  77. };
  78. getReplay = () => {
  79. return this.replayRecord;
  80. };
  81. getRRWebEvents = () => {
  82. return this.rrwebEvents;
  83. };
  84. getRawCrumbs = () => {
  85. return this.breadcrumbs;
  86. };
  87. getRawSpans = () => {
  88. return this.spans;
  89. };
  90. isMemorySpan = (span: ReplaySpan): span is MemorySpanType => {
  91. return span.op === 'memory';
  92. };
  93. isNetworkSpan = (span: ReplaySpan) => {
  94. return !this.isMemorySpan(span) && !span.op.includes('paint');
  95. };
  96. }