traceTree.spec.tsx 61 KB

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