spanTreeModel.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. import {Client} from 'app/api';
  2. import SpanTreeModel from 'app/components/events/interfaces/spans/spanTreeModel';
  3. import {EnhancedProcessedSpanType} from 'app/components/events/interfaces/spans/types';
  4. import {
  5. boundsGenerator,
  6. generateRootSpan,
  7. parseTrace,
  8. } from 'app/components/events/interfaces/spans/utils';
  9. import {EntryType, EventTransaction} from 'app/types/event';
  10. import {assert} from 'app/types/utils';
  11. describe('SpanTreeModel', () => {
  12. const api: Client = new Client();
  13. const event = {
  14. id: '2b658a829a21496b87fd1f14a61abf65',
  15. eventID: '2b658a829a21496b87fd1f14a61abf65',
  16. title: '/organizations/:orgId/discover/results/',
  17. type: 'transaction',
  18. startTimestamp: 1622079935.86141,
  19. endTimestamp: 1622079940.032905,
  20. contexts: {
  21. trace: {
  22. trace_id: '8cbbc19c0f54447ab702f00263262726',
  23. span_id: 'a934857184bdf5a6',
  24. op: 'pageload',
  25. status: 'unknown',
  26. type: 'trace',
  27. },
  28. },
  29. entries: [
  30. {
  31. data: [
  32. {
  33. timestamp: 1622079937.227645,
  34. start_timestamp: 1622079936.90689,
  35. description: 'GET /api/0/organizations/?member=1',
  36. op: 'http',
  37. span_id: 'b23703998ae619e7',
  38. parent_span_id: 'a934857184bdf5a6',
  39. trace_id: '8cbbc19c0f54447ab702f00263262726',
  40. status: 'ok',
  41. tags: {
  42. 'http.status_code': '200',
  43. },
  44. data: {
  45. method: 'GET',
  46. type: 'fetch',
  47. url: '/api/0/organizations/?member=1',
  48. },
  49. },
  50. {
  51. timestamp: 1622079937.20331,
  52. start_timestamp: 1622079936.907515,
  53. description: 'GET /api/0/internal/health/',
  54. op: 'http',
  55. span_id: 'a453cc713e5baf9c',
  56. parent_span_id: 'a934857184bdf5a6',
  57. trace_id: '8cbbc19c0f54447ab702f00263262726',
  58. status: 'ok',
  59. tags: {
  60. 'http.status_code': '200',
  61. },
  62. data: {
  63. method: 'GET',
  64. type: 'fetch',
  65. url: '/api/0/internal/health/',
  66. },
  67. },
  68. {
  69. timestamp: 1622079936.05839,
  70. start_timestamp: 1622079936.048125,
  71. description: '/_static/dist/sentry/sentry.541f5b.css',
  72. op: 'resource.link',
  73. span_id: 'a23f26b939d1a735',
  74. parent_span_id: 'a453cc713e5baf9c',
  75. trace_id: '8cbbc19c0f54447ab702f00263262726',
  76. data: {
  77. 'Decoded Body Size': 159248,
  78. 'Encoded Body Size': 159248,
  79. 'Transfer Size': 275,
  80. },
  81. },
  82. ],
  83. type: EntryType.SPANS,
  84. },
  85. ],
  86. } as EventTransaction;
  87. // @ts-expect-error
  88. MockApiClient.addMockResponse({
  89. url: '/organizations/sentry/events/project:19c403a10af34db2b7d93ad669bb51ed/',
  90. body: {
  91. ...event,
  92. contexts: {
  93. trace: {
  94. trace_id: '61d2d7c5acf448ffa8e2f8f973e2cd36',
  95. span_id: 'a5702f287954a9ef',
  96. parent_span_id: 'b23703998ae619e7',
  97. op: 'something',
  98. status: 'unknown',
  99. type: 'trace',
  100. },
  101. },
  102. entries: [
  103. {
  104. data: [
  105. {
  106. timestamp: 1622079937.227645,
  107. start_timestamp: 1622079936.90689,
  108. description: 'something child',
  109. op: 'child',
  110. span_id: 'bcbea9f18a11e161',
  111. parent_span_id: 'a5702f287954a9ef',
  112. trace_id: '61d2d7c5acf448ffa8e2f8f973e2cd36',
  113. status: 'ok',
  114. data: {},
  115. },
  116. ],
  117. type: EntryType.SPANS,
  118. },
  119. ],
  120. },
  121. });
  122. // @ts-expect-error
  123. MockApiClient.addMockResponse({
  124. url: '/organizations/sentry/events/project:broken/',
  125. body: {
  126. ...event,
  127. },
  128. statusCode: 500,
  129. });
  130. it('makes children', () => {
  131. const parsedTrace = parseTrace(event);
  132. const rootSpan = generateRootSpan(parsedTrace);
  133. const spanTreeModel = new SpanTreeModel(rootSpan, parsedTrace.childSpans, api);
  134. expect(spanTreeModel.children).toHaveLength(2);
  135. });
  136. it('handles recursive children', () => {
  137. const event2 = {
  138. ...event,
  139. entries: [
  140. {
  141. data: [
  142. {
  143. timestamp: 1622079937.227645,
  144. start_timestamp: 1622079936.90689,
  145. description: 'GET /api/0/organizations/?member=1',
  146. op: 'http',
  147. span_id: 'a934857184bdf5a6',
  148. parent_span_id: 'a934857184bdf5a6',
  149. trace_id: '8cbbc19c0f54447ab702f00263262726',
  150. status: 'ok',
  151. tags: {
  152. 'http.status_code': '200',
  153. },
  154. data: {
  155. method: 'GET',
  156. type: 'fetch',
  157. url: '/api/0/organizations/?member=1',
  158. },
  159. },
  160. ],
  161. type: EntryType.SPANS,
  162. },
  163. ],
  164. } as EventTransaction;
  165. const parsedTrace = parseTrace(event2);
  166. const rootSpan = generateRootSpan(parsedTrace);
  167. const spanTreeModel = new SpanTreeModel(rootSpan, parsedTrace.childSpans, api);
  168. expect(spanTreeModel.children).toHaveLength(1);
  169. });
  170. it('operationNameCounts', () => {
  171. const parsedTrace = parseTrace(event);
  172. const rootSpan = generateRootSpan(parsedTrace);
  173. const spanTreeModel = new SpanTreeModel(rootSpan, parsedTrace.childSpans, api);
  174. expect(Object.fromEntries(spanTreeModel.operationNameCounts)).toMatchObject({
  175. http: 2,
  176. pageload: 1,
  177. 'resource.link': 1,
  178. });
  179. });
  180. it('toggleEmbeddedChildren - happy path', async () => {
  181. const parsedTrace = parseTrace(event);
  182. const rootSpan = generateRootSpan(parsedTrace);
  183. const spanTreeModel = new SpanTreeModel(rootSpan, parsedTrace.childSpans, api);
  184. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe('idle');
  185. const fullWaterfall: EnhancedProcessedSpanType[] = [
  186. {
  187. type: 'span',
  188. span: {
  189. trace_id: '8cbbc19c0f54447ab702f00263262726',
  190. span_id: 'a934857184bdf5a6',
  191. parent_span_id: undefined,
  192. start_timestamp: 1622079935.86141,
  193. timestamp: 1622079940.032905,
  194. op: 'pageload',
  195. description: undefined,
  196. data: {},
  197. status: 'unknown',
  198. },
  199. numOfSpanChildren: 2,
  200. treeDepth: 0,
  201. isLastSibling: true,
  202. continuingTreeDepths: [],
  203. showEmbeddedChildren: false,
  204. toggleEmbeddedChildren: expect.any(Function),
  205. fetchEmbeddedChildrenState: 'idle',
  206. toggleSpanGroup: undefined,
  207. },
  208. {
  209. type: 'span',
  210. span: {
  211. timestamp: 1622079937.227645,
  212. start_timestamp: 1622079936.90689,
  213. description: 'GET /api/0/organizations/?member=1',
  214. op: 'http',
  215. span_id: 'b23703998ae619e7',
  216. parent_span_id: 'a934857184bdf5a6',
  217. trace_id: '8cbbc19c0f54447ab702f00263262726',
  218. status: 'ok',
  219. tags: {
  220. 'http.status_code': '200',
  221. },
  222. data: {
  223. method: 'GET',
  224. type: 'fetch',
  225. url: '/api/0/organizations/?member=1',
  226. },
  227. },
  228. numOfSpanChildren: 0,
  229. treeDepth: 1,
  230. isLastSibling: false,
  231. continuingTreeDepths: [],
  232. showEmbeddedChildren: false,
  233. toggleEmbeddedChildren: expect.any(Function),
  234. fetchEmbeddedChildrenState: 'idle',
  235. toggleSpanGroup: undefined,
  236. },
  237. {
  238. type: 'span',
  239. span: {
  240. timestamp: 1622079937.20331,
  241. start_timestamp: 1622079936.907515,
  242. description: 'GET /api/0/internal/health/',
  243. op: 'http',
  244. span_id: 'a453cc713e5baf9c',
  245. parent_span_id: 'a934857184bdf5a6',
  246. trace_id: '8cbbc19c0f54447ab702f00263262726',
  247. status: 'ok',
  248. tags: {
  249. 'http.status_code': '200',
  250. },
  251. data: {
  252. method: 'GET',
  253. type: 'fetch',
  254. url: '/api/0/internal/health/',
  255. },
  256. },
  257. numOfSpanChildren: 1,
  258. treeDepth: 1,
  259. isLastSibling: true,
  260. continuingTreeDepths: [],
  261. showEmbeddedChildren: false,
  262. toggleEmbeddedChildren: expect.any(Function),
  263. fetchEmbeddedChildrenState: 'idle',
  264. toggleSpanGroup: undefined,
  265. },
  266. {
  267. type: 'span',
  268. span: {
  269. timestamp: 1622079936.05839,
  270. start_timestamp: 1622079936.048125,
  271. description: '/_static/dist/sentry/sentry.541f5b.css',
  272. op: 'resource.link',
  273. span_id: 'a23f26b939d1a735',
  274. parent_span_id: 'a453cc713e5baf9c',
  275. trace_id: '8cbbc19c0f54447ab702f00263262726',
  276. data: {
  277. 'Decoded Body Size': 159248,
  278. 'Encoded Body Size': 159248,
  279. 'Transfer Size': 275,
  280. },
  281. },
  282. numOfSpanChildren: 0,
  283. treeDepth: 2,
  284. isLastSibling: true,
  285. continuingTreeDepths: [],
  286. showEmbeddedChildren: false,
  287. toggleEmbeddedChildren: expect.any(Function),
  288. fetchEmbeddedChildrenState: 'idle',
  289. toggleSpanGroup: undefined,
  290. },
  291. ];
  292. const generateBounds = boundsGenerator({
  293. traceStartTimestamp: parsedTrace.traceStartTimestamp,
  294. traceEndTimestamp: parsedTrace.traceEndTimestamp,
  295. viewStart: 0,
  296. viewEnd: 1,
  297. });
  298. let spans = spanTreeModel.getSpansList({
  299. operationNameFilters: {
  300. type: 'no_filter',
  301. },
  302. generateBounds,
  303. treeDepth: 0,
  304. isLastSibling: true,
  305. continuingTreeDepths: [],
  306. hiddenSpanGroups: new Set(),
  307. spanGroups: new Set(),
  308. filterSpans: undefined,
  309. previousSiblingEndTimestamp: undefined,
  310. event,
  311. isOnlySibling: true,
  312. spanGrouping: undefined,
  313. toggleSpanGroup: undefined,
  314. showSpanGroup: false,
  315. addTraceBounds: () => {},
  316. removeTraceBounds: () => {},
  317. });
  318. expect(spans).toEqual(fullWaterfall);
  319. let mockAddTraceBounds = jest.fn();
  320. let mockRemoveTraceBounds = jest.fn();
  321. // embed a child transaction
  322. let promise = spanTreeModel.toggleEmbeddedChildren({
  323. addTraceBounds: mockAddTraceBounds,
  324. removeTraceBounds: mockRemoveTraceBounds,
  325. })({
  326. orgSlug: 'sentry',
  327. eventSlug: 'project:19c403a10af34db2b7d93ad669bb51ed',
  328. });
  329. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe(
  330. 'loading_embedded_transactions'
  331. );
  332. await promise;
  333. expect(mockAddTraceBounds).toHaveBeenCalled();
  334. expect(mockRemoveTraceBounds).not.toHaveBeenCalled();
  335. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe('idle');
  336. spans = spanTreeModel.getSpansList({
  337. operationNameFilters: {
  338. type: 'no_filter',
  339. },
  340. generateBounds,
  341. treeDepth: 0,
  342. isLastSibling: true,
  343. continuingTreeDepths: [],
  344. hiddenSpanGroups: new Set(),
  345. spanGroups: new Set(),
  346. filterSpans: undefined,
  347. previousSiblingEndTimestamp: undefined,
  348. event,
  349. isOnlySibling: true,
  350. spanGrouping: undefined,
  351. toggleSpanGroup: undefined,
  352. showSpanGroup: false,
  353. addTraceBounds: () => {},
  354. removeTraceBounds: () => {},
  355. });
  356. const fullWaterfallExpected: EnhancedProcessedSpanType[] = [...fullWaterfall];
  357. fullWaterfallExpected.splice(
  358. 1,
  359. 0,
  360. // Expect these spans to be embedded
  361. {
  362. type: 'span',
  363. span: {
  364. trace_id: '61d2d7c5acf448ffa8e2f8f973e2cd36',
  365. span_id: 'a5702f287954a9ef',
  366. parent_span_id: 'b23703998ae619e7',
  367. start_timestamp: 1622079935.86141,
  368. timestamp: 1622079940.032905,
  369. op: 'something',
  370. description: undefined,
  371. data: {},
  372. status: 'unknown',
  373. },
  374. numOfSpanChildren: 1,
  375. treeDepth: 1,
  376. isLastSibling: false,
  377. continuingTreeDepths: [],
  378. showEmbeddedChildren: false,
  379. toggleEmbeddedChildren: expect.any(Function),
  380. fetchEmbeddedChildrenState: 'idle',
  381. toggleSpanGroup: undefined,
  382. },
  383. {
  384. type: 'span',
  385. span: {
  386. trace_id: '61d2d7c5acf448ffa8e2f8f973e2cd36',
  387. span_id: 'bcbea9f18a11e161',
  388. parent_span_id: 'a5702f287954a9ef',
  389. start_timestamp: 1622079936.90689,
  390. timestamp: 1622079937.227645,
  391. op: 'child',
  392. description: 'something child',
  393. data: {},
  394. status: 'ok',
  395. },
  396. numOfSpanChildren: 0,
  397. treeDepth: 2,
  398. isLastSibling: true,
  399. continuingTreeDepths: [1],
  400. showEmbeddedChildren: false,
  401. toggleEmbeddedChildren: expect.any(Function),
  402. fetchEmbeddedChildrenState: 'idle',
  403. toggleSpanGroup: undefined,
  404. }
  405. );
  406. fullWaterfallExpected[0] = {
  407. ...fullWaterfallExpected[0],
  408. };
  409. assert(fullWaterfallExpected[0].type === 'span');
  410. fullWaterfallExpected[0].numOfSpanChildren += 1;
  411. fullWaterfallExpected[0].showEmbeddedChildren = true;
  412. expect(spans).toEqual(fullWaterfallExpected);
  413. mockAddTraceBounds = jest.fn();
  414. mockRemoveTraceBounds = jest.fn();
  415. // un-embed a child transaction
  416. promise = spanTreeModel.toggleEmbeddedChildren({
  417. addTraceBounds: mockAddTraceBounds,
  418. removeTraceBounds: mockRemoveTraceBounds,
  419. })({
  420. orgSlug: 'sentry',
  421. eventSlug: 'project:19c403a10af34db2b7d93ad669bb51ed',
  422. });
  423. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe('idle');
  424. await promise;
  425. expect(mockAddTraceBounds).not.toHaveBeenCalled();
  426. expect(mockRemoveTraceBounds).toHaveBeenCalled();
  427. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe('idle');
  428. spans = spanTreeModel.getSpansList({
  429. operationNameFilters: {
  430. type: 'no_filter',
  431. },
  432. generateBounds,
  433. treeDepth: 0,
  434. isLastSibling: true,
  435. continuingTreeDepths: [],
  436. hiddenSpanGroups: new Set(),
  437. spanGroups: new Set(),
  438. filterSpans: undefined,
  439. previousSiblingEndTimestamp: undefined,
  440. event,
  441. isOnlySibling: true,
  442. spanGrouping: undefined,
  443. toggleSpanGroup: undefined,
  444. showSpanGroup: false,
  445. addTraceBounds: () => {},
  446. removeTraceBounds: () => {},
  447. });
  448. expect(spans).toEqual(fullWaterfall);
  449. });
  450. it('toggleEmbeddedChildren - error state', async () => {
  451. const parsedTrace = parseTrace(event);
  452. const rootSpan = generateRootSpan(parsedTrace);
  453. const spanTreeModel = new SpanTreeModel(rootSpan, parsedTrace.childSpans, api);
  454. const promise = spanTreeModel.toggleEmbeddedChildren({
  455. addTraceBounds: () => {},
  456. removeTraceBounds: () => {},
  457. })({
  458. orgSlug: 'sentry',
  459. eventSlug: 'project:broken',
  460. });
  461. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe(
  462. 'loading_embedded_transactions'
  463. );
  464. await promise;
  465. expect(spanTreeModel.fetchEmbeddedChildrenState).toBe(
  466. 'error_fetching_embedded_transactions'
  467. );
  468. });
  469. });