replayReader.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import type {Crumb} from 'sentry/types/breadcrumbs';
  2. import type {Event, EventTransaction} from 'sentry/types/event';
  3. import {
  4. breadcrumbFactory,
  5. replayTimestamps,
  6. rrwebEventListFactory,
  7. spansFactory,
  8. } from 'sentry/utils/replays/replayDataUtils';
  9. import type {
  10. MemorySpanType,
  11. RecordingEvent,
  12. ReplayCrumb,
  13. ReplayError,
  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. event: Event | 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({breadcrumbs, event, errors, rrwebEvents, spans}: ReplayReaderParams) {
  35. if (!breadcrumbs || !event || !rrwebEvents || !spans || !errors) {
  36. return null;
  37. }
  38. return new ReplayReader({breadcrumbs, event, errors, rrwebEvents, spans});
  39. }
  40. private constructor({
  41. breadcrumbs,
  42. event,
  43. errors,
  44. rrwebEvents,
  45. spans,
  46. }: RequiredNotNull<ReplayReaderParams>) {
  47. const {startTimestampMS, endTimestampMS} = replayTimestamps(
  48. rrwebEvents,
  49. breadcrumbs,
  50. spans
  51. );
  52. this.spans = spansFactory(spans);
  53. this.breadcrumbs = breadcrumbFactory(
  54. startTimestampMS,
  55. event,
  56. errors,
  57. breadcrumbs,
  58. this.spans
  59. );
  60. this.rrwebEvents = rrwebEventListFactory(
  61. startTimestampMS,
  62. endTimestampMS,
  63. rrwebEvents
  64. );
  65. this.event = {
  66. ...event,
  67. startTimestamp: startTimestampMS / 1000,
  68. endTimestamp: endTimestampMS / 1000,
  69. } as EventTransaction;
  70. }
  71. private event: EventTransaction;
  72. private rrwebEvents: RecordingEvent[];
  73. private breadcrumbs: Crumb[];
  74. private spans: ReplaySpan[];
  75. getEvent = () => {
  76. return this.event;
  77. };
  78. getRRWebEvents = () => {
  79. return this.rrwebEvents;
  80. };
  81. getRawCrumbs = () => {
  82. return this.breadcrumbs;
  83. };
  84. getRawSpans = () => {
  85. return this.spans;
  86. };
  87. isMemorySpan = (span: ReplaySpan): span is MemorySpanType => {
  88. return span.op === 'memory';
  89. };
  90. isNotMemorySpan = (span: ReplaySpan) => {
  91. return !this.isMemorySpan(span);
  92. };
  93. }