traceTree.spec.tsx 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {EntryType} from 'sentry/types/event';
  3. import type {SiblingAutogroupNode} from 'sentry/views/performance/newTraceDetails/traceModels/siblingAutogroupNode';
  4. import {DEFAULT_TRACE_VIEW_PREFERENCES} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences';
  5. import type {ReplayRecord} from 'sentry/views/replays/types';
  6. import {
  7. isMissingInstrumentationNode,
  8. isParentAutogroupedNode,
  9. isSiblingAutogroupedNode,
  10. isSpanNode,
  11. isTransactionNode,
  12. } from './../traceGuards';
  13. import type {ParentAutogroupNode} from './parentAutogroupNode';
  14. import {TraceTree} from './traceTree';
  15. import {
  16. assertTransactionNode,
  17. makeEventTransaction,
  18. makeSpan,
  19. makeTrace,
  20. makeTraceError,
  21. makeTracePerformanceIssue,
  22. makeTransaction,
  23. } from './traceTreeTestUtils';
  24. function mockSpansResponse(
  25. spans: TraceTree.Span[],
  26. project_slug: string,
  27. event_id: string
  28. ): jest.Mock<any, any> {
  29. return MockApiClient.addMockResponse({
  30. url: `/organizations/org-slug/events/${project_slug}:${event_id}/?averageColumn=span.self_time&averageColumn=span.duration`,
  31. method: 'GET',
  32. body: makeEventTransaction({
  33. entries: [{type: EntryType.SPANS, data: spans}],
  34. }),
  35. });
  36. }
  37. const start = new Date('2024-02-29T00:00:00Z').getTime() / 1e3;
  38. const end = new Date('2024-02-29T00:00:00Z').getTime() / 1e3 + 5;
  39. const traceMetadata = {replay: null, meta: null};
  40. const trace = makeTrace({
  41. transactions: [
  42. makeTransaction({
  43. start_timestamp: start,
  44. timestamp: start + 2,
  45. children: [makeTransaction({start_timestamp: start + 1, timestamp: start + 4})],
  46. }),
  47. ],
  48. orphan_errors: [],
  49. });
  50. const traceWithEventId = makeTrace({
  51. transactions: [
  52. makeTransaction({
  53. event_id: 'event-id',
  54. start_timestamp: start,
  55. timestamp: start + 2,
  56. project_slug: 'project',
  57. children: [
  58. makeTransaction({
  59. start_timestamp: start + 1,
  60. timestamp: start + 4,
  61. event_id: 'child-event-id',
  62. project_slug: 'project',
  63. }),
  64. ],
  65. }),
  66. ],
  67. });
  68. const traceWithVitals = makeTrace({
  69. transactions: [
  70. makeTransaction({
  71. start_timestamp: start,
  72. timestamp: start + 2,
  73. measurements: {ttfb: {value: 0, unit: 'millisecond'}},
  74. }),
  75. ],
  76. });
  77. const traceWithOrphanError = makeTrace({
  78. transactions: [
  79. makeTransaction({
  80. start_timestamp: start,
  81. timestamp: start + 2,
  82. children: [makeTransaction({start_timestamp: start + 1, timestamp: start + 2})],
  83. }),
  84. ],
  85. orphan_errors: [makeTraceError({level: 'error', timestamp: end})],
  86. });
  87. const outOfOrderTrace = makeTrace({
  88. transactions: [
  89. makeTransaction({
  90. start_timestamp: 1,
  91. transaction: 'last',
  92. children: [],
  93. }),
  94. makeTransaction({start_timestamp: 0, transaction: 'first'}),
  95. ],
  96. });
  97. const siblingAutogroupSpans = [
  98. makeSpan({
  99. op: 'db',
  100. description: 'redis',
  101. start_timestamp: start,
  102. timestamp: start + 1,
  103. }),
  104. makeSpan({
  105. op: 'db',
  106. description: 'redis',
  107. start_timestamp: start,
  108. timestamp: start + 1,
  109. }),
  110. makeSpan({
  111. op: 'db',
  112. description: 'redis',
  113. start_timestamp: start,
  114. timestamp: start + 1,
  115. }),
  116. makeSpan({
  117. op: 'db',
  118. description: 'redis',
  119. start_timestamp: start,
  120. timestamp: start + 1,
  121. }),
  122. makeSpan({
  123. op: 'db',
  124. description: 'redis',
  125. start_timestamp: start,
  126. timestamp: start + 1,
  127. }),
  128. ];
  129. const parentAutogroupSpans = [
  130. makeSpan({op: 'db', description: 'redis', span_id: '0000'}),
  131. makeSpan({op: 'db', description: 'redis', span_id: '0001', parent_span_id: '0000'}),
  132. ];
  133. const parentAutogroupSpansWithTailChildren = [
  134. makeSpan({op: 'db', description: 'redis', span_id: '0000'}),
  135. makeSpan({
  136. op: 'db',
  137. description: 'redis',
  138. span_id: '0001',
  139. parent_span_id: '0000',
  140. }),
  141. makeSpan({
  142. op: 'http',
  143. description: 'request',
  144. span_id: '0002',
  145. parent_span_id: '0001',
  146. }),
  147. ];
  148. function findTransactionByEventId(tree: TraceTree, eventId: string) {
  149. return TraceTree.Find(
  150. tree.root,
  151. node => isTransactionNode(node) && node.value.event_id === eventId
  152. );
  153. }
  154. describe('TraceTree', () => {
  155. describe('aggreagate node properties', () => {
  156. it('adds errors to node', () => {
  157. const tree = TraceTree.FromTrace(
  158. makeTrace({
  159. transactions: [
  160. makeTransaction({
  161. errors: [makeTraceError()],
  162. }),
  163. ],
  164. }),
  165. traceMetadata
  166. );
  167. expect(tree.root.children[0].errors.size).toBe(1);
  168. });
  169. it('stores trace error as error on node', () => {
  170. const tree = TraceTree.FromTrace(
  171. makeTrace({
  172. orphan_errors: [makeTraceError()],
  173. }),
  174. traceMetadata
  175. );
  176. expect(tree.root.children[0].children[0].errors.size).toBe(1);
  177. });
  178. it('adds performance issues to node', () => {
  179. const tree = TraceTree.FromTrace(
  180. makeTrace({
  181. transactions: [
  182. makeTransaction({
  183. performance_issues: [makeTracePerformanceIssue()],
  184. }),
  185. ],
  186. }),
  187. traceMetadata
  188. );
  189. expect(tree.root.children[0].children[0].performance_issues.size).toBe(1);
  190. });
  191. it('adds transaction profile to node', () => {
  192. const tree = TraceTree.FromTrace(
  193. makeTrace({
  194. transactions: [
  195. makeTransaction({
  196. profile_id: 'profile-id',
  197. }),
  198. ],
  199. }),
  200. traceMetadata
  201. );
  202. expect(tree.root.children[0].children[0].profiles.length).toBe(1);
  203. });
  204. it('adds continuous profile to node', () => {
  205. const tree = TraceTree.FromTrace(
  206. makeTrace({
  207. transactions: [
  208. makeTransaction({
  209. profiler_id: 'profile-id',
  210. children: [],
  211. }),
  212. ],
  213. }),
  214. traceMetadata
  215. );
  216. expect(tree.root.children[0].children[0].profiles.length).toBe(1);
  217. });
  218. });
  219. describe('adjusts trace start and end', () => {
  220. it('based off min(events.start_timestamp)', () => {
  221. const tree = TraceTree.FromTrace(trace, traceMetadata);
  222. expect(tree.root.space[0]).toBe(trace.transactions[0].start_timestamp * 1e3);
  223. });
  224. it('based off max(events.timestamp)', () => {
  225. const tree = TraceTree.FromTrace(trace, traceMetadata);
  226. expect(tree.root.space[1]).toBe(4000);
  227. });
  228. // This happnes for errors only traces
  229. it('end,0 when we cannot construct a timeline', () => {
  230. const tree = TraceTree.FromTrace(
  231. makeTrace({orphan_errors: [makeTraceError({level: 'error', timestamp: end})]}),
  232. traceMetadata
  233. );
  234. expect(tree.root.space[0]).toBe(end * 1e3);
  235. expect(tree.root.space[1]).toBe(0);
  236. });
  237. it('considers all children when inferring start and end', () => {
  238. const tree = TraceTree.FromTrace(
  239. makeTrace({
  240. transactions: [
  241. makeTransaction({
  242. start_timestamp: start,
  243. timestamp: start + 1,
  244. children: [],
  245. }),
  246. makeTransaction({
  247. start_timestamp: start - 1,
  248. timestamp: start + 2,
  249. children: [],
  250. }),
  251. ],
  252. orphan_errors: [],
  253. }),
  254. traceMetadata
  255. );
  256. expect(tree.root.space[1]).toBe(3000);
  257. expect(tree.root.space[0]).toBe(start * 1e3 - 1e3);
  258. });
  259. it('considers orphan errors when inferring end', () => {
  260. const tree = TraceTree.FromTrace(
  261. makeTrace({
  262. transactions: [
  263. makeTransaction({
  264. start_timestamp: start,
  265. timestamp: start + 1,
  266. children: [],
  267. }),
  268. ],
  269. orphan_errors: [
  270. makeTraceError({
  271. level: 'error',
  272. timestamp: start + 5,
  273. }),
  274. ],
  275. }),
  276. traceMetadata
  277. );
  278. expect(tree.root.space[1]).toBe(5000);
  279. expect(tree.root.space[0]).toBe(start * 1e3);
  280. });
  281. it('replay record extends trace start', () => {
  282. const replayStart = new Date('2024-02-29T00:00:00Z').getTime();
  283. const replayEnd = new Date(replayStart + 5000).getTime();
  284. const tree = TraceTree.FromTrace(
  285. makeTrace({
  286. transactions: [
  287. makeTransaction({
  288. start_timestamp: replayStart / 1e3 + 0.1,
  289. timestamp: replayStart / 1e3 + 0.1,
  290. }),
  291. ],
  292. }),
  293. {
  294. meta: null,
  295. replay: {
  296. started_at: new Date(replayStart),
  297. finished_at: new Date(replayEnd),
  298. } as ReplayRecord,
  299. }
  300. );
  301. expect(tree.root.space[0]).toBe(replayStart);
  302. expect(tree.root.space[1]).toBe(5000);
  303. });
  304. it('measurements extend trace start and end', () => {
  305. const tree = TraceTree.FromTrace(
  306. makeTrace({
  307. transactions: [
  308. makeTransaction({
  309. start_timestamp: start,
  310. timestamp: start + 1,
  311. children: [],
  312. measurements: {
  313. ttfb: {
  314. unit: 'millisecond',
  315. value: -5000,
  316. },
  317. lcp: {
  318. unit: 'millisecond',
  319. value: 5000,
  320. },
  321. },
  322. }),
  323. ],
  324. orphan_errors: [],
  325. }),
  326. traceMetadata
  327. );
  328. expect(tree.root.space).toEqual([start * 1e3 - 5000, 10_000]);
  329. });
  330. });
  331. describe('indicators', () => {
  332. it('measurements are converted to indicators', () => {
  333. const tree = TraceTree.FromTrace(traceWithVitals, traceMetadata);
  334. expect(tree.indicators.length).toBe(1);
  335. expect(tree.indicators[0].start).toBe(start * 1e3);
  336. });
  337. it('sorts indicators by start', () => {
  338. const tree = TraceTree.FromTrace(
  339. makeTrace({
  340. transactions: [
  341. makeTransaction({
  342. start_timestamp: 0,
  343. timestamp: 1,
  344. measurements: {
  345. ttfb: {value: 2000, unit: 'millisecond'},
  346. lcp: {value: 1000, unit: 'millisecond'},
  347. },
  348. }),
  349. ],
  350. }),
  351. traceMetadata
  352. );
  353. expect(tree.indicators.length).toBe(2);
  354. expect(tree.indicators[0].start < tree.indicators[1].start).toBe(true);
  355. });
  356. });
  357. describe('FromTrace', () => {
  358. it('assembles tree from trace', () => {
  359. const tree = TraceTree.FromTrace(trace, traceMetadata);
  360. expect(tree.build().serialize()).toMatchSnapshot();
  361. });
  362. it('sorts by start_timestamp', () => {
  363. const tree = TraceTree.FromTrace(outOfOrderTrace, traceMetadata);
  364. expect(tree.build().serialize()).toMatchSnapshot();
  365. });
  366. it('inserts orphan error', () => {
  367. const tree = TraceTree.FromTrace(traceWithOrphanError, {
  368. meta: null,
  369. replay: null,
  370. });
  371. expect(tree.build().serialize()).toMatchSnapshot();
  372. });
  373. it('if parent span does not exist in span tree, the transaction stays under its previous parent', () => {
  374. const tree = TraceTree.FromTrace(
  375. makeTrace({
  376. transactions: [
  377. makeTransaction({
  378. transaction: 'root',
  379. children: [
  380. makeTransaction({transaction: 'child', parent_span_id: 'does not exist'}),
  381. ],
  382. }),
  383. ],
  384. }),
  385. traceMetadata
  386. );
  387. TraceTree.FromSpans(tree.root.children[0], [makeSpan()], makeEventTransaction());
  388. expect(tree.build().serialize()).toMatchSnapshot();
  389. });
  390. it('initializes canFetch based on spanChildrenCount', () => {
  391. const tree = TraceTree.FromTrace(
  392. makeTrace({
  393. transactions: [
  394. makeTransaction({
  395. event_id: 'transaction',
  396. children: [],
  397. }),
  398. makeTransaction({event_id: 'no-span-count-transaction'}),
  399. makeTransaction({event_id: 'no-spans-transaction', children: []}),
  400. ],
  401. }),
  402. {
  403. meta: {
  404. transactiontoSpanChildrenCount: {
  405. transaction: 10,
  406. 'no-spans-transaction': 1,
  407. // we have no data for child transaction
  408. },
  409. errors: 0,
  410. performance_issues: 0,
  411. projects: 0,
  412. transactions: 0,
  413. },
  414. replay: null,
  415. }
  416. );
  417. expect(findTransactionByEventId(tree, 'transaction')?.canFetch).toBe(true);
  418. expect(findTransactionByEventId(tree, 'no-span-count-transaction')?.canFetch).toBe(
  419. true
  420. );
  421. expect(findTransactionByEventId(tree, 'no-spans-transaction')?.canFetch).toBe(
  422. false
  423. );
  424. });
  425. it('initializes canFetch to true if no spanChildrenCount', () => {
  426. const tree = TraceTree.FromTrace(
  427. makeTrace({
  428. transactions: [
  429. makeTransaction({
  430. event_id: 'transaction',
  431. children: [],
  432. }),
  433. ],
  434. }),
  435. {meta: null, replay: null}
  436. );
  437. expect(findTransactionByEventId(tree, 'transaction')?.canFetch).toBe(true);
  438. });
  439. });
  440. describe('events', () => {
  441. it('does not dispatch timeline change when spans fall inside the trace bounds', async () => {
  442. const t = makeTrace({
  443. transactions: [
  444. makeTransaction({
  445. start_timestamp: start,
  446. timestamp: start + 2,
  447. event_id: 'event-id',
  448. project_slug: 'project',
  449. children: [],
  450. }),
  451. ],
  452. orphan_errors: [],
  453. });
  454. const tree = TraceTree.FromTrace(t, traceMetadata);
  455. const listener = jest.fn();
  456. tree.on('trace timeline change', listener);
  457. const txn = TraceTree.Find(tree.root, n => isTransactionNode(n))!;
  458. mockSpansResponse(
  459. [makeSpan({start_timestamp: start + 0.5, timestamp: start + 1})],
  460. 'project',
  461. 'event-id'
  462. );
  463. await tree.zoom(txn, true, {
  464. api: new MockApiClient(),
  465. organization: OrganizationFixture(),
  466. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  467. });
  468. expect(listener).not.toHaveBeenCalled();
  469. });
  470. it('dispatches timeline change when span timestamp > trace timestamp', async () => {
  471. const t = makeTrace({
  472. transactions: [
  473. makeTransaction({
  474. start_timestamp: start,
  475. timestamp: start + 1,
  476. event_id: 'event-id',
  477. project_slug: 'project',
  478. children: [],
  479. }),
  480. ],
  481. orphan_errors: [],
  482. });
  483. const tree = TraceTree.FromTrace(t, traceMetadata);
  484. const listener = jest.fn();
  485. tree.on('trace timeline change', listener);
  486. const txn = TraceTree.Find(tree.root, n => isTransactionNode(n))!;
  487. const transactionSpaceBounds = JSON.stringify(txn.space);
  488. mockSpansResponse(
  489. [makeSpan({start_timestamp: start, timestamp: start + 1.2})],
  490. 'project',
  491. 'event-id'
  492. );
  493. await tree.zoom(txn, true, {
  494. api: new MockApiClient(),
  495. organization: OrganizationFixture(),
  496. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  497. });
  498. expect(JSON.stringify(txn.space)).toEqual(transactionSpaceBounds);
  499. expect(listener).toHaveBeenCalledWith([start * 1e3, 1200]);
  500. });
  501. });
  502. describe('ForEachChild', () => {
  503. it('iterates dfs', () => {
  504. const tree = TraceTree.FromTrace(
  505. makeTrace({
  506. transactions: [
  507. makeTransaction({
  508. transaction: 'root',
  509. children: [
  510. makeTransaction({transaction: 'child'}),
  511. makeTransaction({transaction: 'other_child'}),
  512. ],
  513. }),
  514. ],
  515. }),
  516. {meta: null, replay: null}
  517. );
  518. const visitedNodes: string[] = [];
  519. TraceTree.ForEachChild(tree.root, node => {
  520. if (isTransactionNode(node)) {
  521. visitedNodes.push(node.value.transaction);
  522. }
  523. });
  524. expect(visitedNodes).toEqual(['root', 'child', 'other_child']);
  525. });
  526. });
  527. describe('expand', () => {
  528. it('expanding a parent autogroup node shows head to tail chain', () => {
  529. const tree = TraceTree.FromTrace(trace, traceMetadata);
  530. TraceTree.FromSpans(
  531. tree.root.children[0].children[0],
  532. parentAutogroupSpansWithTailChildren,
  533. makeEventTransaction()
  534. );
  535. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  536. const parentAutogroupNode = TraceTree.Find(tree.root, n =>
  537. isParentAutogroupedNode(n)
  538. )!;
  539. tree.expand(parentAutogroupNode, true);
  540. expect(tree.build().serialize()).toMatchSnapshot();
  541. });
  542. it('collapsing a parent autogroup node shows tail chain', () => {
  543. const tree = TraceTree.FromTrace(trace, traceMetadata);
  544. TraceTree.FromSpans(
  545. tree.root.children[0].children[0],
  546. parentAutogroupSpansWithTailChildren,
  547. makeEventTransaction()
  548. );
  549. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  550. const parentAutogroupNode = TraceTree.Find(tree.root, n =>
  551. isParentAutogroupedNode(n)
  552. )!;
  553. tree.expand(parentAutogroupNode, true);
  554. tree.expand(parentAutogroupNode, false);
  555. expect(tree.build().serialize()).toMatchSnapshot();
  556. });
  557. it('collapsing intermediary children is preserved', () => {
  558. const tree = TraceTree.FromTrace(trace, traceMetadata);
  559. TraceTree.FromSpans(
  560. tree.root.children[0].children[0],
  561. parentAutogroupSpansWithTailChildren,
  562. makeEventTransaction()
  563. );
  564. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  565. const parentAutogroupNode = TraceTree.Find(tree.root, n =>
  566. isParentAutogroupedNode(n)
  567. )! as ParentAutogroupNode;
  568. // Expand the chain and collapse an intermediary child
  569. tree.expand(parentAutogroupNode, true);
  570. tree.expand(parentAutogroupNode.head, false);
  571. const snapshot = tree.build().serialize();
  572. // Collapse the autogroup node and expand it again
  573. tree.expand(parentAutogroupNode, false);
  574. tree.expand(parentAutogroupNode, true);
  575. // Assert that the snapshot is preserved and we only render the parent autogroup chain
  576. // up to the collapsed span
  577. expect(tree.build().serialize()).toEqual(snapshot);
  578. expect(tree.build().serialize()).toMatchSnapshot();
  579. });
  580. it('expanding a sibling autogroup node shows sibling span', () => {
  581. const tree = TraceTree.FromTrace(trace, traceMetadata);
  582. TraceTree.FromSpans(
  583. tree.root.children[0].children[0],
  584. siblingAutogroupSpans,
  585. makeEventTransaction()
  586. );
  587. TraceTree.AutogroupSiblingSpanNodes(tree.root);
  588. TraceTree.ForEachChild(tree.root, n => {
  589. if (isSiblingAutogroupedNode(n)) {
  590. tree.expand(n, true);
  591. }
  592. });
  593. expect(tree.build().serialize()).toMatchSnapshot();
  594. });
  595. it('collapsing a sibling autogroup node hides children', () => {
  596. const tree = TraceTree.FromTrace(trace, traceMetadata);
  597. TraceTree.FromSpans(
  598. tree.root.children[0].children[0],
  599. siblingAutogroupSpans,
  600. makeEventTransaction()
  601. );
  602. TraceTree.AutogroupSiblingSpanNodes(tree.root);
  603. TraceTree.ForEachChild(tree.root, n => {
  604. if (isSiblingAutogroupedNode(n)) {
  605. tree.expand(n, true);
  606. }
  607. });
  608. TraceTree.ForEachChild(tree.root, n => {
  609. if (isSiblingAutogroupedNode(n)) {
  610. tree.expand(n, false);
  611. }
  612. });
  613. expect(tree.build().serialize()).toMatchSnapshot();
  614. });
  615. });
  616. describe('zoom', () => {
  617. it('does nothing if node cannot fetch', () => {
  618. const tree = TraceTree.FromTrace(traceWithEventId, traceMetadata);
  619. const request = mockSpansResponse([], 'project', 'event-id');
  620. tree.root.children[0].children[0].canFetch = false;
  621. tree.zoom(tree.root.children[0].children[0], true, {
  622. api: new MockApiClient(),
  623. organization: OrganizationFixture(),
  624. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  625. });
  626. expect(request).not.toHaveBeenCalled();
  627. });
  628. it('caches promise', () => {
  629. const tree = TraceTree.FromTrace(traceWithEventId, traceMetadata);
  630. const request = mockSpansResponse([], 'project', 'event-id');
  631. tree.zoom(tree.root.children[0].children[0], true, {
  632. api: new MockApiClient(),
  633. organization: OrganizationFixture(),
  634. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  635. });
  636. tree.zoom(tree.root.children[0], true, {
  637. api: new MockApiClient(),
  638. organization: OrganizationFixture(),
  639. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  640. });
  641. expect(request).toHaveBeenCalledTimes(1);
  642. });
  643. it('zooms in on transaction node', async () => {
  644. const tree = TraceTree.FromTrace(traceWithEventId, traceMetadata);
  645. mockSpansResponse([makeSpan()], 'project', 'child-event-id');
  646. // Zoom mutates the list, so we need to build first
  647. tree.build();
  648. await tree.zoom(tree.root.children[0].children[0].children[0], true, {
  649. api: new MockApiClient(),
  650. organization: OrganizationFixture(),
  651. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  652. });
  653. expect(tree.build().serialize()).toMatchSnapshot();
  654. });
  655. it('maintains the span tree when parent is zoomed in', async () => {
  656. const tree = TraceTree.FromTrace(traceWithEventId, traceMetadata);
  657. // Zoom mutates the list, so we need to build first
  658. tree.build();
  659. // Zoom in on child span
  660. mockSpansResponse([makeSpan()], 'project', 'child-event-id');
  661. await tree.zoom(tree.root.children[0].children[0].children[0], true, {
  662. api: new MockApiClient(),
  663. organization: OrganizationFixture(),
  664. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  665. });
  666. // Then zoom in on a parent
  667. mockSpansResponse([makeSpan()], 'project', 'event-id');
  668. await tree.zoom(tree.root.children[0].children[0], true, {
  669. api: new MockApiClient(),
  670. organization: OrganizationFixture(),
  671. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  672. });
  673. expect(tree.build().serialize()).toMatchSnapshot();
  674. });
  675. it('reparents child transactions under spans with matching ids', async () => {
  676. const tree = TraceTree.FromTrace(
  677. makeTrace({
  678. transactions: [
  679. makeTransaction({
  680. transaction: 'root',
  681. event_id: 'parent-event-id',
  682. project_slug: 'project',
  683. children: [
  684. makeTransaction({
  685. transaction: 'child',
  686. parent_span_id: '0000',
  687. event_id: 'child-event-id',
  688. project_slug: 'project',
  689. }),
  690. ],
  691. }),
  692. ],
  693. }),
  694. traceMetadata
  695. );
  696. // Zoom mutates the list, so we need to build first
  697. tree.build();
  698. mockSpansResponse([makeSpan({span_id: '0001'})], 'project', 'child-event-id');
  699. await tree.zoom(tree.root.children[0].children[0].children[0], true, {
  700. api: new MockApiClient(),
  701. organization: OrganizationFixture(),
  702. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  703. });
  704. mockSpansResponse([makeSpan({span_id: '0000'})], 'project', 'parent-event-id');
  705. await tree.zoom(tree.root.children[0].children[0], true, {
  706. api: new MockApiClient(),
  707. organization: OrganizationFixture(),
  708. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  709. });
  710. expect(tree.build().serialize()).toMatchSnapshot();
  711. });
  712. it('preserves parent of nested child transactions', async () => {
  713. const tree = TraceTree.FromTrace(
  714. makeTrace({
  715. transactions: [
  716. makeTransaction({
  717. transaction: 'root',
  718. event_id: 'parent-event-id',
  719. project_slug: 'project',
  720. children: [
  721. makeTransaction({
  722. transaction: 'child',
  723. event_id: 'child-event-id',
  724. project_slug: 'project',
  725. parent_span_id: '0000',
  726. children: [
  727. makeTransaction({
  728. transaction: 'grandchild',
  729. event_id: 'grandchild-event-id',
  730. project_slug: 'project',
  731. }),
  732. ],
  733. }),
  734. ],
  735. }),
  736. ],
  737. }),
  738. traceMetadata
  739. );
  740. // Zoom mutates the list, so we need to build first
  741. tree.build();
  742. mockSpansResponse([makeSpan({span_id: '0000'})], 'project', 'parent-event-id');
  743. await tree.zoom(tree.root.children[0].children[0], true, {
  744. api: new MockApiClient(),
  745. organization: OrganizationFixture(),
  746. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  747. });
  748. const grandchild = findTransactionByEventId(tree, 'grandchild-event-id');
  749. const child = findTransactionByEventId(tree, 'child-event-id');
  750. expect(grandchild?.parent).toBe(child);
  751. expect(tree.serialize()).toMatchSnapshot();
  752. });
  753. it('zoomout returns tree back to a transaction tree', async () => {
  754. const tree = TraceTree.FromTrace(
  755. makeTrace({
  756. transactions: [
  757. makeTransaction({
  758. transaction: 'root',
  759. event_id: 'parent-event-id',
  760. project_slug: 'project',
  761. children: [
  762. makeTransaction({
  763. transaction: 'child',
  764. event_id: 'child-event-id',
  765. project_slug: 'project',
  766. parent_span_id: '0000',
  767. children: [
  768. makeTransaction({
  769. transaction: 'grandchild',
  770. event_id: 'grandchild-event-id',
  771. project_slug: 'project',
  772. }),
  773. ],
  774. }),
  775. ],
  776. }),
  777. ],
  778. }),
  779. traceMetadata
  780. );
  781. // Zoom mutates the list, so we need to build first
  782. const transactionTreeSnapshot = tree.build().serialize();
  783. mockSpansResponse([makeSpan({span_id: '0000'})], 'project', 'parent-event-id');
  784. for (const bool of [true, false]) {
  785. await tree.zoom(tree.root.children[0].children[0], bool, {
  786. api: new MockApiClient(),
  787. organization: OrganizationFixture(),
  788. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  789. });
  790. }
  791. expect(tree.serialize()).toEqual(transactionTreeSnapshot);
  792. });
  793. // @TODO This currently filters out all spans - we should preserve spans that are children of other
  794. // zoomed in transactions
  795. it('zooming out preserves spans of child zoomed in transaction', async () => {
  796. const tree = TraceTree.FromTrace(
  797. makeTrace({
  798. transactions: [
  799. makeTransaction({
  800. transaction: 'root',
  801. event_id: 'parent-event-id',
  802. project_slug: 'project',
  803. children: [
  804. makeTransaction({
  805. transaction: 'child',
  806. event_id: 'child-event-id',
  807. project_slug: 'project',
  808. children: [
  809. makeTransaction({
  810. transaction: 'grandchild',
  811. event_id: 'grandchild-event-id',
  812. project_slug: 'project',
  813. parent_span_id: '0000',
  814. }),
  815. ],
  816. }),
  817. ],
  818. }),
  819. ],
  820. }),
  821. traceMetadata
  822. );
  823. // Zoom mutates the list, so we need to build first
  824. tree.build();
  825. mockSpansResponse(
  826. [makeSpan({span_id: '0000', op: 'parent-op'})],
  827. 'project',
  828. 'child-event-id'
  829. );
  830. const child = findTransactionByEventId(tree, 'child-event-id');
  831. await tree.zoom(child!, true, {
  832. api: new MockApiClient(),
  833. organization: OrganizationFixture(),
  834. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  835. });
  836. mockSpansResponse(
  837. [makeSpan({span_id: '0001', op: 'child-op'})],
  838. 'project',
  839. 'grandchild-event-id'
  840. );
  841. const grandchild = findTransactionByEventId(tree, 'grandchild-event-id');
  842. await tree.zoom(grandchild!, true, {
  843. api: new MockApiClient(),
  844. organization: OrganizationFixture(),
  845. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  846. });
  847. await tree.zoom(child!, false, {
  848. api: new MockApiClient(),
  849. organization: OrganizationFixture(),
  850. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  851. });
  852. const spans = TraceTree.FindAll(tree.root, n => isSpanNode(n));
  853. expect(spans.length).toBe(1);
  854. expect(tree.serialize()).toMatchSnapshot();
  855. });
  856. });
  857. describe('Find', () => {
  858. it('finds first node by predicate', () => {
  859. const tree = TraceTree.FromTrace(
  860. makeTrace({
  861. transactions: [
  862. makeTransaction({
  863. transaction: 'first',
  864. children: [makeTransaction({transaction: 'second'})],
  865. }),
  866. ],
  867. }),
  868. traceMetadata
  869. );
  870. const node = TraceTree.Find(tree.root, n => isTransactionNode(n));
  871. expect(node).not.toBeNull();
  872. expect((node as any).value.transaction).toBe('first');
  873. });
  874. it('returns null if no node is found', () => {
  875. const tree = TraceTree.FromTrace(trace, traceMetadata);
  876. const node = TraceTree.Find(tree.root, n => (n as any) === 'does not exist');
  877. expect(node).toBeNull();
  878. });
  879. });
  880. describe('FindByID', () => {
  881. it('finds transaction by event_id', () => {
  882. const traceWithError = makeTrace({
  883. transactions: [
  884. makeTransaction({transaction: 'first', event_id: 'first-event-id'}),
  885. ],
  886. });
  887. const tree = TraceTree.FromTrace(traceWithError, traceMetadata);
  888. const node = TraceTree.FindByID(tree.root, 'first-event-id');
  889. assertTransactionNode(node);
  890. expect(node.value.transaction).toBe('first');
  891. });
  892. it('matches by error event_id', () => {
  893. const traceWithError = makeTrace({
  894. transactions: [
  895. makeTransaction({
  896. transaction: 'first',
  897. event_id: 'txn-event-id',
  898. errors: [makeTraceError({event_id: 'error-event-id'})],
  899. }),
  900. ],
  901. });
  902. const tree = TraceTree.FromTrace(traceWithError, traceMetadata);
  903. const node = TraceTree.FindByID(tree.root, 'error-event-id');
  904. assertTransactionNode(node);
  905. expect(node.value.transaction).toBe('first');
  906. });
  907. });
  908. describe('FindAll', () => {
  909. it('finds all nodes by predicate', () => {
  910. const tree = TraceTree.FromTrace(trace, traceMetadata);
  911. const nodes = TraceTree.FindAll(tree.root, n => isTransactionNode(n));
  912. expect(nodes.length).toBe(2);
  913. });
  914. });
  915. describe('DirectVisibleChildren', () => {
  916. it('returns children for transaction', () => {
  917. const tree = TraceTree.FromTrace(trace, traceMetadata);
  918. expect(TraceTree.DirectVisibleChildren(tree.root.children[0])).toEqual(
  919. tree.root.children[0].children
  920. );
  921. });
  922. it('returns tail for collapsed parent autogroup', () => {
  923. const tree = TraceTree.FromTrace(trace, traceMetadata);
  924. TraceTree.FromSpans(
  925. tree.root.children[0],
  926. parentAutogroupSpansWithTailChildren,
  927. makeEventTransaction()
  928. );
  929. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  930. const parentAutogroup = TraceTree.Find(tree.root, node =>
  931. isParentAutogroupedNode(node)
  932. ) as ParentAutogroupNode;
  933. expect(parentAutogroup).not.toBeNull();
  934. expect(TraceTree.DirectVisibleChildren(parentAutogroup)[0]).toBe(
  935. parentAutogroup.tail.children[0]
  936. );
  937. });
  938. it('returns head for expanded parent autogroup', () => {
  939. const tree = TraceTree.FromTrace(trace, traceMetadata);
  940. TraceTree.FromSpans(
  941. tree.root.children[0],
  942. parentAutogroupSpans,
  943. makeEventTransaction()
  944. );
  945. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  946. const parentAutogroup = TraceTree.Find(tree.root, node =>
  947. isParentAutogroupedNode(node)
  948. ) as ParentAutogroupNode;
  949. tree.expand(parentAutogroup, true);
  950. expect(TraceTree.DirectVisibleChildren(parentAutogroup)[0]).toBe(
  951. parentAutogroup.head
  952. );
  953. });
  954. });
  955. describe('HasVisibleChildren', () => {
  956. it('true when transaction has children', () => {
  957. const tree = TraceTree.FromTrace(
  958. makeTrace({
  959. transactions: [makeTransaction({children: [makeTransaction()]})],
  960. }),
  961. traceMetadata
  962. );
  963. expect(TraceTree.HasVisibleChildren(tree.root.children[0])).toBe(true);
  964. });
  965. describe('span', () => {
  966. it.each([true, false])('%s when span has children and is expanded', expanded => {
  967. const tree = TraceTree.FromTrace(
  968. makeTrace({
  969. transactions: [makeTransaction({children: [makeTransaction()]})],
  970. }),
  971. traceMetadata
  972. );
  973. TraceTree.FromSpans(
  974. tree.root.children[0],
  975. [
  976. makeSpan({span_id: '0000'}),
  977. makeSpan({span_id: '0001', parent_span_id: '0000'}),
  978. ],
  979. makeEventTransaction()
  980. );
  981. const span = TraceTree.Find(
  982. tree.root,
  983. node => isSpanNode(node) && node.value.span_id === '0000'
  984. )!;
  985. tree.expand(span, expanded);
  986. expect(TraceTree.HasVisibleChildren(span)).toBe(expanded);
  987. });
  988. });
  989. describe('sibling autogroup', () => {
  990. it.each([true, false])('%s when sibling autogroup is expanded', expanded => {
  991. const tree = TraceTree.FromTrace(trace, traceMetadata);
  992. TraceTree.FromSpans(
  993. tree.root.children[0],
  994. siblingAutogroupSpans,
  995. makeEventTransaction()
  996. );
  997. TraceTree.AutogroupSiblingSpanNodes(tree.root);
  998. const siblingAutogroup = TraceTree.Find(tree.root, node =>
  999. isSiblingAutogroupedNode(node)
  1000. );
  1001. tree.expand(siblingAutogroup!, expanded);
  1002. expect(TraceTree.HasVisibleChildren(siblingAutogroup!)).toBe(expanded);
  1003. });
  1004. });
  1005. describe('parent autogroup', () => {
  1006. it.each([true, false])('%s when parent autogroup is expanded', expanded => {
  1007. const tree = TraceTree.FromTrace(trace, traceMetadata);
  1008. TraceTree.FromSpans(
  1009. tree.root.children[0],
  1010. parentAutogroupSpans,
  1011. makeEventTransaction()
  1012. );
  1013. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  1014. const parentAutogroup = TraceTree.Find(tree.root, node =>
  1015. isParentAutogroupedNode(node)
  1016. );
  1017. tree.expand(parentAutogroup!, expanded);
  1018. expect(TraceTree.HasVisibleChildren(parentAutogroup!)).toBe(expanded);
  1019. });
  1020. });
  1021. describe('parent autogroup when tail has children', () => {
  1022. // Always true because tail has children
  1023. it.each([true, false])('%s when parent autogroup is expanded', expanded => {
  1024. const tree = TraceTree.FromTrace(trace, traceMetadata);
  1025. TraceTree.FromSpans(
  1026. tree.root.children[0],
  1027. parentAutogroupSpansWithTailChildren,
  1028. makeEventTransaction()
  1029. );
  1030. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  1031. tree.build();
  1032. const parentAutogroup = TraceTree.Find(tree.root, node =>
  1033. isParentAutogroupedNode(node)
  1034. );
  1035. tree.expand(parentAutogroup!, expanded);
  1036. expect(TraceTree.HasVisibleChildren(parentAutogroup!)).toBe(true);
  1037. });
  1038. });
  1039. });
  1040. describe('IsLastChild', () => {
  1041. it('returns false if node is not last child', () => {
  1042. const tree = TraceTree.FromTrace(
  1043. makeTrace({
  1044. transactions: [makeTransaction(), makeTransaction()],
  1045. }),
  1046. traceMetadata
  1047. );
  1048. expect(TraceTree.IsLastChild(tree.root.children[0].children[0])).toBe(false);
  1049. });
  1050. it('returns true if node is last child', () => {
  1051. const tree = TraceTree.FromTrace(
  1052. makeTrace({
  1053. transactions: [makeTransaction(), makeTransaction()],
  1054. }),
  1055. traceMetadata
  1056. );
  1057. expect(TraceTree.IsLastChild(tree.root.children[0].children[1])).toBe(true);
  1058. });
  1059. });
  1060. describe('Invalidate', () => {
  1061. it('invalidates node', () => {
  1062. const tree = TraceTree.FromTrace(trace, traceMetadata);
  1063. tree.root.children[0].depth = 10;
  1064. tree.root.children[0].connectors = [1, 2, 3];
  1065. TraceTree.invalidate(tree.root.children[0], false);
  1066. expect(tree.root.children[0].depth).toBeUndefined();
  1067. expect(tree.root.children[0].connectors).toBeUndefined();
  1068. });
  1069. it('recursively invalidates children', () => {
  1070. const tree = TraceTree.FromTrace(trace, traceMetadata);
  1071. tree.root.children[0].depth = 10;
  1072. tree.root.children[0].connectors = [1, 2, 3];
  1073. TraceTree.invalidate(tree.root, true);
  1074. expect(tree.root.children[0].depth).toBeUndefined();
  1075. expect(tree.root.children[0].connectors).toBeUndefined();
  1076. });
  1077. });
  1078. describe('appendTree', () => {
  1079. it('appends tree to end of current tree', () => {
  1080. const tree = TraceTree.FromTrace(trace, {replay: null, meta: null});
  1081. tree.appendTree(TraceTree.FromTrace(trace, {replay: null, meta: null}));
  1082. expect(tree.build().serialize()).toMatchSnapshot();
  1083. });
  1084. it('appending extends trace space', () => {
  1085. const tree = TraceTree.FromTrace(
  1086. makeTrace({
  1087. transactions: [makeTransaction({start_timestamp: start, timestamp: start + 1})],
  1088. }),
  1089. {replay: null, meta: null}
  1090. );
  1091. const otherTree = TraceTree.FromTrace(
  1092. makeTrace({
  1093. transactions: [
  1094. makeTransaction({start_timestamp: start, timestamp: start + 10}),
  1095. ],
  1096. }),
  1097. {replay: null, meta: null}
  1098. );
  1099. tree.appendTree(otherTree);
  1100. expect(tree.root.space[0]).toBe(start * 1e3);
  1101. expect(tree.root.space[1]).toBe(10 * 1e3);
  1102. });
  1103. });
  1104. // @TODO: These are helper methods that are in some cases already tested indirectly through the tests above,
  1105. // but it wouldnt hurt to test them explicitly.
  1106. // describe('VisibleChildren', () => {
  1107. // it.todo('expanded transaction children are visible');
  1108. // it.todo('zoomed in transaction children are visible');
  1109. // it.todo('collapsed span children are not visible');
  1110. // it.todo('expanded parent autogroup children shows head to tail chain');
  1111. // it.todo(
  1112. // 'expanded parent autogroup with intermediary collapsed span stop the chain at the collapsed span'
  1113. // );
  1114. // it.todo('collapsed parent autogroup shows tail chain');
  1115. // });
  1116. describe('PathToNode', () => {
  1117. const nestedTransactionTrace = makeTrace({
  1118. transactions: [
  1119. makeTransaction({
  1120. start_timestamp: start,
  1121. timestamp: start + 2,
  1122. transaction: 'parent',
  1123. span_id: 'parent-span-id',
  1124. event_id: 'parent-event-id',
  1125. children: [
  1126. makeTransaction({
  1127. start_timestamp: start + 1,
  1128. timestamp: start + 4,
  1129. transaction: 'child',
  1130. event_id: 'child-event-id',
  1131. }),
  1132. ],
  1133. }),
  1134. ],
  1135. });
  1136. it('path to transaction node', () => {
  1137. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1138. const transactionNode = TraceTree.Find(
  1139. tree.root,
  1140. node => isTransactionNode(node) && node.value.transaction === 'child'
  1141. )!;
  1142. const path = TraceTree.PathToNode(transactionNode);
  1143. expect(path).toEqual(['txn-child-event-id']);
  1144. });
  1145. it('path to span includes parent txn', () => {
  1146. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1147. const child = TraceTree.Find(
  1148. tree.root,
  1149. node => isTransactionNode(node) && node.value.transaction === 'child'
  1150. )!;
  1151. TraceTree.FromSpans(
  1152. child,
  1153. [makeSpan({span_id: 'span-id'})],
  1154. makeEventTransaction()
  1155. );
  1156. const span = TraceTree.Find(tree.root, node => isSpanNode(node))!;
  1157. const path = TraceTree.PathToNode(span);
  1158. expect(path).toEqual(['span-span-id', 'txn-child-event-id']);
  1159. });
  1160. describe('parent autogroup', () => {
  1161. const pathParentAutogroupSpans = [
  1162. makeSpan({op: 'db', description: 'redis', span_id: 'head-span-id'}),
  1163. makeSpan({
  1164. op: 'db',
  1165. description: 'redis',
  1166. span_id: 'tail-span-id',
  1167. parent_span_id: 'head-span-id',
  1168. }),
  1169. makeSpan({
  1170. op: 'http',
  1171. description: 'request',
  1172. span_id: 'child-span-id',
  1173. parent_span_id: 'tail-span-id',
  1174. }),
  1175. ];
  1176. it('parent autogroup', () => {
  1177. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1178. const child = TraceTree.Find(
  1179. tree.root,
  1180. node => isTransactionNode(node) && node.value.transaction === 'child'
  1181. )!;
  1182. TraceTree.FromSpans(child, pathParentAutogroupSpans, makeEventTransaction());
  1183. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  1184. const parentAutogroup = TraceTree.Find(tree.root, node =>
  1185. isParentAutogroupedNode(node)
  1186. )!;
  1187. const path = TraceTree.PathToNode(parentAutogroup);
  1188. expect(path).toEqual(['ag-head-span-id', 'txn-child-event-id']);
  1189. });
  1190. it('path to child of parent autogroup skips autogroup', () => {
  1191. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1192. const child = TraceTree.Find(
  1193. tree.root,
  1194. node => isTransactionNode(node) && node.value.transaction === 'child'
  1195. )!;
  1196. TraceTree.FromSpans(child, pathParentAutogroupSpans, makeEventTransaction());
  1197. TraceTree.AutogroupDirectChildrenSpanNodes(tree.root);
  1198. const parentAutogroup = TraceTree.Find(tree.root, node =>
  1199. isParentAutogroupedNode(node)
  1200. ) as ParentAutogroupNode;
  1201. expect(TraceTree.PathToNode(parentAutogroup.tail)).toEqual([
  1202. 'span-tail-span-id',
  1203. 'txn-child-event-id',
  1204. ]);
  1205. const requestSpan = TraceTree.Find(
  1206. tree.root,
  1207. node => isSpanNode(node) && node.value.description === 'request'
  1208. )!;
  1209. expect(TraceTree.PathToNode(requestSpan)).toEqual([
  1210. 'span-child-span-id',
  1211. 'txn-child-event-id',
  1212. ]);
  1213. });
  1214. });
  1215. describe('sibling autogroup', () => {
  1216. const pathSiblingAutogroupSpans = [
  1217. makeSpan({
  1218. op: 'db',
  1219. description: 'redis',
  1220. span_id: '0',
  1221. start_timestamp: start,
  1222. timestamp: start + 1,
  1223. }),
  1224. makeSpan({
  1225. op: 'db',
  1226. description: 'redis',
  1227. start_timestamp: start,
  1228. timestamp: start + 1,
  1229. span_id: '1',
  1230. }),
  1231. makeSpan({
  1232. op: 'db',
  1233. description: 'redis',
  1234. start_timestamp: start,
  1235. timestamp: start + 1,
  1236. }),
  1237. makeSpan({
  1238. op: 'db',
  1239. description: 'redis',
  1240. start_timestamp: start,
  1241. timestamp: start + 1,
  1242. }),
  1243. makeSpan({
  1244. op: 'db',
  1245. description: 'redis',
  1246. start_timestamp: start,
  1247. timestamp: start + 1,
  1248. }),
  1249. ];
  1250. it('path to sibling autogroup', () => {
  1251. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1252. const child = TraceTree.Find(
  1253. tree.root,
  1254. node => isTransactionNode(node) && node.value.transaction === 'child'
  1255. )!;
  1256. TraceTree.FromSpans(child, pathSiblingAutogroupSpans, makeEventTransaction());
  1257. TraceTree.AutogroupSiblingSpanNodes(tree.root);
  1258. const siblingAutogroup = TraceTree.Find(tree.root, node =>
  1259. isSiblingAutogroupedNode(node)
  1260. ) as SiblingAutogroupNode;
  1261. const path = TraceTree.PathToNode(siblingAutogroup);
  1262. expect(path).toEqual(['ag-0', 'txn-child-event-id']);
  1263. });
  1264. it('path to child of sibling autogroup skips autogroup', () => {
  1265. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1266. const child = TraceTree.Find(
  1267. tree.root,
  1268. node => isTransactionNode(node) && node.value.transaction === 'child'
  1269. )!;
  1270. TraceTree.FromSpans(child, pathSiblingAutogroupSpans, makeEventTransaction());
  1271. TraceTree.AutogroupSiblingSpanNodes(tree.root);
  1272. const siblingAutogroup = TraceTree.Find(tree.root, node =>
  1273. isSiblingAutogroupedNode(node)
  1274. ) as SiblingAutogroupNode;
  1275. const path = TraceTree.PathToNode(siblingAutogroup.children[1]);
  1276. expect(path).toEqual(['span-1', 'txn-child-event-id']);
  1277. });
  1278. });
  1279. it('path to missing instrumentation node', () => {
  1280. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1281. const missingInstrumentationSpans = [
  1282. makeSpan({
  1283. op: 'db',
  1284. description: 'redis',
  1285. span_id: '0',
  1286. start_timestamp: start,
  1287. timestamp: start + 1,
  1288. }),
  1289. makeSpan({
  1290. op: 'db',
  1291. description: 'redis',
  1292. start_timestamp: start + 2,
  1293. timestamp: start + 4,
  1294. }),
  1295. ];
  1296. const child = TraceTree.Find(
  1297. tree.root,
  1298. node => isTransactionNode(node) && node.value.transaction === 'child'
  1299. )!;
  1300. TraceTree.FromSpans(child, missingInstrumentationSpans, makeEventTransaction());
  1301. TraceTree.DetectMissingInstrumentation(tree.root);
  1302. const missingInstrumentationNode = TraceTree.Find(tree.root, node =>
  1303. isMissingInstrumentationNode(node)
  1304. )!;
  1305. const path = TraceTree.PathToNode(missingInstrumentationNode);
  1306. expect(path).toEqual(['ms-0', 'txn-child-event-id']);
  1307. });
  1308. });
  1309. describe('ExpandToPath', () => {
  1310. const organization = OrganizationFixture();
  1311. const api = new MockApiClient();
  1312. const nestedTransactionTrace = makeTrace({
  1313. transactions: [
  1314. makeTransaction({
  1315. start_timestamp: start,
  1316. timestamp: start + 2,
  1317. transaction: 'parent',
  1318. span_id: 'parent-span-id',
  1319. event_id: 'parent-event-id',
  1320. children: [
  1321. makeTransaction({
  1322. start_timestamp: start + 1,
  1323. timestamp: start + 4,
  1324. transaction: 'child',
  1325. event_id: 'child-event-id',
  1326. project_slug: 'project',
  1327. }),
  1328. ],
  1329. }),
  1330. ],
  1331. });
  1332. it('expands transactions from path segments', async () => {
  1333. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1334. const child = TraceTree.Find(
  1335. tree.root,
  1336. node => isTransactionNode(node) && node.value.transaction === 'child'
  1337. )!;
  1338. await TraceTree.ExpandToPath(tree, TraceTree.PathToNode(child), {
  1339. api,
  1340. organization,
  1341. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  1342. });
  1343. expect(tree.build().serialize()).toMatchSnapshot();
  1344. });
  1345. it('discards non txns segments', async () => {
  1346. const tree = TraceTree.FromTrace(nestedTransactionTrace, traceMetadata);
  1347. const child = TraceTree.Find(
  1348. tree.root,
  1349. node => isTransactionNode(node) && node.value.transaction === 'child'
  1350. )!;
  1351. const request = mockSpansResponse([makeSpan()], 'project', 'child-event-id');
  1352. await TraceTree.ExpandToPath(tree, ['span-0', ...TraceTree.PathToNode(child)], {
  1353. api,
  1354. organization,
  1355. preferences: DEFAULT_TRACE_VIEW_PREFERENCES,
  1356. });
  1357. expect(request).toHaveBeenCalled();
  1358. expect(tree.build().serialize()).toMatchSnapshot();
  1359. });
  1360. });
  1361. });