replaySegments.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {EventType} from '@sentry-internal/rrweb';
  2. import {serializedNodeWithId} from '@sentry-internal/rrweb-snapshot';
  3. import type {ReplaySpan as TReplaySpan} from 'sentry/views/replays/types';
  4. type FullSnapshotEvent = {
  5. data: {
  6. initialOffset: {
  7. left: number;
  8. top: number;
  9. };
  10. node: serializedNodeWithId;
  11. };
  12. timestamp: number;
  13. type: EventType.FullSnapshot;
  14. };
  15. type BaseReplayProps = {
  16. timestamp: Date;
  17. };
  18. export function ReplaySegmentInit({
  19. height = 600,
  20. href = 'http://localhost/',
  21. timestamp = new Date(),
  22. width = 800,
  23. }: BaseReplayProps & {
  24. height: number;
  25. href: string;
  26. width: number;
  27. }) {
  28. return [
  29. {
  30. type: EventType.DomContentLoaded,
  31. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  32. },
  33. {
  34. type: EventType.Load,
  35. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  36. },
  37. {
  38. type: EventType.Meta,
  39. data: {href, width, height},
  40. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  41. },
  42. ];
  43. }
  44. export function ReplaySegmentFullsnapshot({
  45. timestamp,
  46. childNodes,
  47. }: BaseReplayProps & {childNodes: serializedNodeWithId[]}): [FullSnapshotEvent] {
  48. return [
  49. {
  50. type: EventType.FullSnapshot,
  51. timestamp: timestamp.getTime(),
  52. data: {
  53. initialOffset: {
  54. top: 0,
  55. left: 0,
  56. },
  57. node: {
  58. type: 0, // NodeType.DocumentType
  59. id: 0,
  60. childNodes: [
  61. ReplayRRWebNode({
  62. tagName: 'body',
  63. attributes: {
  64. style:
  65. 'margin:0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu;',
  66. },
  67. childNodes,
  68. }),
  69. ],
  70. },
  71. },
  72. },
  73. ];
  74. }
  75. export function ReplaySegmentConsole({timestamp = new Date()}: BaseReplayProps) {
  76. return ReplaySegmentBreadcrumb({
  77. timestamp,
  78. payload: {
  79. timestamp: timestamp.getTime() / 1000, // sentry data inside rrweb is in seconds
  80. type: 'default',
  81. category: 'console',
  82. data: {
  83. arguments: [
  84. './src/pages/template/Header.js\n Line 14: The href attribute requires a valid value to be accessible. Provide a valid, navigable address as the href value.',
  85. ],
  86. logger: 'console',
  87. },
  88. level: 'warning',
  89. message:
  90. './src/pages/template/Header.js\n Line 14: The href attribute requires a valid value to be accessible. Provide a valid, navigable address as the href value.',
  91. },
  92. });
  93. }
  94. export function ReplaySegmentNavigation({
  95. timestamp = new Date(),
  96. hrefFrom = '/',
  97. hrefTo = '/profile/',
  98. }: BaseReplayProps & {hrefFrom: string; hrefTo: string}) {
  99. return ReplaySegmentBreadcrumb({
  100. timestamp,
  101. payload: {
  102. timestamp: timestamp.getTime() / 1000, // sentry data inside rrweb is in seconds
  103. type: 'default',
  104. category: 'navigation',
  105. data: {
  106. from: hrefFrom,
  107. to: hrefTo,
  108. },
  109. },
  110. });
  111. }
  112. export function ReplaySegmentBreadcrumb({
  113. timestamp = new Date(),
  114. payload,
  115. }: BaseReplayProps & {payload: any}) {
  116. return [
  117. {
  118. type: EventType.Custom,
  119. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  120. data: {
  121. tag: 'breadcrumb',
  122. payload,
  123. },
  124. },
  125. ];
  126. }
  127. export function ReplaySegmentSpan({
  128. timestamp = new Date(),
  129. payload,
  130. }: BaseReplayProps & {payload: any}) {
  131. return [
  132. {
  133. type: EventType.Custom,
  134. timestamp: timestamp.getTime(), // rrweb timestamps are in ms
  135. data: {
  136. tag: 'performanceSpan',
  137. payload,
  138. },
  139. },
  140. ];
  141. }
  142. const nextRRWebId = (function () {
  143. let __rrwebID = 0;
  144. return () => ++__rrwebID;
  145. })();
  146. export function ReplayRRWebNode({
  147. id,
  148. tagName,
  149. attributes,
  150. childNodes,
  151. textContent,
  152. }: {
  153. attributes?: Record<string, string>;
  154. childNodes?: serializedNodeWithId[];
  155. id?: number;
  156. tagName?: string;
  157. textContent?: string;
  158. }): serializedNodeWithId {
  159. id = id ?? nextRRWebId();
  160. if (tagName) {
  161. return {
  162. type: 2, // NodeType.Element
  163. id,
  164. tagName,
  165. attributes: attributes ?? {},
  166. childNodes: childNodes ?? [],
  167. };
  168. }
  169. return {
  170. type: 3, // NodeType.Text
  171. id,
  172. textContent: textContent ?? '',
  173. };
  174. }
  175. export function ReplayRRWebDivHelloWorld() {
  176. return ReplayRRWebNode({
  177. tagName: 'div',
  178. childNodes: [
  179. ReplayRRWebNode({
  180. tagName: 'h1',
  181. attributes: {style: 'text-align: center;'},
  182. childNodes: [
  183. ReplayRRWebNode({
  184. textContent: 'Hello World',
  185. }),
  186. ],
  187. }),
  188. ],
  189. });
  190. }
  191. export function ReplaySpanPayload({
  192. startTimestamp = new Date(),
  193. endTimestamp = new Date(),
  194. ...extra
  195. }: {
  196. op: string;
  197. data?: Record<string, any>;
  198. description?: string;
  199. endTimestamp?: Date;
  200. startTimestamp?: Date;
  201. }): TReplaySpan<Record<string, any>> {
  202. return {
  203. data: {},
  204. id: '0',
  205. ...extra,
  206. startTimestamp: startTimestamp.getTime() / 1000, // in seconds, with ms precision
  207. endTimestamp: endTimestamp?.getTime() / 1000, // in seconds, with ms precision
  208. timestamp: startTimestamp?.getTime(), // in ms, same as startTimestamp
  209. };
  210. }
  211. export function ReplaySpanPayloadNavigate({
  212. description = 'http://test.com',
  213. endTimestamp = new Date(),
  214. startTimestamp = new Date(),
  215. }: {
  216. // The url goes into the description field
  217. description: string;
  218. endTimestamp: Date;
  219. startTimestamp: Date;
  220. }) {
  221. const duration = endTimestamp.getTime() - startTimestamp.getTime(); // in MS
  222. return ReplaySpanPayload({
  223. op: 'navigation.navigate',
  224. description,
  225. startTimestamp,
  226. endTimestamp,
  227. data: {
  228. size: 1149,
  229. decodedBodySize: 1712,
  230. encodedBodySize: 849,
  231. duration,
  232. domInteractive: duration - 200,
  233. domContentLoadedEventStart: duration - 50,
  234. domContentLoadedEventEnd: duration - 48,
  235. loadEventStart: duration, // real value would be approx the same
  236. loadEventEnd: duration, // real value would be approx the same
  237. domComplete: duration, // real value would be approx the same
  238. redirectCount: 0,
  239. },
  240. });
  241. }