traceTree.spec.tsx 67 KB

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