replayReader.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import type {BreadcrumbTypeNavigation, Crumb} from 'sentry/types/breadcrumbs';
  2. import {BreadcrumbType} from 'sentry/types/breadcrumbs';
  3. import type {Event, EventTransaction} from 'sentry/types/event';
  4. import {
  5. breadcrumbFactory,
  6. replayTimestamps,
  7. rrwebEventListFactory,
  8. spansFactory,
  9. } from 'sentry/utils/replays/replayDataUtils';
  10. import type {
  11. MemorySpanType,
  12. RecordingEvent,
  13. ReplayCrumb,
  14. ReplayError,
  15. ReplayRecord,
  16. ReplaySpan,
  17. } from 'sentry/views/replays/types';
  18. interface ReplayReaderParams {
  19. breadcrumbs: ReplayCrumb[] | undefined;
  20. errors: ReplayError[] | undefined;
  21. /**
  22. * The root Replay event, created at the start of the browser session.
  23. */
  24. event: Event | undefined;
  25. /**
  26. * The captured data from rrweb.
  27. * Saved as N attachments that belong to the root Replay event.
  28. */
  29. rrwebEvents: RecordingEvent[] | undefined;
  30. spans: ReplaySpan[] | undefined;
  31. }
  32. type RequiredNotNull<T> = {
  33. [P in keyof T]: NonNullable<T[P]>;
  34. };
  35. export default class ReplayReader {
  36. static factory({breadcrumbs, event, errors, rrwebEvents, spans}: ReplayReaderParams) {
  37. if (!breadcrumbs || !event || !rrwebEvents || !spans || !errors) {
  38. return null;
  39. }
  40. return new ReplayReader({breadcrumbs, event, errors, rrwebEvents, spans});
  41. }
  42. private constructor({
  43. breadcrumbs,
  44. event,
  45. errors,
  46. rrwebEvents,
  47. spans,
  48. }: RequiredNotNull<ReplayReaderParams>) {
  49. const {startTimestampMs, endTimestampMs} = replayTimestamps(
  50. rrwebEvents,
  51. breadcrumbs,
  52. spans
  53. );
  54. this.spans = spansFactory(spans);
  55. this.breadcrumbs = breadcrumbFactory(
  56. startTimestampMs,
  57. event,
  58. errors,
  59. breadcrumbs,
  60. this.spans
  61. );
  62. this.rrwebEvents = rrwebEventListFactory(
  63. startTimestampMs,
  64. endTimestampMs,
  65. rrwebEvents
  66. );
  67. this.event = {
  68. ...event,
  69. startTimestamp: startTimestampMs / 1000,
  70. endTimestamp: endTimestampMs / 1000,
  71. } as EventTransaction;
  72. const urls = (
  73. this.getRawCrumbs().filter(
  74. crumb => crumb.category === BreadcrumbType.NAVIGATION
  75. ) as BreadcrumbTypeNavigation[]
  76. )
  77. .map(crumb => crumb.data?.to)
  78. .filter(Boolean) as string[];
  79. this.replayRecord = {
  80. countErrors: this.getRawCrumbs().filter(
  81. crumb => crumb.category === BreadcrumbType.ERROR
  82. ).length,
  83. countSegments: 0,
  84. countUrls: urls.length,
  85. errorIds: [],
  86. dist: this.event.dist,
  87. duration: endTimestampMs - startTimestampMs,
  88. environment: null,
  89. finishedAt: new Date(endTimestampMs), // TODO(replay): Convert from string to Date when reading API
  90. longestTransaction: 0,
  91. platform: this.event.platform,
  92. projectId: this.event.projectID,
  93. projectSlug: '', // TODO(replay): Read from useProject to fill this in
  94. release: null, // event.release is not a string, expected to be `version@1.4`
  95. replayId: this.event.id,
  96. sdkName: this.event.sdk?.name,
  97. sdkVersion: this.event.sdk?.version,
  98. startedAt: new Date(startTimestampMs), // TODO(replay): Convert from string to Date when reading API
  99. tags: this.event.tags.reduce((tags, {key, value}) => {
  100. tags[key] = value;
  101. return tags;
  102. }, {} as ReplayRecord['tags']),
  103. title: this.event.title,
  104. traceIds: [],
  105. urls,
  106. userAgent: '',
  107. user: {
  108. email: this.event.user?.email,
  109. id: this.event.user?.id,
  110. ipAddress: this.event.user?.ip_address,
  111. name: this.event.user?.name,
  112. },
  113. } as ReplayRecord;
  114. }
  115. private event: EventTransaction;
  116. private replayRecord: ReplayRecord;
  117. private rrwebEvents: RecordingEvent[];
  118. private breadcrumbs: Crumb[];
  119. private spans: ReplaySpan[];
  120. getEvent = () => {
  121. return this.event;
  122. };
  123. /**
  124. * @returns Duration of Replay (milliseonds)
  125. */
  126. getDurationMs = () => {
  127. return this.replayRecord.duration;
  128. };
  129. getReplay = () => {
  130. return this.replayRecord;
  131. };
  132. getRRWebEvents = () => {
  133. return this.rrwebEvents;
  134. };
  135. getRawCrumbs = () => {
  136. return this.breadcrumbs;
  137. };
  138. getRawSpans = () => {
  139. return this.spans;
  140. };
  141. isMemorySpan = (span: ReplaySpan): span is MemorySpanType => {
  142. return span.op === 'memory';
  143. };
  144. isNetworkSpan = (span: ReplaySpan) => {
  145. return !this.isMemorySpan(span) && !span.op.includes('paint');
  146. };
  147. }