traceTree.spec.tsx 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {waitFor} from 'sentry-test/reactTestingLibrary';
  3. import type {RawSpanType} from 'sentry/components/events/interfaces/spans/types';
  4. import {EntryType, type Event} from 'sentry/types';
  5. import type {
  6. TraceFullDetailed,
  7. TracePerformanceIssue,
  8. TraceSplitResults,
  9. } from 'sentry/utils/performance/quickTrace/types';
  10. import {TraceType} from '../traceDetails/newTraceDetailsContent';
  11. import {
  12. isAutogroupedNode,
  13. isMissingInstrumentationNode,
  14. isSpanNode,
  15. isTraceErrorNode,
  16. isTransactionNode,
  17. } from './guards';
  18. import {
  19. ParentAutogroupNode,
  20. SiblingAutogroupNode,
  21. TraceTree,
  22. TraceTreeNode,
  23. } from './traceTree';
  24. function makeTrace(
  25. overrides: Partial<TraceSplitResults<TraceFullDetailed>>
  26. ): TraceSplitResults<TraceFullDetailed> {
  27. return {
  28. transactions: [],
  29. orphan_errors: [],
  30. ...overrides,
  31. } as TraceSplitResults<TraceFullDetailed>;
  32. }
  33. function makeTransaction(overrides: Partial<TraceFullDetailed> = {}): TraceFullDetailed {
  34. return {
  35. children: [],
  36. start_timestamp: 0,
  37. timestamp: 1,
  38. transaction: 'transaction',
  39. 'transaction.op': '',
  40. 'transaction.status': '',
  41. performance_issues: [],
  42. errors: [],
  43. ...overrides,
  44. } as TraceFullDetailed;
  45. }
  46. function makeSpan(overrides: Partial<RawSpanType> = {}): TraceTree.Span {
  47. return {
  48. op: '',
  49. description: '',
  50. span_id: '',
  51. start_timestamp: 0,
  52. timestamp: 10,
  53. event: makeEvent(),
  54. errors: [],
  55. performance_issues: [],
  56. childTransaction: undefined,
  57. ...overrides,
  58. } as TraceTree.Span;
  59. }
  60. function makeTraceError(
  61. overrides: Partial<TraceTree.TraceError> = {}
  62. ): TraceTree.TraceError {
  63. return {
  64. title: 'MaybeEncodingError: Error sending result',
  65. level: 'error',
  66. event_type: 'error',
  67. data: {},
  68. ...overrides,
  69. } as TraceTree.TraceError;
  70. }
  71. function makeTracePerformanceIssue(
  72. overrides: Partial<TracePerformanceIssue> = {}
  73. ): TracePerformanceIssue {
  74. return {
  75. culprit: 'code',
  76. end: new Date().toISOString(),
  77. span: [],
  78. start: new Date().toISOString(),
  79. suspect_spans: ['sus span'],
  80. type: 0,
  81. issue_short_id: 'issue short id',
  82. ...overrides,
  83. } as TracePerformanceIssue;
  84. }
  85. function makeEvent(overrides: Partial<Event> = {}, spans: RawSpanType[] = []): Event {
  86. return {
  87. entries: [{type: EntryType.SPANS, data: spans}],
  88. ...overrides,
  89. } as Event;
  90. }
  91. function assertSpanNode(
  92. node: TraceTreeNode<TraceTree.NodeValue>
  93. ): asserts node is TraceTreeNode<TraceTree.Span> {
  94. if (!isSpanNode(node)) {
  95. throw new Error('node is not a span');
  96. }
  97. }
  98. // function assertTraceNode(
  99. // node: TraceTreeNode<TraceTree.NodeValue>
  100. // ): asserts node is TraceTreeNode<TraceTree.Trace> {
  101. // if (!isTraceNode(node)) {
  102. // throw new Error('node is not a trace');
  103. // }
  104. // }
  105. function assertTransactionNode(
  106. node: TraceTreeNode<TraceTree.NodeValue> | null
  107. ): asserts node is TraceTreeNode<TraceTree.Transaction> {
  108. if (!node || !isTransactionNode(node)) {
  109. throw new Error('node is not a transaction');
  110. }
  111. }
  112. function assertMissingInstrumentationNode(
  113. node: TraceTreeNode<TraceTree.NodeValue>
  114. ): asserts node is TraceTreeNode<TraceTree.MissingInstrumentationSpan> {
  115. if (!isMissingInstrumentationNode(node)) {
  116. throw new Error('node is not a missing instrumentation node');
  117. }
  118. }
  119. function assertTraceErrorNode(
  120. node: TraceTreeNode<TraceTree.NodeValue>
  121. ): asserts node is TraceTreeNode<TraceTree.TraceError> {
  122. if (!isTraceErrorNode(node)) {
  123. throw new Error('node is not a trace error node');
  124. }
  125. }
  126. function assertAutogroupedNode(
  127. node: TraceTreeNode<TraceTree.NodeValue>
  128. ): asserts node is ParentAutogroupNode | SiblingAutogroupNode {
  129. if (!isAutogroupedNode(node)) {
  130. throw new Error('node is not a autogrouped node');
  131. }
  132. }
  133. function assertParentAutogroupedNode(
  134. node: TraceTreeNode<TraceTree.NodeValue>
  135. ): asserts node is ParentAutogroupNode {
  136. if (!(node instanceof ParentAutogroupNode)) {
  137. throw new Error('node is not a parent autogrouped node');
  138. }
  139. }
  140. function assertSiblingAutogroupedNode(
  141. node: TraceTreeNode<TraceTree.NodeValue>
  142. ): asserts node is ParentAutogroupNode {
  143. if (!(node instanceof SiblingAutogroupNode)) {
  144. throw new Error('node is not a parent node');
  145. }
  146. }
  147. describe('TreeNode', () => {
  148. it('expands transaction nodes by default', () => {
  149. const node = new TraceTreeNode(null, makeTransaction(), {
  150. project_slug: '',
  151. event_id: '',
  152. });
  153. expect(node.expanded).toBe(true);
  154. });
  155. it('points parent to node', () => {
  156. const root = new TraceTreeNode(null, makeTransaction(), {
  157. project_slug: '',
  158. event_id: '',
  159. });
  160. const child = new TraceTreeNode(root, makeTransaction(), {
  161. project_slug: '',
  162. event_id: '',
  163. });
  164. expect(child.parent).toBe(root);
  165. });
  166. it('depth', () => {
  167. const root = new TraceTreeNode(null, makeTransaction(), {
  168. project_slug: '',
  169. event_id: '',
  170. });
  171. const child = new TraceTreeNode(root, makeTransaction(), {
  172. project_slug: '',
  173. event_id: '',
  174. });
  175. const grandChild = new TraceTreeNode(child, makeTransaction(), {
  176. project_slug: '',
  177. event_id: '',
  178. });
  179. expect(grandChild.depth).toBe(1);
  180. });
  181. it('getVisibleChildren', () => {
  182. const root = new TraceTreeNode(null, makeTransaction(), {
  183. project_slug: '',
  184. event_id: '',
  185. });
  186. const child = new TraceTreeNode(root, makeTransaction(), {
  187. project_slug: '',
  188. event_id: '',
  189. });
  190. root.children.push(child);
  191. expect(root.getVisibleChildren()).toHaveLength(1);
  192. expect(root.getVisibleChildren()[0]).toBe(child);
  193. root.expanded = false;
  194. expect(root.getVisibleChildren()).toHaveLength(0);
  195. });
  196. it('getVisibleChildrenCount', () => {
  197. const root = new TraceTreeNode(null, makeTransaction(), {
  198. project_slug: '',
  199. event_id: '',
  200. });
  201. const child = new TraceTreeNode(root, makeTransaction(), {
  202. project_slug: '',
  203. event_id: '',
  204. });
  205. root.children.push(child);
  206. expect(root.getVisibleChildrenCount()).toBe(1);
  207. root.expanded = false;
  208. expect(root.getVisibleChildrenCount()).toBe(0);
  209. });
  210. describe('indicators', () => {
  211. it('collects indicator', () => {
  212. const tree = TraceTree.FromTrace(
  213. makeTrace({
  214. transactions: [
  215. makeTransaction({
  216. start_timestamp: 0,
  217. timestamp: 1,
  218. measurements: {ttfb: {value: 0, unit: 'millisecond'}},
  219. }),
  220. ],
  221. })
  222. );
  223. expect(tree.indicators.length).toBe(1);
  224. expect(tree.indicators[0].start).toBe(0);
  225. });
  226. it('converts timestamp to milliseconds', () => {
  227. const tree = TraceTree.FromTrace(
  228. makeTrace({
  229. transactions: [
  230. makeTransaction({
  231. start_timestamp: 0,
  232. timestamp: 1,
  233. measurements: {
  234. ttfb: {value: 500, unit: 'millisecond'},
  235. fcp: {value: 0.5, unit: 'second'},
  236. lcp: {value: 500_000_000, unit: 'nanosecond'},
  237. },
  238. }),
  239. ],
  240. })
  241. );
  242. expect(tree.indicators[0].start).toBe(500);
  243. expect(tree.indicators[1].start).toBe(500);
  244. expect(tree.indicators[2].start).toBe(500);
  245. });
  246. it('extends end timestamp to include measurement', () => {
  247. const tree = TraceTree.FromTrace(
  248. makeTrace({
  249. transactions: [
  250. makeTransaction({
  251. start_timestamp: 0,
  252. timestamp: 1,
  253. measurements: {
  254. ttfb: {value: 2, unit: 'second'},
  255. },
  256. }),
  257. ],
  258. })
  259. );
  260. expect(tree.root.space).toEqual([0, 2000]);
  261. });
  262. it('adjusts end and converst timestamp to ms', () => {
  263. const tree = TraceTree.FromTrace(
  264. makeTrace({
  265. transactions: [
  266. makeTransaction({
  267. start_timestamp: 0,
  268. timestamp: 1,
  269. measurements: {
  270. ttfb: {value: 2000, unit: 'millisecond'},
  271. },
  272. }),
  273. ],
  274. })
  275. );
  276. expect(tree.root.space).toEqual([0, 2000]);
  277. expect(tree.indicators[0].start).toBe(2000);
  278. });
  279. it('sorts measurements by start', () => {
  280. const tree = TraceTree.FromTrace(
  281. makeTrace({
  282. transactions: [
  283. makeTransaction({
  284. start_timestamp: 0,
  285. timestamp: 1,
  286. measurements: {
  287. ttfb: {value: 2000, unit: 'millisecond'},
  288. lcp: {value: 1000, unit: 'millisecond'},
  289. },
  290. }),
  291. ],
  292. })
  293. );
  294. expect(tree.indicators[0].start).toBe(1000);
  295. expect(tree.indicators[1].start).toBe(2000);
  296. });
  297. });
  298. describe('parent autogrouped node segments', () => {
  299. it('collapses durations', () => {
  300. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  301. project_slug: '',
  302. event_id: '',
  303. });
  304. let parent = root;
  305. for (let i = 0; i < 5; i++) {
  306. const node = new TraceTreeNode(
  307. parent,
  308. makeSpan({
  309. description: 'span',
  310. op: 'db',
  311. start_timestamp: i,
  312. timestamp: i + 1,
  313. span_id: i.toString(),
  314. parent_span_id: parent.value.span_id,
  315. }),
  316. {
  317. project_slug: '',
  318. event_id: '',
  319. }
  320. );
  321. parent.children.push(node);
  322. parent = node;
  323. }
  324. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  325. const autogroupedNode = root.children[0];
  326. assertParentAutogroupedNode(autogroupedNode);
  327. expect(autogroupedNode.autogroupedSegments).toEqual([[0, 5000]]);
  328. });
  329. it('does not collapse durations when there is a gap', () => {
  330. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  331. project_slug: '',
  332. event_id: '',
  333. });
  334. let parent = root;
  335. const ts = [
  336. [0, 1],
  337. [1.5, 2],
  338. [2.5, 3],
  339. [3.5, 4],
  340. [4.5, 5],
  341. ];
  342. for (let i = 0; i < 5; i++) {
  343. const node = new TraceTreeNode(
  344. parent,
  345. makeSpan({
  346. description: 'span',
  347. op: 'db',
  348. start_timestamp: ts[i][0],
  349. timestamp: ts[i][1],
  350. span_id: i.toString(),
  351. parent_span_id: parent.value.span_id,
  352. }),
  353. {
  354. project_slug: '',
  355. event_id: '',
  356. }
  357. );
  358. parent.children.push(node);
  359. parent = node;
  360. }
  361. for (let i = 1; i < ts.length; i++) {
  362. ts[i][0] *= 1000;
  363. ts[i][1] = 0.5 * 1000;
  364. }
  365. ts[0][0] = 0;
  366. ts[0][1] = 1 * 1000;
  367. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  368. const autogroupedNode = root.children[0];
  369. assertParentAutogroupedNode(autogroupedNode);
  370. expect(autogroupedNode.autogroupedSegments).toEqual(ts);
  371. });
  372. });
  373. describe('sibling autogrouped node segments', () => {
  374. it('collapses durations', () => {
  375. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  376. project_slug: '',
  377. event_id: '',
  378. });
  379. for (let i = 0; i < 5; i++) {
  380. root.children.push(
  381. new TraceTreeNode(
  382. root,
  383. makeSpan({
  384. description: 'span',
  385. op: 'db',
  386. start_timestamp: i,
  387. timestamp: i + 1,
  388. }),
  389. {
  390. project_slug: '',
  391. event_id: '',
  392. }
  393. )
  394. );
  395. }
  396. TraceTree.AutogroupSiblingSpanNodes(root);
  397. const autogroupedNode = root.children[0];
  398. assertAutogroupedNode(autogroupedNode);
  399. expect(autogroupedNode.autogroupedSegments).toEqual([[0, 5000]]);
  400. });
  401. it('does not collapse durations when there is a gap', () => {
  402. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  403. project_slug: '',
  404. event_id: '',
  405. });
  406. const ts = [
  407. [0, 1],
  408. [1.5, 2],
  409. [2.5, 3],
  410. [3.5, 4],
  411. [4.5, 5],
  412. ];
  413. for (let i = 0; i < 5; i++) {
  414. root.children.push(
  415. new TraceTreeNode(
  416. root,
  417. makeSpan({
  418. description: 'span',
  419. op: 'db',
  420. start_timestamp: ts[i][0],
  421. timestamp: ts[i][1],
  422. }),
  423. {
  424. project_slug: '',
  425. event_id: '',
  426. }
  427. )
  428. );
  429. }
  430. for (let i = 0; i < ts.length; i++) {
  431. ts[i][0] *= 1000;
  432. ts[i][1] = 0.5 * 1000;
  433. }
  434. ts[0][0] = 0;
  435. ts[0][1] = 1 * 1000;
  436. TraceTree.AutogroupSiblingSpanNodes(root);
  437. const autogroupedNode = root.children[0];
  438. assertAutogroupedNode(autogroupedNode);
  439. expect(autogroupedNode.autogroupedSegments).toEqual(ts);
  440. });
  441. });
  442. describe('path', () => {
  443. describe('nested transactions', () => {
  444. let child: any = null;
  445. for (let i = 0; i < 3; i++) {
  446. const node = new TraceTreeNode(
  447. child,
  448. makeTransaction({
  449. event_id: i === 0 ? 'parent' : i === 1 ? 'child' : 'grandchild',
  450. }),
  451. {
  452. project_slug: '',
  453. event_id: '',
  454. }
  455. );
  456. child = node;
  457. }
  458. it('first txn node', () => {
  459. expect(child.parent.parent.path).toEqual(['txn:parent']);
  460. });
  461. it('leafmost node', () => {
  462. expect(child.path).toEqual(['txn:grandchild', 'txn:child', 'txn:parent']);
  463. });
  464. });
  465. it('orphan errors', () => {
  466. const tree = TraceTree.FromTrace(
  467. makeTrace({
  468. transactions: [],
  469. orphan_errors: [makeTraceError({event_id: 'error_id'})],
  470. })
  471. );
  472. expect(tree.list[1].path).toEqual(['error:error_id']);
  473. });
  474. describe('spans', () => {
  475. const tree = TraceTree.FromTrace(
  476. makeTrace({
  477. transactions: [
  478. makeTransaction({
  479. transaction: '/',
  480. project_slug: 'project',
  481. event_id: 'event_id',
  482. }),
  483. ],
  484. })
  485. );
  486. MockApiClient.addMockResponse({
  487. url: '/organizations/org-slug/events/project:event_id/',
  488. method: 'GET',
  489. body: makeEvent({}, [
  490. makeSpan({
  491. description: 'span',
  492. op: 'db',
  493. span_id: 'span',
  494. start_timestamp: 0,
  495. timestamp: 1,
  496. }),
  497. makeSpan({
  498. description: 'span',
  499. op: 'db',
  500. span_id: 'span',
  501. start_timestamp: 1.5,
  502. timestamp: 2,
  503. }),
  504. ]),
  505. });
  506. tree.zoomIn(tree.list[1], true, {
  507. api: new MockApiClient(),
  508. organization: OrganizationFixture(),
  509. });
  510. it('when span is a child of a txn', async () => {
  511. await waitFor(() => {
  512. expect(tree.list.length).toBe(5);
  513. });
  514. expect(tree.list[tree.list.length - 1].path).toEqual([
  515. 'span:span',
  516. 'txn:event_id',
  517. ]);
  518. });
  519. it('missing instrumentation', () => {
  520. expect(tree.list[3].path).toEqual(['ms:span', 'txn:event_id']);
  521. });
  522. });
  523. describe('autogrouped children', () => {
  524. const tree = TraceTree.FromTrace(
  525. makeTrace({
  526. transactions: [
  527. makeTransaction({
  528. transaction: '/',
  529. project_slug: 'project',
  530. event_id: 'event_id',
  531. }),
  532. ],
  533. })
  534. );
  535. MockApiClient.addMockResponse({
  536. url: '/organizations/org-slug/events/project:event_id/',
  537. method: 'GET',
  538. body: makeEvent({}, [
  539. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  540. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  541. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  542. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  543. ]),
  544. });
  545. tree.zoomIn(tree.list[1], true, {
  546. api: new MockApiClient(),
  547. organization: OrganizationFixture(),
  548. });
  549. it('autogrouped node', async () => {
  550. await waitFor(() => {
  551. expect(tree.list.length).toBe(3);
  552. });
  553. tree.expand(tree.list[2], true);
  554. assertAutogroupedNode(tree.list[2]);
  555. expect(tree.list[2].path).toEqual(['ag:2', 'txn:event_id']);
  556. });
  557. it('child is part of autogrouping', () => {
  558. expect(tree.list[tree.list.length - 1].path).toEqual([
  559. 'span:5',
  560. 'ag:2',
  561. 'txn:event_id',
  562. ]);
  563. });
  564. });
  565. describe('collapses some node by default', () => {
  566. it('android okhttp', async () => {
  567. const tree = TraceTree.FromTrace(
  568. makeTrace({
  569. transactions: [
  570. makeTransaction({
  571. transaction: '/',
  572. project_slug: 'project',
  573. event_id: 'event_id',
  574. }),
  575. ],
  576. })
  577. );
  578. MockApiClient.addMockResponse({
  579. url: '/organizations/org-slug/events/project:event_id/',
  580. method: 'GET',
  581. body: makeEvent({}, [
  582. makeSpan({
  583. description: 'span',
  584. span_id: '2',
  585. op: 'http.client',
  586. origin: 'auto.http.okhttp',
  587. }),
  588. makeSpan({
  589. description: 'span',
  590. op: 'http.client.tls',
  591. span_id: '3',
  592. parent_span_id: '2',
  593. }),
  594. ]),
  595. });
  596. tree.zoomIn(tree.list[1], true, {
  597. api: new MockApiClient(),
  598. organization: OrganizationFixture(),
  599. });
  600. await waitFor(() => {
  601. // trace
  602. // - transaction
  603. // - http.client
  604. // ^ child of http.client is not visible
  605. expect(tree.list.length).toBe(3);
  606. });
  607. });
  608. });
  609. describe('non expanded direct children autogrouped path', () => {
  610. const tree = TraceTree.FromTrace(
  611. makeTrace({
  612. transactions: [
  613. makeTransaction({
  614. transaction: '/',
  615. project_slug: 'project',
  616. event_id: 'event_id',
  617. }),
  618. ],
  619. })
  620. );
  621. MockApiClient.addMockResponse({
  622. url: '/organizations/org-slug/events/project:event_id/',
  623. method: 'GET',
  624. body: makeEvent({}, [
  625. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  626. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  627. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  628. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  629. makeSpan({description: 'span', op: '6', span_id: '6', parent_span_id: '5'}),
  630. ]),
  631. });
  632. tree.zoomIn(tree.list[1], true, {
  633. api: new MockApiClient(),
  634. organization: OrganizationFixture(),
  635. });
  636. it('autogrouped node', async () => {
  637. await waitFor(() => {
  638. expect(tree.list.length).toBe(4);
  639. });
  640. assertAutogroupedNode(tree.list[2]);
  641. expect(tree.list[2].path).toEqual(['ag:2', 'txn:event_id']);
  642. });
  643. it('span node skips autogrouped node because it is not expanded', async () => {
  644. await waitFor(() => {
  645. expect(tree.list.length).toBe(4);
  646. });
  647. expect(tree.list[tree.list.length - 1].path).toEqual(['span:6', 'txn:event_id']);
  648. });
  649. });
  650. });
  651. });
  652. describe('TraceTree', () => {
  653. beforeEach(() => {
  654. MockApiClient.clearMockResponses();
  655. });
  656. it('builds from transactions', () => {
  657. const tree = TraceTree.FromTrace(
  658. makeTrace({
  659. transactions: [
  660. makeTransaction({
  661. children: [],
  662. }),
  663. makeTransaction({
  664. children: [],
  665. }),
  666. ],
  667. })
  668. );
  669. expect(tree.list).toHaveLength(3);
  670. });
  671. it('builds orphan errors', () => {
  672. const tree = TraceTree.FromTrace(
  673. makeTrace({
  674. transactions: [
  675. makeTransaction({
  676. children: [],
  677. }),
  678. makeTransaction({
  679. children: [],
  680. }),
  681. ],
  682. orphan_errors: [makeTraceError()],
  683. })
  684. );
  685. expect(tree.list).toHaveLength(4);
  686. });
  687. it('processes orphan errors without timestamps', () => {
  688. const tree = TraceTree.FromTrace(
  689. makeTrace({
  690. transactions: [
  691. makeTransaction({
  692. children: [],
  693. }),
  694. ],
  695. orphan_errors: [
  696. {
  697. level: 'error',
  698. title: 'Error',
  699. event_type: 'error',
  700. } as TraceTree.TraceError,
  701. ],
  702. })
  703. );
  704. expect(tree.list).toHaveLength(3);
  705. });
  706. it('sorts orphan errors', () => {
  707. const tree = TraceTree.FromTrace(
  708. makeTrace({
  709. transactions: [
  710. makeTransaction({
  711. start_timestamp: 0,
  712. timestamp: 1,
  713. }),
  714. makeTransaction({
  715. start_timestamp: 2,
  716. timestamp: 3,
  717. }),
  718. ],
  719. orphan_errors: [makeTraceError({timestamp: 1, level: 'error'})],
  720. })
  721. );
  722. expect(tree.list).toHaveLength(4);
  723. assertTraceErrorNode(tree.list[2]);
  724. });
  725. it('adjusts trace bounds by orphan error timestamp as well', () => {
  726. const tree = TraceTree.FromTrace(
  727. makeTrace({
  728. transactions: [
  729. makeTransaction({
  730. start_timestamp: 0.1,
  731. timestamp: 0.15,
  732. children: [],
  733. }),
  734. makeTransaction({
  735. start_timestamp: 0.2,
  736. timestamp: 0.25,
  737. children: [],
  738. }),
  739. ],
  740. orphan_errors: [
  741. makeTraceError({timestamp: 0.05}),
  742. makeTraceError({timestamp: 0.3}),
  743. ],
  744. })
  745. );
  746. expect(tree.list).toHaveLength(5);
  747. expect(tree.root.space).toStrictEqual([
  748. 0.05 * tree.root.multiplier,
  749. (0.3 - 0.05) * tree.root.multiplier,
  750. ]);
  751. });
  752. it('calculates correct trace type', () => {
  753. let tree = TraceTree.FromTrace(
  754. makeTrace({
  755. transactions: [],
  756. orphan_errors: [],
  757. })
  758. );
  759. expect(tree.shape).toBe(TraceType.EMPTY_TRACE);
  760. tree = TraceTree.FromTrace(
  761. makeTrace({
  762. transactions: [
  763. makeTransaction({
  764. children: [],
  765. }),
  766. makeTransaction({
  767. children: [],
  768. }),
  769. ],
  770. orphan_errors: [],
  771. })
  772. );
  773. expect(tree.shape).toBe(TraceType.NO_ROOT);
  774. tree = TraceTree.FromTrace(
  775. makeTrace({
  776. transactions: [
  777. makeTransaction({
  778. parent_span_id: null,
  779. children: [],
  780. }),
  781. ],
  782. orphan_errors: [],
  783. })
  784. );
  785. expect(tree.shape).toBe(TraceType.ONE_ROOT);
  786. tree = TraceTree.FromTrace(
  787. makeTrace({
  788. transactions: [
  789. makeTransaction({
  790. parent_span_id: null,
  791. children: [],
  792. }),
  793. makeTransaction({
  794. children: [],
  795. }),
  796. ],
  797. orphan_errors: [],
  798. })
  799. );
  800. expect(tree.shape).toBe(TraceType.BROKEN_SUBTRACES);
  801. tree = TraceTree.FromTrace(
  802. makeTrace({
  803. transactions: [
  804. makeTransaction({
  805. parent_span_id: null,
  806. children: [],
  807. }),
  808. makeTransaction({
  809. parent_span_id: null,
  810. children: [],
  811. }),
  812. ],
  813. orphan_errors: [],
  814. })
  815. );
  816. expect(tree.shape).toBe(TraceType.MULTIPLE_ROOTS);
  817. tree = TraceTree.FromTrace(
  818. makeTrace({
  819. transactions: [],
  820. orphan_errors: [makeTraceError()],
  821. })
  822. );
  823. expect(tree.shape).toBe(TraceType.ONLY_ERRORS);
  824. });
  825. it('builds from spans when root is a transaction node', () => {
  826. const root = new TraceTreeNode(
  827. null,
  828. makeTransaction({
  829. children: [],
  830. }),
  831. {project_slug: '', event_id: ''}
  832. );
  833. const node = TraceTree.FromSpans(
  834. root,
  835. makeEvent(),
  836. [
  837. makeSpan({start_timestamp: 0, op: '1', span_id: '1'}),
  838. makeSpan({start_timestamp: 1, op: '2', span_id: '2', parent_span_id: '1'}),
  839. makeSpan({start_timestamp: 2, op: '3', span_id: '3', parent_span_id: '2'}),
  840. makeSpan({start_timestamp: 3, op: '4', span_id: '4', parent_span_id: '1'}),
  841. ],
  842. {sdk: undefined}
  843. );
  844. if (!isSpanNode(node.children[0])) {
  845. throw new Error('Child needs to be a span');
  846. }
  847. expect(node.children[0].value.span_id).toBe('1');
  848. expect(node.children[0].value.start_timestamp).toBe(0);
  849. expect(node.children.length).toBe(1);
  850. assertSpanNode(node.children[0].children[0]);
  851. assertSpanNode(node.children[0].children[0].children[0]);
  852. assertSpanNode(node.children[0].children[1]);
  853. expect(node.children[0].children[0].value.start_timestamp).toBe(1);
  854. expect(node.children[0].children[0].children[0].value.start_timestamp).toBe(2);
  855. expect(node.children[0].children[1].value.start_timestamp).toBe(3);
  856. });
  857. it('builds from spans and copies txn nodes', () => {
  858. // transaction transaction
  859. // - child transaction -> - span
  860. // - span
  861. // - child-transaction
  862. // - span
  863. const root = new TraceTreeNode(
  864. null,
  865. makeTransaction({
  866. children: [],
  867. }),
  868. {project_slug: '', event_id: ''}
  869. );
  870. root.children.push(
  871. new TraceTreeNode(
  872. root,
  873. makeTransaction({
  874. parent_span_id: 'child-transaction',
  875. }),
  876. {project_slug: '', event_id: ''}
  877. )
  878. );
  879. const node = TraceTree.FromSpans(
  880. root,
  881. makeEvent(),
  882. [
  883. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  884. makeSpan({
  885. start_timestamp: 0.1,
  886. timestamp: 0.2,
  887. op: 'child-transaction',
  888. span_id: 'child-transaction',
  889. }),
  890. makeSpan({start_timestamp: 0.2, timestamp: 0.25, op: 'span', span_id: 'none'}),
  891. ],
  892. {sdk: undefined}
  893. );
  894. assertSpanNode(node.children[1]);
  895. assertTransactionNode(node.children[1].children[0]);
  896. });
  897. it('builds from spans and copies txn nodes to nested children', () => {
  898. // parent transaction parent transaction
  899. // - child transaction -> - span
  900. // - grandchild transaction -> - child-transaction
  901. // - grandchild-transaction
  902. //
  903. const root = new TraceTreeNode(
  904. null,
  905. makeTransaction({
  906. span_id: 'parent-transaction',
  907. children: [],
  908. }),
  909. {project_slug: '', event_id: ''}
  910. );
  911. let start: TraceTreeNode<TraceTree.NodeValue> = root;
  912. for (let i = 0; i < 2; i++) {
  913. const node = new TraceTreeNode(
  914. start,
  915. makeTransaction({
  916. transaction: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  917. parent_span_id: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  918. }),
  919. {project_slug: '', event_id: ''}
  920. );
  921. start.children.push(node);
  922. start = node;
  923. }
  924. const node = TraceTree.FromSpans(
  925. root,
  926. makeEvent(),
  927. [
  928. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  929. makeSpan({
  930. start_timestamp: 0.1,
  931. timestamp: 0.2,
  932. op: 'child-transaction',
  933. span_id: 'child-transaction',
  934. }),
  935. ],
  936. {sdk: undefined}
  937. );
  938. assertSpanNode(node.children[1]);
  939. assertTransactionNode(node.children[1].children[0]);
  940. assertTransactionNode(node.children[1].children[0].children[0]);
  941. });
  942. it('injects missing spans', () => {
  943. const root = new TraceTreeNode(
  944. null,
  945. makeTransaction({
  946. children: [],
  947. }),
  948. {project_slug: '', event_id: ''}
  949. );
  950. const date = new Date().getTime();
  951. const node = TraceTree.FromSpans(
  952. root,
  953. makeEvent(),
  954. [
  955. makeSpan({
  956. start_timestamp: date,
  957. timestamp: date + 1,
  958. span_id: '1',
  959. op: 'span 1',
  960. }),
  961. makeSpan({
  962. start_timestamp: date + 2,
  963. timestamp: date + 4,
  964. op: 'span 2',
  965. span_id: '2',
  966. }),
  967. ],
  968. {sdk: undefined}
  969. );
  970. assertSpanNode(node.children[0]);
  971. assertMissingInstrumentationNode(node.children[1]);
  972. assertSpanNode(node.children[2]);
  973. expect(node.children.length).toBe(3);
  974. expect(node.children[0].value.op).toBe('span 1');
  975. expect(node.children[1].value.type).toBe('missing_instrumentation');
  976. expect(node.children[2].value.op).toBe('span 2');
  977. });
  978. it('does not inject missing spans for javascript platform', () => {
  979. const root = new TraceTreeNode(
  980. null,
  981. makeTransaction({
  982. children: [],
  983. }),
  984. {project_slug: '', event_id: ''}
  985. );
  986. const date = new Date().getTime();
  987. const node = TraceTree.FromSpans(
  988. root,
  989. makeEvent(),
  990. [
  991. makeSpan({
  992. start_timestamp: date,
  993. timestamp: date + 1,
  994. span_id: '1',
  995. op: 'span 1',
  996. }),
  997. makeSpan({
  998. start_timestamp: date + 2,
  999. timestamp: date + 4,
  1000. op: 'span 2',
  1001. span_id: '2',
  1002. }),
  1003. ],
  1004. {sdk: 'sentry.javascript.browser'}
  1005. );
  1006. assertSpanNode(node.children[0]);
  1007. assertSpanNode(node.children[1]);
  1008. expect(node.children.length).toBe(2);
  1009. expect(node.children[0].value.op).toBe('span 1');
  1010. expect(node.children[1].value.op).toBe('span 2');
  1011. });
  1012. it('builds and preserves list order', async () => {
  1013. const organization = OrganizationFixture();
  1014. const api = new MockApiClient();
  1015. const tree = TraceTree.FromTrace(
  1016. makeTrace({
  1017. transactions: [
  1018. makeTransaction({
  1019. transaction: 'txn 1',
  1020. start_timestamp: 0,
  1021. children: [makeTransaction({start_timestamp: 1, transaction: 'txn 2'})],
  1022. }),
  1023. ],
  1024. })
  1025. );
  1026. tree.expand(tree.list[0], true);
  1027. const node = tree.list[1];
  1028. const request = MockApiClient.addMockResponse({
  1029. url: '/organizations/org-slug/events/undefined:undefined/',
  1030. method: 'GET',
  1031. body: makeEvent({startTimestamp: 0}, [
  1032. makeSpan({start_timestamp: 1, op: 'span 1', span_id: '1'}),
  1033. makeSpan({
  1034. start_timestamp: 2,
  1035. op: 'span 2',
  1036. span_id: '2',
  1037. parent_span_id: '1',
  1038. }),
  1039. makeSpan({start_timestamp: 3, op: 'span 3', parent_span_id: '2'}),
  1040. makeSpan({start_timestamp: 4, op: 'span 4', parent_span_id: '1'}),
  1041. ]),
  1042. });
  1043. // 0
  1044. // 1
  1045. // 2
  1046. // 3
  1047. // 4
  1048. tree.zoomIn(node, true, {api, organization});
  1049. await waitFor(() => {
  1050. expect(node.zoomedIn).toBe(true);
  1051. });
  1052. expect(request).toHaveBeenCalled();
  1053. expect(tree.list.length).toBe(6);
  1054. assertTransactionNode(tree.list[1]);
  1055. assertSpanNode(tree.list[2]);
  1056. assertSpanNode(tree.list[3]);
  1057. expect(tree.list[1].value.start_timestamp).toBe(0);
  1058. expect(tree.list[2].value.start_timestamp).toBe(1);
  1059. expect(tree.list[3].value.start_timestamp).toBe(2);
  1060. });
  1061. it('preserves input order', () => {
  1062. const firstChild = makeTransaction({
  1063. start_timestamp: 0,
  1064. timestamp: 1,
  1065. children: [],
  1066. });
  1067. const secondChild = makeTransaction({
  1068. start_timestamp: 1,
  1069. timestamp: 2,
  1070. children: [],
  1071. });
  1072. const tree = TraceTree.FromTrace(
  1073. makeTrace({
  1074. transactions: [
  1075. makeTransaction({
  1076. start_timestamp: 0,
  1077. timestamp: 2,
  1078. children: [firstChild, secondChild],
  1079. }),
  1080. makeTransaction({
  1081. start_timestamp: 2,
  1082. timestamp: 4,
  1083. }),
  1084. ],
  1085. })
  1086. );
  1087. expect(tree.list).toHaveLength(5);
  1088. expect(tree.expand(tree.list[1], false)).toBe(true);
  1089. expect(tree.list).toHaveLength(3);
  1090. expect(tree.expand(tree.list[1], true)).toBe(true);
  1091. expect(tree.list).toHaveLength(5);
  1092. expect(tree.list[2].value).toBe(firstChild);
  1093. expect(tree.list[3].value).toBe(secondChild);
  1094. });
  1095. it('creates children -> parent references', () => {
  1096. const tree = TraceTree.FromTrace(
  1097. makeTrace({
  1098. transactions: [
  1099. makeTransaction({
  1100. start_timestamp: 0,
  1101. timestamp: 2,
  1102. children: [makeTransaction({start_timestamp: 1, timestamp: 2})],
  1103. }),
  1104. makeTransaction({
  1105. start_timestamp: 2,
  1106. timestamp: 4,
  1107. }),
  1108. ],
  1109. })
  1110. );
  1111. expect(tree.list).toHaveLength(4);
  1112. expect(tree.list[2].parent?.value).toBe(tree.list[1].value);
  1113. });
  1114. it('establishes parent-child relationships', () => {
  1115. const tree = TraceTree.FromTrace(
  1116. makeTrace({
  1117. transactions: [
  1118. makeTransaction({
  1119. children: [makeTransaction()],
  1120. }),
  1121. ],
  1122. })
  1123. );
  1124. expect(tree.root.children).toHaveLength(1);
  1125. expect(tree.root.children[0].children).toHaveLength(1);
  1126. });
  1127. it('isLastChild', () => {
  1128. const tree = TraceTree.FromTrace(
  1129. makeTrace({
  1130. transactions: [
  1131. makeTransaction({
  1132. children: [makeTransaction(), makeTransaction()],
  1133. }),
  1134. makeTransaction(),
  1135. ],
  1136. orphan_errors: [],
  1137. })
  1138. );
  1139. tree.expand(tree.list[1], true);
  1140. expect(tree.list[0].isLastChild).toBe(true);
  1141. expect(tree.list[1].isLastChild).toBe(false);
  1142. expect(tree.list[2].isLastChild).toBe(false);
  1143. expect(tree.list[3].isLastChild).toBe(true);
  1144. expect(tree.list[4].isLastChild).toBe(true);
  1145. });
  1146. describe('connectors', () => {
  1147. it('computes transaction connectors', () => {
  1148. const tree = TraceTree.FromTrace(
  1149. makeTrace({
  1150. transactions: [
  1151. makeTransaction({
  1152. transaction: 'sibling',
  1153. children: [
  1154. makeTransaction({transaction: 'child'}),
  1155. makeTransaction({transaction: 'child'}),
  1156. ],
  1157. }),
  1158. makeTransaction({transaction: 'sibling'}),
  1159. ],
  1160. })
  1161. );
  1162. // -1 root
  1163. // ------ list begins here
  1164. // 0 transaction
  1165. // 0 |- sibling
  1166. // -1, 2| | - child
  1167. // -1| | - child
  1168. // 0 |- sibling
  1169. tree.expand(tree.list[1], true);
  1170. expect(tree.list.length).toBe(5);
  1171. expect(tree.list[0].connectors.length).toBe(0);
  1172. expect(tree.list[1].connectors.length).toBe(1);
  1173. expect(tree.list[1].connectors[0]).toBe(-1);
  1174. expect(tree.list[2].connectors[0]).toBe(-1);
  1175. expect(tree.list[2].connectors[1]).toBe(2);
  1176. expect(tree.list[2].connectors.length).toBe(2);
  1177. expect(tree.list[3].connectors[0]).toBe(-1);
  1178. expect(tree.list[3].connectors.length).toBe(1);
  1179. expect(tree.list[4].connectors.length).toBe(0);
  1180. });
  1181. it('computes span connectors', async () => {
  1182. const tree = TraceTree.FromTrace(
  1183. makeTrace({
  1184. transactions: [
  1185. makeTransaction({
  1186. project_slug: 'project',
  1187. event_id: 'event_id',
  1188. transaction: 'transaction',
  1189. children: [],
  1190. }),
  1191. ],
  1192. })
  1193. );
  1194. // root
  1195. // |- node1 []
  1196. // |- node2 []
  1197. MockApiClient.addMockResponse({
  1198. url: '/organizations/org-slug/events/project:event_id/',
  1199. method: 'GET',
  1200. body: makeEvent({}, [makeSpan({start_timestamp: 0, op: 'span', span_id: '1'})]),
  1201. });
  1202. expect(tree.list.length).toBe(2);
  1203. tree.zoomIn(tree.list[1], true, {
  1204. api: new MockApiClient(),
  1205. organization: OrganizationFixture(),
  1206. });
  1207. await waitFor(() => {
  1208. expect(tree.list.length).toBe(3);
  1209. });
  1210. // root
  1211. // |- node1 []
  1212. // |- node2 []
  1213. // |- span1 []
  1214. const span = tree.list[tree.list.length - 1];
  1215. expect(span.connectors.length).toBe(0);
  1216. });
  1217. });
  1218. describe('expanding', () => {
  1219. it('expands a node and updates the list', () => {
  1220. const tree = TraceTree.FromTrace(
  1221. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1222. );
  1223. const node = tree.list[1];
  1224. expect(tree.expand(node, false)).toBe(true);
  1225. expect(tree.list.length).toBe(2);
  1226. expect(node.expanded).toBe(false);
  1227. expect(tree.expand(node, true)).toBe(true);
  1228. expect(node.expanded).toBe(true);
  1229. // Assert that the list has been updated
  1230. expect(tree.list).toHaveLength(3);
  1231. expect(tree.list[2]).toBe(node.children[0]);
  1232. });
  1233. it('collapses a node and updates the list', () => {
  1234. const tree = TraceTree.FromTrace(
  1235. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1236. );
  1237. const node = tree.list[1];
  1238. tree.expand(node, true);
  1239. expect(tree.list.length).toBe(3);
  1240. expect(tree.expand(node, false)).toBe(true);
  1241. expect(node.expanded).toBe(false);
  1242. // Assert that the list has been updated
  1243. expect(tree.list).toHaveLength(2);
  1244. expect(tree.list[1]).toBe(node);
  1245. });
  1246. it('preserves children expanded state', () => {
  1247. const tree = TraceTree.FromTrace(
  1248. makeTrace({
  1249. transactions: [
  1250. makeTransaction({
  1251. children: [
  1252. makeTransaction({children: [makeTransaction({start_timestamp: 1000})]}),
  1253. makeTransaction({start_timestamp: 5}),
  1254. ],
  1255. }),
  1256. ],
  1257. })
  1258. );
  1259. expect(tree.expand(tree.list[2], false)).toBe(true);
  1260. // Assert that the list has been updated
  1261. expect(tree.list).toHaveLength(4);
  1262. expect(tree.expand(tree.list[2], true)).toBe(true);
  1263. expect(tree.list.length).toBe(5);
  1264. expect(tree.list[tree.list.length - 1].value).toEqual(
  1265. makeTransaction({start_timestamp: 5})
  1266. );
  1267. });
  1268. it('expanding or collapsing a zoomed in node doesnt do anything', async () => {
  1269. const organization = OrganizationFixture();
  1270. const api = new MockApiClient();
  1271. const tree = TraceTree.FromTrace(
  1272. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1273. );
  1274. const node = tree.list[0];
  1275. const request = MockApiClient.addMockResponse({
  1276. url: '/organizations/org-slug/events/undefined:undefined/',
  1277. method: 'GET',
  1278. body: makeEvent(),
  1279. });
  1280. tree.zoomIn(node, true, {api, organization});
  1281. await waitFor(() => {
  1282. expect(node.zoomedIn).toBe(true);
  1283. });
  1284. expect(request).toHaveBeenCalled();
  1285. expect(tree.expand(node, true)).toBe(false);
  1286. });
  1287. });
  1288. describe('zooming', () => {
  1289. it('marks node as zoomed in', async () => {
  1290. const organization = OrganizationFixture();
  1291. const api = new MockApiClient();
  1292. const tree = TraceTree.FromTrace(
  1293. makeTrace({
  1294. transactions: [
  1295. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1296. ],
  1297. })
  1298. );
  1299. const request = MockApiClient.addMockResponse({
  1300. url: '/organizations/org-slug/events/project:event_id/',
  1301. method: 'GET',
  1302. body: makeEvent(),
  1303. });
  1304. const node = tree.list[1];
  1305. expect(node.zoomedIn).toBe(false);
  1306. tree.zoomIn(node, true, {api, organization});
  1307. await waitFor(() => {
  1308. expect(node.zoomedIn).toBe(true);
  1309. });
  1310. expect(request).toHaveBeenCalled();
  1311. });
  1312. it('fetches spans for node when zooming in', async () => {
  1313. const tree = TraceTree.FromTrace(
  1314. makeTrace({
  1315. transactions: [
  1316. makeTransaction({
  1317. transaction: 'txn',
  1318. project_slug: 'project',
  1319. event_id: 'event_id',
  1320. }),
  1321. ],
  1322. })
  1323. );
  1324. const request = MockApiClient.addMockResponse({
  1325. url: '/organizations/org-slug/events/project:event_id/',
  1326. method: 'GET',
  1327. body: makeEvent({}, [makeSpan()]),
  1328. });
  1329. const node = tree.list[1];
  1330. expect(node.children).toHaveLength(0);
  1331. tree.zoomIn(node, true, {
  1332. api: new MockApiClient(),
  1333. organization: OrganizationFixture(),
  1334. });
  1335. expect(request).toHaveBeenCalled();
  1336. await waitFor(() => {
  1337. expect(node.children).toHaveLength(1);
  1338. });
  1339. // Assert that the children have been updated
  1340. assertTransactionNode(node.children[0].parent);
  1341. expect(node.children[0].parent.value.transaction).toBe('txn');
  1342. expect(node.children[0].depth).toBe(node.depth + 1);
  1343. });
  1344. it('handles bottom up zooming', async () => {
  1345. const tree = TraceTree.FromTrace(
  1346. makeTrace({
  1347. transactions: [
  1348. makeTransaction({
  1349. transaction: 'transaction',
  1350. project_slug: 'project',
  1351. event_id: 'event_id',
  1352. children: [
  1353. makeTransaction({
  1354. parent_span_id: 'span',
  1355. transaction: 'child transaction',
  1356. project_slug: 'child_project',
  1357. event_id: 'child_event_id',
  1358. }),
  1359. ],
  1360. }),
  1361. ],
  1362. })
  1363. );
  1364. const first_request = MockApiClient.addMockResponse({
  1365. url: '/organizations/org-slug/events/project:event_id/',
  1366. method: 'GET',
  1367. body: makeEvent({}, [makeSpan({op: 'db', span_id: 'span'})]),
  1368. });
  1369. const second_request = MockApiClient.addMockResponse({
  1370. url: '/organizations/org-slug/events/child_project:child_event_id/',
  1371. method: 'GET',
  1372. body: makeEvent({}, [
  1373. makeSpan({op: 'db', span_id: 'span'}),
  1374. makeSpan({op: 'db', span_id: 'span 1', parent_span_id: 'span'}),
  1375. makeSpan({op: 'db', span_id: 'span 2', parent_span_id: 'span 1'}),
  1376. makeSpan({op: 'db', span_id: 'span 3', parent_span_id: 'span 2'}),
  1377. makeSpan({op: 'db', span_id: 'span 4', parent_span_id: 'span 3'}),
  1378. makeSpan({op: 'db', span_id: 'span 5', parent_span_id: 'span 4'}),
  1379. ]),
  1380. });
  1381. tree.zoomIn(tree.list[2], true, {
  1382. api: new MockApiClient(),
  1383. organization: OrganizationFixture(),
  1384. });
  1385. await waitFor(() => {
  1386. expect(second_request).toHaveBeenCalled();
  1387. });
  1388. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1389. tree.zoomIn(tree.list[1], true, {
  1390. api: new MockApiClient(),
  1391. organization: OrganizationFixture(),
  1392. });
  1393. await waitFor(() => {
  1394. expect(first_request).toHaveBeenCalled();
  1395. });
  1396. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1397. });
  1398. it('zooms out', async () => {
  1399. const tree = TraceTree.FromTrace(
  1400. makeTrace({
  1401. transactions: [
  1402. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1403. ],
  1404. })
  1405. );
  1406. MockApiClient.addMockResponse({
  1407. url: '/organizations/org-slug/events/project:event_id/',
  1408. method: 'GET',
  1409. body: makeEvent({}, [makeSpan({span_id: 'span1', description: 'span1'})]),
  1410. });
  1411. tree.zoomIn(tree.list[1], true, {
  1412. api: new MockApiClient(),
  1413. organization: OrganizationFixture(),
  1414. });
  1415. await waitFor(() => {
  1416. assertSpanNode(tree.list[1].children[0]);
  1417. expect(tree.list[1].children[0].value.description).toBe('span1');
  1418. });
  1419. tree.zoomIn(tree.list[1], false, {
  1420. api: new MockApiClient(),
  1421. organization: OrganizationFixture(),
  1422. });
  1423. await waitFor(() => {
  1424. // Assert child no longer points to children
  1425. expect(tree.list[1].zoomedIn).toBe(false);
  1426. expect(tree.list[1].children[0]).toBe(undefined);
  1427. expect(tree.list[2]).toBe(undefined);
  1428. });
  1429. });
  1430. it('zooms in and out', async () => {
  1431. const tree = TraceTree.FromTrace(
  1432. makeTrace({
  1433. transactions: [
  1434. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1435. ],
  1436. })
  1437. );
  1438. MockApiClient.addMockResponse({
  1439. url: '/organizations/org-slug/events/project:event_id/',
  1440. method: 'GET',
  1441. body: makeEvent({}, [makeSpan({span_id: 'span 1', description: 'span1'})]),
  1442. });
  1443. // Zoom in
  1444. tree.zoomIn(tree.list[1], true, {
  1445. api: new MockApiClient(),
  1446. organization: OrganizationFixture(),
  1447. });
  1448. await waitFor(() => {
  1449. expect(tree.list[1].zoomedIn).toBe(true);
  1450. assertSpanNode(tree.list[1].children[0]);
  1451. expect(tree.list[1].children[0].value.description).toBe('span1');
  1452. });
  1453. // Zoom out
  1454. tree.zoomIn(tree.list[1], false, {
  1455. api: new MockApiClient(),
  1456. organization: OrganizationFixture(),
  1457. });
  1458. await waitFor(() => {
  1459. expect(tree.list[2]).toBe(undefined);
  1460. });
  1461. // Zoom in
  1462. tree.zoomIn(tree.list[1], true, {
  1463. api: new MockApiClient(),
  1464. organization: OrganizationFixture(),
  1465. });
  1466. await waitFor(() => {
  1467. assertSpanNode(tree.list[1].children[0]);
  1468. expect(tree.list[1].children[0].value?.description).toBe('span1');
  1469. });
  1470. });
  1471. it('zooms in and out preserving siblings', async () => {
  1472. const tree = TraceTree.FromTrace(
  1473. makeTrace({
  1474. transactions: [
  1475. makeTransaction({
  1476. project_slug: 'project',
  1477. event_id: 'event_id',
  1478. start_timestamp: 0,
  1479. children: [
  1480. makeTransaction({
  1481. start_timestamp: 1,
  1482. timestamp: 2,
  1483. project_slug: 'other_project',
  1484. event_id: 'event_id',
  1485. }),
  1486. makeTransaction({start_timestamp: 2, timestamp: 3}),
  1487. ],
  1488. }),
  1489. ],
  1490. })
  1491. );
  1492. const request = MockApiClient.addMockResponse({
  1493. url: '/organizations/org-slug/events/other_project:event_id/',
  1494. method: 'GET',
  1495. body: makeEvent({}, [makeSpan({description: 'span1'})]),
  1496. });
  1497. tree.expand(tree.list[1], true);
  1498. tree.zoomIn(tree.list[2], true, {
  1499. api: new MockApiClient(),
  1500. organization: OrganizationFixture(),
  1501. });
  1502. expect(request).toHaveBeenCalled();
  1503. // Zoom in
  1504. await waitFor(() => {
  1505. expect(tree.list.length).toBe(5);
  1506. });
  1507. // Zoom out
  1508. tree.zoomIn(tree.list[2], false, {
  1509. api: new MockApiClient(),
  1510. organization: OrganizationFixture(),
  1511. });
  1512. await waitFor(() => {
  1513. expect(tree.list.length).toBe(4);
  1514. });
  1515. });
  1516. it('preserves expanded state when zooming in and out', async () => {
  1517. const tree = TraceTree.FromTrace(
  1518. makeTrace({
  1519. transactions: [
  1520. makeTransaction({
  1521. project_slug: 'project',
  1522. event_id: 'event_id',
  1523. children: [
  1524. makeTransaction({project_slug: 'other_project', event_id: 'event_id'}),
  1525. ],
  1526. }),
  1527. ],
  1528. })
  1529. );
  1530. MockApiClient.addMockResponse({
  1531. url: '/organizations/org-slug/events/project:event_id/',
  1532. method: 'GET',
  1533. body: makeEvent({}, [
  1534. makeSpan({description: 'span1'}),
  1535. makeSpan({description: 'span2'}),
  1536. ]),
  1537. });
  1538. tree.expand(tree.list[1], true);
  1539. expect(tree.list.length).toBe(3);
  1540. tree.zoomIn(tree.list[1], true, {
  1541. api: new MockApiClient(),
  1542. organization: OrganizationFixture(),
  1543. });
  1544. await waitFor(() => {
  1545. expect(tree.list.length).toBe(4);
  1546. });
  1547. tree.zoomIn(tree.list[1], false, {
  1548. api: new MockApiClient(),
  1549. organization: OrganizationFixture(),
  1550. });
  1551. await waitFor(() => {
  1552. expect(tree.list.length).toBe(3);
  1553. });
  1554. expect(tree.list[1].expanded).toBe(true);
  1555. });
  1556. });
  1557. describe('autogrouping', () => {
  1558. it('auto groups sibling spans and preserves tail spans', () => {
  1559. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1560. project_slug: '',
  1561. event_id: '',
  1562. });
  1563. for (let i = 0; i < 5; i++) {
  1564. root.children.push(
  1565. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1566. project_slug: '',
  1567. event_id: '',
  1568. })
  1569. );
  1570. }
  1571. root.children.push(
  1572. new TraceTreeNode(root, makeSpan({description: 'span', op: 'http'}), {
  1573. project_slug: '',
  1574. event_id: '',
  1575. })
  1576. );
  1577. expect(root.children.length).toBe(6);
  1578. TraceTree.AutogroupSiblingSpanNodes(root);
  1579. expect(root.children.length).toBe(2);
  1580. });
  1581. it('autogroups when number of children is exactly 5', () => {
  1582. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1583. project_slug: '',
  1584. event_id: '',
  1585. });
  1586. for (let i = 0; i < 5; i++) {
  1587. root.children.push(
  1588. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1589. project_slug: '',
  1590. event_id: '',
  1591. })
  1592. );
  1593. }
  1594. expect(root.children.length).toBe(5);
  1595. TraceTree.AutogroupSiblingSpanNodes(root);
  1596. expect(root.children.length).toBe(1);
  1597. });
  1598. it('collects errors and performance issues for sibling autogrouped node', () => {
  1599. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1600. project_slug: '',
  1601. event_id: '',
  1602. });
  1603. for (let i = 0; i < 5; i++) {
  1604. const node = new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1605. project_slug: '',
  1606. event_id: '',
  1607. });
  1608. node.errors.add(makeTraceError());
  1609. node.performance_issues.add(makeTracePerformanceIssue());
  1610. root.children.push(node);
  1611. }
  1612. expect(root.children.length).toBe(5);
  1613. TraceTree.AutogroupSiblingSpanNodes(root);
  1614. expect(root.children.length).toBe(1);
  1615. const autogroupedNode = root.children[0];
  1616. assertSiblingAutogroupedNode(autogroupedNode);
  1617. expect(autogroupedNode.has_errors).toBe(true);
  1618. expect(autogroupedNode.errors.size).toBe(5);
  1619. expect(autogroupedNode.performance_issues.size).toBe(5);
  1620. });
  1621. it('adds autogrouped siblings as children under autogrouped node', () => {
  1622. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1623. project_slug: '',
  1624. event_id: '',
  1625. });
  1626. for (let i = 0; i < 5; i++) {
  1627. root.children.push(
  1628. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1629. project_slug: '',
  1630. event_id: '',
  1631. })
  1632. );
  1633. }
  1634. expect(root.children.length).toBe(5);
  1635. TraceTree.AutogroupSiblingSpanNodes(root);
  1636. expect(root.children.length).toBe(1);
  1637. const autoGroupedNode = root.children[0];
  1638. assertAutogroupedNode(autoGroupedNode);
  1639. expect(autoGroupedNode.groupCount).toBe(5);
  1640. expect(autoGroupedNode.children.length).toBe(5);
  1641. });
  1642. it('autogroups when number of children is > 5', () => {
  1643. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1644. project_slug: '',
  1645. event_id: '',
  1646. });
  1647. for (let i = 0; i < 7; i++) {
  1648. root.children.push(
  1649. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1650. project_slug: '',
  1651. event_id: '',
  1652. })
  1653. );
  1654. }
  1655. expect(root.children.length).toBe(7);
  1656. TraceTree.AutogroupSiblingSpanNodes(root);
  1657. expect(root.children.length).toBe(1);
  1658. });
  1659. it('autogroups direct children case', () => {
  1660. // db db db
  1661. // http -> parent autogroup (3) -> parent autogroup (3)
  1662. // http http
  1663. // http http
  1664. // http
  1665. const root: TraceTreeNode<TraceTree.Span> = new TraceTreeNode(
  1666. null,
  1667. makeSpan({
  1668. description: `span1`,
  1669. span_id: `1`,
  1670. op: 'db',
  1671. }),
  1672. {project_slug: '', event_id: ''}
  1673. );
  1674. let last: TraceTreeNode<any> = root;
  1675. for (let i = 0; i < 3; i++) {
  1676. const node = new TraceTreeNode(
  1677. last,
  1678. makeSpan({
  1679. description: `span${i}`,
  1680. span_id: `${i}`,
  1681. op: 'http',
  1682. }),
  1683. {
  1684. project_slug: '',
  1685. event_id: '',
  1686. }
  1687. );
  1688. last.children.push(node);
  1689. last = node;
  1690. }
  1691. if (!root) {
  1692. throw new Error('root is null');
  1693. }
  1694. expect(root.children.length).toBe(1);
  1695. expect(root.children[0].children.length).toBe(1);
  1696. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1697. expect(root.children.length).toBe(1);
  1698. assertAutogroupedNode(root.children[0]);
  1699. expect(root.children[0].children.length).toBe(0);
  1700. root.children[0].expanded = true;
  1701. expect((root.children[0].children[0].value as RawSpanType).description).toBe(
  1702. 'span0'
  1703. );
  1704. });
  1705. it('collects errors and performance issues for parent autogrouped node', () => {
  1706. // db db db
  1707. // http -> parent autogroup (3) -> parent autogroup (3)
  1708. // http http
  1709. // http http
  1710. // http
  1711. const root: TraceTreeNode<TraceTree.Span> = new TraceTreeNode(
  1712. null,
  1713. makeSpan({
  1714. description: `span1`,
  1715. span_id: `1`,
  1716. op: 'db',
  1717. }),
  1718. {project_slug: '', event_id: ''}
  1719. );
  1720. let last: TraceTreeNode<any> = root;
  1721. for (let i = 0; i < 3; i++) {
  1722. const node = new TraceTreeNode(
  1723. last,
  1724. makeSpan({
  1725. description: `span${i}`,
  1726. span_id: `${i}`,
  1727. op: 'http',
  1728. }),
  1729. {
  1730. project_slug: '',
  1731. event_id: '',
  1732. }
  1733. );
  1734. node.errors.add(makeTraceError());
  1735. node.performance_issues.add(makeTracePerformanceIssue());
  1736. last.children.push(node);
  1737. last = node;
  1738. }
  1739. if (!root) {
  1740. throw new Error('root is null');
  1741. }
  1742. expect(root.children.length).toBe(1);
  1743. expect(root.children[0].children.length).toBe(1);
  1744. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1745. expect(root.children.length).toBe(1);
  1746. assertAutogroupedNode(root.children[0]);
  1747. expect(root.children[0].has_errors).toBe(true);
  1748. expect(root.children[0].errors.size).toBe(3);
  1749. expect(root.children[0].performance_issues.size).toBe(3);
  1750. });
  1751. it('autogrouping direct children skips rendering intermediary nodes', () => {
  1752. // db db db
  1753. // http autogrouped (3) autogrouped (3)
  1754. // http -> db -> http
  1755. // http http
  1756. // db http
  1757. // db
  1758. const root = new TraceTreeNode(
  1759. null,
  1760. makeSpan({span_id: 'span1', description: 'span1', op: 'db'}),
  1761. {
  1762. project_slug: '',
  1763. event_id: '',
  1764. }
  1765. );
  1766. let last = root;
  1767. for (let i = 0; i < 4; i++) {
  1768. const node = new TraceTreeNode(
  1769. last,
  1770. makeSpan({
  1771. span_id: `span`,
  1772. description: `span`,
  1773. op: i === 3 ? 'db' : 'http',
  1774. }),
  1775. {
  1776. project_slug: '',
  1777. event_id: '',
  1778. }
  1779. );
  1780. last.children.push(node);
  1781. last = node;
  1782. }
  1783. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1784. const autoGroupedNode = root.children[0];
  1785. assertAutogroupedNode(autoGroupedNode);
  1786. expect(autoGroupedNode.children.length).toBe(1);
  1787. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('db');
  1788. autoGroupedNode.expanded = true;
  1789. expect(autoGroupedNode.children.length).toBe(1);
  1790. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('http');
  1791. });
  1792. it('nested direct autogrouping', () => {
  1793. // db db db
  1794. // http -> parent autogroup (3) -> parent autogroup (3)
  1795. // http db http
  1796. // http parent autogroup (3) http
  1797. // db http
  1798. // http db
  1799. // http parent autogrouped (3)
  1800. // http http
  1801. // http
  1802. // http
  1803. const root = new TraceTreeNode(
  1804. null,
  1805. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1806. {
  1807. project_slug: '',
  1808. event_id: '',
  1809. }
  1810. );
  1811. let last = root;
  1812. for (let i = 0; i < 3; i++) {
  1813. if (i === 1) {
  1814. const autogroupBreakingSpan = new TraceTreeNode(
  1815. last,
  1816. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1817. {
  1818. project_slug: '',
  1819. event_id: '',
  1820. }
  1821. );
  1822. last.children.push(autogroupBreakingSpan);
  1823. last = autogroupBreakingSpan;
  1824. } else {
  1825. for (let j = 0; j < 3; j++) {
  1826. const node = new TraceTreeNode(
  1827. last,
  1828. makeSpan({span_id: `span${j}`, description: `span${j}`, op: 'http'}),
  1829. {
  1830. project_slug: '',
  1831. event_id: '',
  1832. }
  1833. );
  1834. last.children.push(node);
  1835. last = node;
  1836. }
  1837. }
  1838. }
  1839. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1840. assertAutogroupedNode(root.children[0]);
  1841. assertAutogroupedNode(root.children[0].children[0].children[0]);
  1842. });
  1843. it('sibling autogrouping', () => {
  1844. // db db
  1845. // http sibling autogrouped (5)
  1846. // http
  1847. // http ->
  1848. // http
  1849. // http
  1850. const root = new TraceTreeNode(
  1851. null,
  1852. makeTransaction({start_timestamp: 0, timestamp: 10}),
  1853. {
  1854. project_slug: '',
  1855. event_id: '',
  1856. }
  1857. );
  1858. for (let i = 0; i < 5; i++) {
  1859. root.children.push(
  1860. new TraceTreeNode(root, makeSpan({start_timestamp: i, timestamp: i + 1}), {
  1861. project_slug: '',
  1862. event_id: '',
  1863. })
  1864. );
  1865. }
  1866. TraceTree.AutogroupSiblingSpanNodes(root);
  1867. expect(root.children.length).toBe(1);
  1868. assertAutogroupedNode(root.children[0]);
  1869. });
  1870. it('multiple sibling autogrouping', () => {
  1871. // db db
  1872. // http sibling autogrouped (5)
  1873. // http db
  1874. // http -> sibling autogrouped (5)
  1875. // http
  1876. // http
  1877. // db
  1878. // http
  1879. // http
  1880. // http
  1881. // http
  1882. // http
  1883. const root = new TraceTreeNode(
  1884. null,
  1885. makeTransaction({start_timestamp: 0, timestamp: 10}),
  1886. {
  1887. project_slug: '',
  1888. event_id: '',
  1889. }
  1890. );
  1891. for (let i = 0; i < 10; i++) {
  1892. if (i === 5) {
  1893. root.children.push(
  1894. new TraceTreeNode(
  1895. root,
  1896. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'db'}),
  1897. {
  1898. project_slug: '',
  1899. event_id: '',
  1900. }
  1901. )
  1902. );
  1903. }
  1904. root.children.push(
  1905. new TraceTreeNode(
  1906. root,
  1907. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'http'}),
  1908. {
  1909. project_slug: '',
  1910. event_id: '',
  1911. }
  1912. )
  1913. );
  1914. }
  1915. TraceTree.AutogroupSiblingSpanNodes(root);
  1916. assertAutogroupedNode(root.children[0]);
  1917. expect(root.children).toHaveLength(3);
  1918. assertAutogroupedNode(root.children[2]);
  1919. });
  1920. it('renders children of autogrouped direct children nodes', async () => {
  1921. const tree = TraceTree.FromTrace(
  1922. makeTrace({
  1923. transactions: [
  1924. makeTransaction({
  1925. transaction: '/',
  1926. project_slug: 'project',
  1927. event_id: 'event_id',
  1928. }),
  1929. ],
  1930. })
  1931. );
  1932. MockApiClient.addMockResponse({
  1933. url: '/organizations/org-slug/events/project:event_id/',
  1934. method: 'GET',
  1935. body: makeEvent({}, [
  1936. makeSpan({description: 'parent span', op: 'http', span_id: '1'}),
  1937. makeSpan({description: 'span', op: 'db', span_id: '2', parent_span_id: '1'}),
  1938. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  1939. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  1940. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  1941. makeSpan({
  1942. description: 'span',
  1943. op: 'redis',
  1944. span_id: '6',
  1945. parent_span_id: '5',
  1946. }),
  1947. makeSpan({description: 'span', op: 'https', parent_span_id: '1'}),
  1948. ]),
  1949. });
  1950. expect(tree.list.length).toBe(2);
  1951. tree.zoomIn(tree.list[1], true, {
  1952. api: new MockApiClient(),
  1953. organization: OrganizationFixture(),
  1954. });
  1955. await waitFor(() => {
  1956. expect(tree.list.length).toBe(6);
  1957. });
  1958. const autogroupedNode = tree.list[tree.list.length - 3];
  1959. assertParentAutogroupedNode(autogroupedNode);
  1960. expect('autogrouped_by' in autogroupedNode?.value).toBeTruthy();
  1961. expect(autogroupedNode.groupCount).toBe(4);
  1962. expect(autogroupedNode.head.value.span_id).toBe('2');
  1963. expect(autogroupedNode.tail.value.span_id).toBe('5');
  1964. // Expand autogrouped node
  1965. expect(tree.expand(autogroupedNode, true)).toBe(true);
  1966. expect(tree.list.length).toBe(10);
  1967. // Collapse autogrouped node
  1968. expect(tree.expand(autogroupedNode, false)).toBe(true);
  1969. expect(tree.list.length).toBe(6);
  1970. expect(autogroupedNode.children[0].depth).toBe(4);
  1971. });
  1972. });
  1973. });