replayReader.tsx 3.1 KB

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