traceTree.spec.tsx 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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 {TraceType} from '../../traceDetails/newTraceDetailsContent';
  11. import {
  12. isAutogroupedNode,
  13. isMissingInstrumentationNode,
  14. isSpanNode,
  15. isTraceErrorNode,
  16. isTransactionNode,
  17. } from '../guards';
  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('inserts no data node when txn has no span children', async () => {
  534. const tree = TraceTree.FromTrace(
  535. makeTrace({
  536. transactions: [
  537. makeTransaction({
  538. transaction: '/',
  539. project_slug: 'project',
  540. event_id: 'event_id',
  541. }),
  542. ],
  543. })
  544. );
  545. MockApiClient.addMockResponse({
  546. url: EVENT_REQUEST_URL,
  547. method: 'GET',
  548. body: makeEvent({}, []),
  549. });
  550. await tree.zoomIn(tree.list[1], true, {
  551. api: new MockApiClient(),
  552. organization: OrganizationFixture(),
  553. });
  554. assertNoDataNode(tree.list[2]);
  555. expect(tree.list.length).toBe(3);
  556. });
  557. describe('autogrouped children', () => {
  558. const tree = TraceTree.FromTrace(
  559. makeTrace({
  560. transactions: [
  561. makeTransaction({
  562. transaction: '/',
  563. project_slug: 'project',
  564. event_id: 'event_id',
  565. }),
  566. ],
  567. })
  568. );
  569. MockApiClient.addMockResponse({
  570. url: EVENT_REQUEST_URL,
  571. method: 'GET',
  572. body: makeEvent({}, [
  573. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  574. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  575. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  576. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  577. ]),
  578. });
  579. tree.zoomIn(tree.list[1], true, {
  580. api: new MockApiClient(),
  581. organization: OrganizationFixture(),
  582. });
  583. it('autogrouped node', async () => {
  584. await waitFor(() => {
  585. expect(tree.list.length).toBe(3);
  586. });
  587. tree.expand(tree.list[2], true);
  588. assertAutogroupedNode(tree.list[2]);
  589. expect(tree.list[2].path).toEqual(['ag-2', 'txn-event_id']);
  590. });
  591. it('child is part of autogrouping', () => {
  592. expect(tree.list[tree.list.length - 1].path).toEqual([
  593. 'span-5',
  594. 'ag-2',
  595. 'txn-event_id',
  596. ]);
  597. });
  598. });
  599. describe('collapses some node by default', () => {
  600. it('android okhttp', async () => {
  601. const tree = TraceTree.FromTrace(
  602. makeTrace({
  603. transactions: [
  604. makeTransaction({
  605. transaction: '/',
  606. project_slug: 'project',
  607. event_id: 'event_id',
  608. }),
  609. ],
  610. })
  611. );
  612. MockApiClient.addMockResponse({
  613. url: EVENT_REQUEST_URL,
  614. method: 'GET',
  615. body: makeEvent({}, [
  616. makeSpan({
  617. description: 'span',
  618. span_id: '2',
  619. op: 'http.client',
  620. origin: 'auto.http.okhttp',
  621. }),
  622. makeSpan({
  623. description: 'span',
  624. op: 'http.client.tls',
  625. span_id: '3',
  626. parent_span_id: '2',
  627. }),
  628. ]),
  629. });
  630. tree.zoomIn(tree.list[1], true, {
  631. api: new MockApiClient(),
  632. organization: OrganizationFixture(),
  633. });
  634. await waitFor(() => {
  635. // trace
  636. // - transaction
  637. // - http.client
  638. // ^ child of http.client is not visible
  639. expect(tree.list.length).toBe(3);
  640. });
  641. });
  642. });
  643. describe('non expanded direct children autogrouped path', () => {
  644. const tree = TraceTree.FromTrace(
  645. makeTrace({
  646. transactions: [
  647. makeTransaction({
  648. transaction: '/',
  649. project_slug: 'project',
  650. event_id: 'event_id',
  651. }),
  652. ],
  653. })
  654. );
  655. MockApiClient.addMockResponse({
  656. url: EVENT_REQUEST_URL,
  657. method: 'GET',
  658. body: makeEvent({}, [
  659. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  660. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  661. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  662. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  663. makeSpan({description: 'span', op: '6', span_id: '6', parent_span_id: '5'}),
  664. ]),
  665. });
  666. tree.zoomIn(tree.list[1], true, {
  667. api: new MockApiClient(),
  668. organization: OrganizationFixture(),
  669. });
  670. it('autogrouped node', async () => {
  671. await waitFor(() => {
  672. expect(tree.list.length).toBe(4);
  673. });
  674. assertAutogroupedNode(tree.list[2]);
  675. expect(tree.list[2].path).toEqual(['ag-2', 'txn-event_id']);
  676. });
  677. it('span node skips autogrouped node because it is not expanded', async () => {
  678. await waitFor(() => {
  679. expect(tree.list.length).toBe(4);
  680. });
  681. expect(tree.list[tree.list.length - 1].path).toEqual(['span-6', 'txn-event_id']);
  682. });
  683. });
  684. });
  685. });
  686. describe('TraceTree', () => {
  687. beforeEach(() => {
  688. MockApiClient.clearMockResponses();
  689. });
  690. it('builds from transactions', () => {
  691. const tree = TraceTree.FromTrace(
  692. makeTrace({
  693. transactions: [
  694. makeTransaction({
  695. children: [],
  696. }),
  697. makeTransaction({
  698. children: [],
  699. }),
  700. ],
  701. })
  702. );
  703. expect(tree.list).toHaveLength(3);
  704. });
  705. it('builds orphan errors', () => {
  706. const tree = TraceTree.FromTrace(
  707. makeTrace({
  708. transactions: [
  709. makeTransaction({
  710. children: [],
  711. }),
  712. makeTransaction({
  713. children: [],
  714. }),
  715. ],
  716. orphan_errors: [makeTraceError()],
  717. })
  718. );
  719. expect(tree.list).toHaveLength(4);
  720. });
  721. it('processes orphan errors without timestamps', () => {
  722. const tree = TraceTree.FromTrace(
  723. makeTrace({
  724. transactions: [
  725. makeTransaction({
  726. children: [],
  727. }),
  728. ],
  729. orphan_errors: [
  730. {
  731. level: 'error',
  732. title: 'Error',
  733. event_type: 'error',
  734. } as TraceTree.TraceError,
  735. ],
  736. })
  737. );
  738. expect(tree.list).toHaveLength(3);
  739. });
  740. it('sorts orphan errors', () => {
  741. const tree = TraceTree.FromTrace(
  742. makeTrace({
  743. transactions: [
  744. makeTransaction({
  745. start_timestamp: 0,
  746. timestamp: 1,
  747. }),
  748. makeTransaction({
  749. start_timestamp: 2,
  750. timestamp: 3,
  751. }),
  752. ],
  753. orphan_errors: [makeTraceError({timestamp: 1, level: 'error'})],
  754. })
  755. );
  756. expect(tree.list).toHaveLength(4);
  757. assertTraceErrorNode(tree.list[2]);
  758. });
  759. it('adjusts trace bounds by orphan error timestamp as well', () => {
  760. const tree = TraceTree.FromTrace(
  761. makeTrace({
  762. transactions: [
  763. makeTransaction({
  764. start_timestamp: 0.1,
  765. timestamp: 0.15,
  766. children: [],
  767. }),
  768. makeTransaction({
  769. start_timestamp: 0.2,
  770. timestamp: 0.25,
  771. children: [],
  772. }),
  773. ],
  774. orphan_errors: [
  775. makeTraceError({timestamp: 0.05}),
  776. makeTraceError({timestamp: 0.3}),
  777. ],
  778. })
  779. );
  780. expect(tree.list).toHaveLength(5);
  781. expect(tree.root.space).toStrictEqual([
  782. 0.05 * tree.root.multiplier,
  783. (0.3 - 0.05) * tree.root.multiplier,
  784. ]);
  785. });
  786. it('calculates correct trace type', () => {
  787. let tree = TraceTree.FromTrace(
  788. makeTrace({
  789. transactions: [],
  790. orphan_errors: [],
  791. })
  792. );
  793. expect(tree.shape).toBe(TraceType.EMPTY_TRACE);
  794. tree = TraceTree.FromTrace(
  795. makeTrace({
  796. transactions: [
  797. makeTransaction({
  798. children: [],
  799. }),
  800. makeTransaction({
  801. children: [],
  802. }),
  803. ],
  804. orphan_errors: [],
  805. })
  806. );
  807. expect(tree.shape).toBe(TraceType.NO_ROOT);
  808. tree = TraceTree.FromTrace(
  809. makeTrace({
  810. transactions: [
  811. makeTransaction({
  812. parent_span_id: null,
  813. children: [],
  814. }),
  815. ],
  816. orphan_errors: [],
  817. })
  818. );
  819. expect(tree.shape).toBe(TraceType.ONE_ROOT);
  820. tree = TraceTree.FromTrace(
  821. makeTrace({
  822. transactions: [
  823. makeTransaction({
  824. parent_span_id: null,
  825. children: [],
  826. }),
  827. makeTransaction({
  828. children: [],
  829. }),
  830. ],
  831. orphan_errors: [],
  832. })
  833. );
  834. expect(tree.shape).toBe(TraceType.BROKEN_SUBTRACES);
  835. tree = TraceTree.FromTrace(
  836. makeTrace({
  837. transactions: [
  838. makeTransaction({
  839. parent_span_id: null,
  840. children: [],
  841. }),
  842. makeTransaction({
  843. parent_span_id: null,
  844. children: [],
  845. }),
  846. ],
  847. orphan_errors: [],
  848. })
  849. );
  850. expect(tree.shape).toBe(TraceType.MULTIPLE_ROOTS);
  851. tree = TraceTree.FromTrace(
  852. makeTrace({
  853. transactions: [],
  854. orphan_errors: [makeTraceError()],
  855. })
  856. );
  857. expect(tree.shape).toBe(TraceType.ONLY_ERRORS);
  858. });
  859. it('builds from spans when root is a transaction node', () => {
  860. const root = new TraceTreeNode(
  861. null,
  862. makeTransaction({
  863. children: [],
  864. }),
  865. {project_slug: '', event_id: ''}
  866. );
  867. const node = TraceTree.FromSpans(
  868. root,
  869. makeEvent(),
  870. [
  871. makeSpan({start_timestamp: 0, op: '1', span_id: '1'}),
  872. makeSpan({start_timestamp: 1, op: '2', span_id: '2', parent_span_id: '1'}),
  873. makeSpan({start_timestamp: 2, op: '3', span_id: '3', parent_span_id: '2'}),
  874. makeSpan({start_timestamp: 3, op: '4', span_id: '4', parent_span_id: '1'}),
  875. ],
  876. {sdk: undefined}
  877. );
  878. if (!isSpanNode(node.children[0])) {
  879. throw new Error('Child needs to be a span');
  880. }
  881. expect(node.children[0].value.span_id).toBe('1');
  882. expect(node.children[0].value.start_timestamp).toBe(0);
  883. expect(node.children.length).toBe(1);
  884. assertSpanNode(node.children[0].children[0]);
  885. assertSpanNode(node.children[0].children[0].children[0]);
  886. assertSpanNode(node.children[0].children[1]);
  887. expect(node.children[0].children[0].value.start_timestamp).toBe(1);
  888. expect(node.children[0].children[0].children[0].value.start_timestamp).toBe(2);
  889. expect(node.children[0].children[1].value.start_timestamp).toBe(3);
  890. });
  891. it('builds from spans and copies txn nodes', () => {
  892. // transaction transaction
  893. // - child transaction -> - span
  894. // - span
  895. // - child-transaction
  896. // - span
  897. const root = new TraceTreeNode(
  898. null,
  899. makeTransaction({
  900. children: [],
  901. }),
  902. {project_slug: '', event_id: ''}
  903. );
  904. root.children.push(
  905. new TraceTreeNode(
  906. root,
  907. makeTransaction({
  908. parent_span_id: 'child-transaction',
  909. }),
  910. {project_slug: '', event_id: ''}
  911. )
  912. );
  913. const node = TraceTree.FromSpans(
  914. root,
  915. makeEvent(),
  916. [
  917. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  918. makeSpan({
  919. start_timestamp: 0.1,
  920. timestamp: 0.2,
  921. op: 'child-transaction',
  922. span_id: 'child-transaction',
  923. }),
  924. makeSpan({start_timestamp: 0.2, timestamp: 0.25, op: 'span', span_id: 'none'}),
  925. ],
  926. {sdk: undefined}
  927. );
  928. assertSpanNode(node.children[1]);
  929. assertTransactionNode(node.children[1].children[0]);
  930. });
  931. it('builds from spans and copies txn nodes to nested children', () => {
  932. // parent transaction parent transaction
  933. // - child transaction -> - span
  934. // - grandchild transaction -> - child-transaction
  935. // - grandchild-transaction
  936. //
  937. const root = new TraceTreeNode(
  938. null,
  939. makeTransaction({
  940. span_id: 'parent-transaction',
  941. children: [],
  942. }),
  943. {project_slug: '', event_id: ''}
  944. );
  945. let start: TraceTreeNode<TraceTree.NodeValue> = root;
  946. for (let i = 0; i < 2; i++) {
  947. const node = new TraceTreeNode(
  948. start,
  949. makeTransaction({
  950. transaction: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  951. parent_span_id: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  952. }),
  953. {project_slug: '', event_id: ''}
  954. );
  955. start.children.push(node);
  956. start = node;
  957. }
  958. const node = TraceTree.FromSpans(
  959. root,
  960. makeEvent(),
  961. [
  962. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  963. makeSpan({
  964. start_timestamp: 0.1,
  965. timestamp: 0.2,
  966. op: 'child-transaction',
  967. span_id: 'child-transaction',
  968. }),
  969. ],
  970. {sdk: undefined}
  971. );
  972. assertSpanNode(node.children[1]);
  973. assertTransactionNode(node.children[1].children[0]);
  974. assertTransactionNode(node.children[1].children[0].children[0]);
  975. });
  976. it('injects missing spans', () => {
  977. const root = new TraceTreeNode(
  978. null,
  979. makeTransaction({
  980. children: [],
  981. }),
  982. {project_slug: '', event_id: ''}
  983. );
  984. const date = new Date().getTime();
  985. const node = TraceTree.FromSpans(
  986. root,
  987. makeEvent(),
  988. [
  989. makeSpan({
  990. start_timestamp: date,
  991. timestamp: date + 1,
  992. span_id: '1',
  993. op: 'span 1',
  994. }),
  995. makeSpan({
  996. start_timestamp: date + 2,
  997. timestamp: date + 4,
  998. op: 'span 2',
  999. span_id: '2',
  1000. }),
  1001. ],
  1002. {sdk: undefined}
  1003. );
  1004. assertSpanNode(node.children[0]);
  1005. assertMissingInstrumentationNode(node.children[1]);
  1006. assertSpanNode(node.children[2]);
  1007. expect(node.children.length).toBe(3);
  1008. expect(node.children[0].value.op).toBe('span 1');
  1009. expect(node.children[1].value.type).toBe('missing_instrumentation');
  1010. expect(node.children[2].value.op).toBe('span 2');
  1011. });
  1012. it('does not inject missing spans for javascript platform', () => {
  1013. const root = new TraceTreeNode(
  1014. null,
  1015. makeTransaction({
  1016. children: [],
  1017. }),
  1018. {project_slug: '', event_id: ''}
  1019. );
  1020. const date = new Date().getTime();
  1021. const node = TraceTree.FromSpans(
  1022. root,
  1023. makeEvent(),
  1024. [
  1025. makeSpan({
  1026. start_timestamp: date,
  1027. timestamp: date + 1,
  1028. span_id: '1',
  1029. op: 'span 1',
  1030. }),
  1031. makeSpan({
  1032. start_timestamp: date + 2,
  1033. timestamp: date + 4,
  1034. op: 'span 2',
  1035. span_id: '2',
  1036. }),
  1037. ],
  1038. {sdk: 'sentry.javascript.browser'}
  1039. );
  1040. assertSpanNode(node.children[0]);
  1041. assertSpanNode(node.children[1]);
  1042. expect(node.children.length).toBe(2);
  1043. expect(node.children[0].value.op).toBe('span 1');
  1044. expect(node.children[1].value.op).toBe('span 2');
  1045. });
  1046. it('builds and preserves list order', async () => {
  1047. const organization = OrganizationFixture();
  1048. const api = new MockApiClient();
  1049. const tree = TraceTree.FromTrace(
  1050. makeTrace({
  1051. transactions: [
  1052. makeTransaction({
  1053. transaction: 'txn 1',
  1054. start_timestamp: 0,
  1055. children: [makeTransaction({start_timestamp: 1, transaction: 'txn 2'})],
  1056. }),
  1057. ],
  1058. })
  1059. );
  1060. tree.expand(tree.list[0], true);
  1061. const node = tree.list[1];
  1062. const request = MockApiClient.addMockResponse({
  1063. url: '/organizations/org-slug/events/undefined:undefined/?averageColumn=span.self_time&averageColumn=span.duration',
  1064. method: 'GET',
  1065. body: makeEvent({startTimestamp: 0}, [
  1066. makeSpan({start_timestamp: 1, op: 'span 1', span_id: '1'}),
  1067. makeSpan({
  1068. start_timestamp: 2,
  1069. op: 'span 2',
  1070. span_id: '2',
  1071. parent_span_id: '1',
  1072. }),
  1073. makeSpan({start_timestamp: 3, op: 'span 3', parent_span_id: '2'}),
  1074. makeSpan({start_timestamp: 4, op: 'span 4', parent_span_id: '1'}),
  1075. ]),
  1076. });
  1077. // 0
  1078. // 1
  1079. // 2
  1080. // 3
  1081. // 4
  1082. tree.zoomIn(node, true, {api, organization});
  1083. await waitFor(() => {
  1084. expect(node.zoomedIn).toBe(true);
  1085. });
  1086. expect(request).toHaveBeenCalled();
  1087. expect(tree.list.length).toBe(7);
  1088. assertTransactionNode(tree.list[1]);
  1089. assertSpanNode(tree.list[2]);
  1090. assertSpanNode(tree.list[3]);
  1091. expect(tree.list[1].value.start_timestamp).toBe(0);
  1092. expect(tree.list[2].value.start_timestamp).toBe(1);
  1093. expect(tree.list[3].value.start_timestamp).toBe(2);
  1094. });
  1095. it('preserves input order', () => {
  1096. const firstChild = makeTransaction({
  1097. start_timestamp: 0,
  1098. timestamp: 1,
  1099. children: [],
  1100. });
  1101. const secondChild = makeTransaction({
  1102. start_timestamp: 1,
  1103. timestamp: 2,
  1104. children: [],
  1105. });
  1106. const tree = TraceTree.FromTrace(
  1107. makeTrace({
  1108. transactions: [
  1109. makeTransaction({
  1110. start_timestamp: 0,
  1111. timestamp: 2,
  1112. children: [firstChild, secondChild],
  1113. }),
  1114. makeTransaction({
  1115. start_timestamp: 2,
  1116. timestamp: 4,
  1117. }),
  1118. ],
  1119. })
  1120. );
  1121. expect(tree.list).toHaveLength(5);
  1122. expect(tree.expand(tree.list[1], false)).toBe(true);
  1123. expect(tree.list).toHaveLength(3);
  1124. expect(tree.expand(tree.list[1], true)).toBe(true);
  1125. expect(tree.list).toHaveLength(5);
  1126. expect(tree.list[2].value).toBe(firstChild);
  1127. expect(tree.list[3].value).toBe(secondChild);
  1128. });
  1129. it('creates children -> parent references', () => {
  1130. const tree = TraceTree.FromTrace(
  1131. makeTrace({
  1132. transactions: [
  1133. makeTransaction({
  1134. start_timestamp: 0,
  1135. timestamp: 2,
  1136. children: [makeTransaction({start_timestamp: 1, timestamp: 2})],
  1137. }),
  1138. makeTransaction({
  1139. start_timestamp: 2,
  1140. timestamp: 4,
  1141. }),
  1142. ],
  1143. })
  1144. );
  1145. expect(tree.list).toHaveLength(4);
  1146. expect(tree.list[2].parent?.value).toBe(tree.list[1].value);
  1147. });
  1148. it('establishes parent-child relationships', () => {
  1149. const tree = TraceTree.FromTrace(
  1150. makeTrace({
  1151. transactions: [
  1152. makeTransaction({
  1153. children: [makeTransaction()],
  1154. }),
  1155. ],
  1156. })
  1157. );
  1158. expect(tree.root.children).toHaveLength(1);
  1159. expect(tree.root.children[0].children).toHaveLength(1);
  1160. });
  1161. it('isLastChild', () => {
  1162. const tree = TraceTree.FromTrace(
  1163. makeTrace({
  1164. transactions: [
  1165. makeTransaction({
  1166. children: [makeTransaction(), makeTransaction()],
  1167. }),
  1168. makeTransaction(),
  1169. ],
  1170. orphan_errors: [],
  1171. })
  1172. );
  1173. tree.expand(tree.list[1], true);
  1174. expect(tree.list[0].isLastChild).toBe(true);
  1175. expect(tree.list[1].isLastChild).toBe(false);
  1176. expect(tree.list[2].isLastChild).toBe(false);
  1177. expect(tree.list[3].isLastChild).toBe(true);
  1178. expect(tree.list[4].isLastChild).toBe(true);
  1179. });
  1180. describe('connectors', () => {
  1181. it('computes transaction connectors', () => {
  1182. const tree = TraceTree.FromTrace(
  1183. makeTrace({
  1184. transactions: [
  1185. makeTransaction({
  1186. transaction: 'sibling',
  1187. children: [
  1188. makeTransaction({transaction: 'child'}),
  1189. makeTransaction({transaction: 'child'}),
  1190. ],
  1191. }),
  1192. makeTransaction({transaction: 'sibling'}),
  1193. ],
  1194. })
  1195. );
  1196. // -1 root
  1197. // ------ list begins here
  1198. // 0 transaction
  1199. // 0 |- sibling
  1200. // -1, 2| | - child
  1201. // -1| | - child
  1202. // 0 |- sibling
  1203. tree.expand(tree.list[1], true);
  1204. expect(tree.list.length).toBe(5);
  1205. expect(tree.list[0].connectors.length).toBe(0);
  1206. expect(tree.list[1].connectors.length).toBe(1);
  1207. expect(tree.list[1].connectors[0]).toBe(-1);
  1208. expect(tree.list[2].connectors[0]).toBe(-1);
  1209. expect(tree.list[2].connectors[1]).toBe(2);
  1210. expect(tree.list[2].connectors.length).toBe(2);
  1211. expect(tree.list[3].connectors[0]).toBe(-1);
  1212. expect(tree.list[3].connectors.length).toBe(1);
  1213. expect(tree.list[4].connectors.length).toBe(0);
  1214. });
  1215. it('computes span connectors', async () => {
  1216. const tree = TraceTree.FromTrace(
  1217. makeTrace({
  1218. transactions: [
  1219. makeTransaction({
  1220. project_slug: 'project',
  1221. event_id: 'event_id',
  1222. transaction: 'transaction',
  1223. children: [],
  1224. }),
  1225. ],
  1226. })
  1227. );
  1228. // root
  1229. // |- node1 []
  1230. // |- node2 []
  1231. MockApiClient.addMockResponse({
  1232. url: EVENT_REQUEST_URL,
  1233. method: 'GET',
  1234. body: makeEvent({}, [makeSpan({start_timestamp: 0, op: 'span', span_id: '1'})]),
  1235. });
  1236. expect(tree.list.length).toBe(2);
  1237. tree.zoomIn(tree.list[1], true, {
  1238. api: new MockApiClient(),
  1239. organization: OrganizationFixture(),
  1240. });
  1241. await waitFor(() => {
  1242. expect(tree.list.length).toBe(3);
  1243. });
  1244. // root
  1245. // |- node1 []
  1246. // |- node2 []
  1247. // |- span1 []
  1248. const span = tree.list[tree.list.length - 1];
  1249. expect(span.connectors.length).toBe(0);
  1250. });
  1251. });
  1252. describe('expanding', () => {
  1253. it('expands a node and updates the list', () => {
  1254. const tree = TraceTree.FromTrace(
  1255. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1256. );
  1257. const node = tree.list[1];
  1258. expect(tree.expand(node, false)).toBe(true);
  1259. expect(tree.list.length).toBe(2);
  1260. expect(node.expanded).toBe(false);
  1261. expect(tree.expand(node, true)).toBe(true);
  1262. expect(node.expanded).toBe(true);
  1263. // Assert that the list has been updated
  1264. expect(tree.list).toHaveLength(3);
  1265. expect(tree.list[2]).toBe(node.children[0]);
  1266. });
  1267. it('collapses a node and updates the list', () => {
  1268. const tree = TraceTree.FromTrace(
  1269. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1270. );
  1271. const node = tree.list[1];
  1272. tree.expand(node, true);
  1273. expect(tree.list.length).toBe(3);
  1274. expect(tree.expand(node, false)).toBe(true);
  1275. expect(node.expanded).toBe(false);
  1276. // Assert that the list has been updated
  1277. expect(tree.list).toHaveLength(2);
  1278. expect(tree.list[1]).toBe(node);
  1279. });
  1280. it('preserves children expanded state', () => {
  1281. const tree = TraceTree.FromTrace(
  1282. makeTrace({
  1283. transactions: [
  1284. makeTransaction({
  1285. children: [
  1286. makeTransaction({children: [makeTransaction({start_timestamp: 1000})]}),
  1287. makeTransaction({start_timestamp: 5}),
  1288. ],
  1289. }),
  1290. ],
  1291. })
  1292. );
  1293. expect(tree.expand(tree.list[2], false)).toBe(true);
  1294. // Assert that the list has been updated
  1295. expect(tree.list).toHaveLength(4);
  1296. expect(tree.expand(tree.list[2], true)).toBe(true);
  1297. expect(tree.list.length).toBe(5);
  1298. expect(tree.list[tree.list.length - 1].value).toEqual(
  1299. makeTransaction({start_timestamp: 5})
  1300. );
  1301. });
  1302. it('expanding or collapsing a zoomed in node doesnt do anything', async () => {
  1303. const organization = OrganizationFixture();
  1304. const api = new MockApiClient();
  1305. const tree = TraceTree.FromTrace(
  1306. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1307. );
  1308. const node = tree.list[0];
  1309. const request = MockApiClient.addMockResponse({
  1310. url: '/organizations/org-slug/events/undefined:undefined/?averageColumn=span.self_time&averageColumn=span.duration',
  1311. method: 'GET',
  1312. body: makeEvent(),
  1313. });
  1314. tree.zoomIn(node, true, {api, organization});
  1315. await waitFor(() => {
  1316. expect(node.zoomedIn).toBe(true);
  1317. });
  1318. expect(request).toHaveBeenCalled();
  1319. expect(tree.expand(node, true)).toBe(false);
  1320. });
  1321. it('expanding', async () => {
  1322. const organization = OrganizationFixture();
  1323. const api = new MockApiClient();
  1324. const tree = TraceTree.FromTrace(
  1325. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1326. );
  1327. const node = tree.list[0];
  1328. const request = MockApiClient.addMockResponse({
  1329. url: '/organizations/org-slug/events/undefined:undefined/?averageColumn=span.self_time&averageColumn=span.duration',
  1330. method: 'GET',
  1331. body: makeEvent(),
  1332. });
  1333. tree.zoomIn(node, true, {api, organization});
  1334. await waitFor(() => {
  1335. expect(node.zoomedIn).toBe(true);
  1336. });
  1337. expect(request).toHaveBeenCalled();
  1338. expect(tree.expand(node, true)).toBe(false);
  1339. });
  1340. it('accounts for intermediary expanded or collapsed nodes in autogrouped chain', async () => {
  1341. const organization = OrganizationFixture();
  1342. const api = new MockApiClient();
  1343. const tree = TraceTree.FromTrace(
  1344. makeTrace({
  1345. transactions: [
  1346. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1347. ],
  1348. })
  1349. );
  1350. MockApiClient.addMockResponse({
  1351. url: EVENT_REQUEST_URL,
  1352. method: 'GET',
  1353. body: makeEvent({}, [
  1354. makeSpan({start_timestamp: 0, op: 'span', span_id: 'root'}),
  1355. makeSpan({
  1356. start_timestamp: 10,
  1357. op: 'last',
  1358. span_id: 'last',
  1359. parent_span_id: 'root',
  1360. }),
  1361. makeSpan({
  1362. start_timestamp: 0,
  1363. op: 'db',
  1364. parent_span_id: 'root',
  1365. span_id: 'first-db',
  1366. }),
  1367. makeSpan({
  1368. start_timestamp: 0,
  1369. op: 'db',
  1370. parent_span_id: 'first-db',
  1371. span_id: 'second-db',
  1372. }),
  1373. makeSpan({
  1374. start_timestamp: 0,
  1375. op: 'other',
  1376. parent_span_id: 'second-db',
  1377. span_id: 'other',
  1378. }),
  1379. makeSpan({
  1380. start_timestamp: 0,
  1381. op: 'another',
  1382. parent_span_id: 'second-db',
  1383. span_id: 'other',
  1384. }),
  1385. ]),
  1386. });
  1387. tree.zoomIn(tree.list[1], true, {api, organization});
  1388. await waitFor(() => {
  1389. expect(tree.list[1].zoomedIn).toBe(true);
  1390. });
  1391. tree.print();
  1392. // expand autogroup
  1393. tree.expand(tree.list[3], true);
  1394. const last = tree.list[tree.list.length - 1];
  1395. // root
  1396. // transaction
  1397. // span
  1398. // parent autogroup (2) <-- expand the autogroup and collapse nodes between head/tail
  1399. // db <--- collapse
  1400. // db <--- collapse
  1401. // other
  1402. // another
  1403. // last
  1404. // collapse innermost two children
  1405. tree.expand(tree.list[5], false);
  1406. tree.expand(tree.list[4], false);
  1407. // collapse autogroup
  1408. tree.expand(tree.list[3], false);
  1409. tree.expand(tree.list[3], true);
  1410. expect(tree.list[tree.list.length - 1]).toBe(last);
  1411. });
  1412. });
  1413. describe('zooming', () => {
  1414. it('marks node as zoomed in', async () => {
  1415. const organization = OrganizationFixture();
  1416. const api = new MockApiClient();
  1417. const tree = TraceTree.FromTrace(
  1418. makeTrace({
  1419. transactions: [
  1420. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1421. ],
  1422. })
  1423. );
  1424. const request = MockApiClient.addMockResponse({
  1425. url: EVENT_REQUEST_URL,
  1426. method: 'GET',
  1427. body: makeEvent(),
  1428. });
  1429. const node = tree.list[1];
  1430. expect(node.zoomedIn).toBe(false);
  1431. tree.zoomIn(node, true, {api, organization});
  1432. await waitFor(() => {
  1433. expect(node.zoomedIn).toBe(true);
  1434. });
  1435. expect(request).toHaveBeenCalled();
  1436. });
  1437. it('fetches spans for node when zooming in', async () => {
  1438. const tree = TraceTree.FromTrace(
  1439. makeTrace({
  1440. transactions: [
  1441. makeTransaction({
  1442. transaction: 'txn',
  1443. project_slug: 'project',
  1444. event_id: 'event_id',
  1445. }),
  1446. ],
  1447. })
  1448. );
  1449. const request = MockApiClient.addMockResponse({
  1450. url: EVENT_REQUEST_URL,
  1451. method: 'GET',
  1452. body: makeEvent({}, [makeSpan()]),
  1453. });
  1454. const node = tree.list[1];
  1455. expect(node.children).toHaveLength(0);
  1456. tree.zoomIn(node, true, {
  1457. api: new MockApiClient(),
  1458. organization: OrganizationFixture(),
  1459. });
  1460. expect(request).toHaveBeenCalled();
  1461. await waitFor(() => {
  1462. expect(node.children).toHaveLength(1);
  1463. });
  1464. // Assert that the children have been updated
  1465. assertTransactionNode(node.children[0].parent);
  1466. expect(node.children[0].parent.value.transaction).toBe('txn');
  1467. expect(node.children[0].depth).toBe(node.depth + 1);
  1468. });
  1469. it('handles orphaned transaction nodes', async () => {
  1470. // Data quality issue where children transactions cannot be
  1471. // reparented under the fetched spans
  1472. //
  1473. // transaction <-- zooming transaction
  1474. // - child transaction - span
  1475. // - another child transaction - child transaction
  1476. // - another child transaction (orphaned)
  1477. const tree = TraceTree.FromTrace(
  1478. makeTrace({
  1479. transactions: [
  1480. makeTransaction({
  1481. transaction: 'root',
  1482. project_slug: 'project',
  1483. event_id: 'event_id',
  1484. children: [
  1485. makeTransaction({transaction: 'child', parent_span_id: 'span'}),
  1486. makeTransaction({transaction: 'another child'}),
  1487. ],
  1488. }),
  1489. ],
  1490. })
  1491. );
  1492. MockApiClient.addMockResponse({
  1493. url: EVENT_REQUEST_URL,
  1494. method: 'GET',
  1495. body: makeEvent({}, [makeSpan({span_id: 'span'})]),
  1496. });
  1497. assertTransactionNode(tree.list[tree.list.length - 1]);
  1498. tree.zoomIn(tree.list[1], true, {
  1499. api: new MockApiClient(),
  1500. organization: OrganizationFixture(),
  1501. });
  1502. await waitFor(() => {
  1503. expect(tree.list[1].zoomedIn).toBe(true);
  1504. });
  1505. const last = tree.list[tree.list.length - 1];
  1506. assertTransactionNode(last);
  1507. expect(last.value.transaction).toBe('another child');
  1508. });
  1509. it('handles bottom up zooming', async () => {
  1510. const tree = TraceTree.FromTrace(
  1511. makeTrace({
  1512. transactions: [
  1513. makeTransaction({
  1514. transaction: 'transaction',
  1515. project_slug: 'project',
  1516. event_id: 'event_id',
  1517. children: [
  1518. makeTransaction({
  1519. parent_span_id: 'span',
  1520. transaction: 'child transaction',
  1521. project_slug: 'child_project',
  1522. event_id: 'child_event_id',
  1523. }),
  1524. ],
  1525. }),
  1526. ],
  1527. })
  1528. );
  1529. const first_request = MockApiClient.addMockResponse({
  1530. url: EVENT_REQUEST_URL,
  1531. method: 'GET',
  1532. body: makeEvent({}, [makeSpan({op: 'db', span_id: 'span'})]),
  1533. });
  1534. const second_request = MockApiClient.addMockResponse({
  1535. url: '/organizations/org-slug/events/child_project:child_event_id/?averageColumn=span.self_time&averageColumn=span.duration',
  1536. method: 'GET',
  1537. body: makeEvent({}, [
  1538. makeSpan({op: 'db', span_id: 'span'}),
  1539. makeSpan({op: 'db', span_id: 'span 1', parent_span_id: 'span'}),
  1540. makeSpan({op: 'db', span_id: 'span 2', parent_span_id: 'span 1'}),
  1541. makeSpan({op: 'db', span_id: 'span 3', parent_span_id: 'span 2'}),
  1542. makeSpan({op: 'db', span_id: 'span 4', parent_span_id: 'span 3'}),
  1543. makeSpan({op: 'db', span_id: 'span 5', parent_span_id: 'span 4'}),
  1544. ]),
  1545. });
  1546. tree.zoomIn(tree.list[2], true, {
  1547. api: new MockApiClient(),
  1548. organization: OrganizationFixture(),
  1549. });
  1550. await waitFor(() => {
  1551. expect(second_request).toHaveBeenCalled();
  1552. });
  1553. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1554. tree.zoomIn(tree.list[1], true, {
  1555. api: new MockApiClient(),
  1556. organization: OrganizationFixture(),
  1557. });
  1558. await waitFor(() => {
  1559. expect(first_request).toHaveBeenCalled();
  1560. });
  1561. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1562. });
  1563. it('zooms out', async () => {
  1564. const tree = TraceTree.FromTrace(
  1565. makeTrace({
  1566. transactions: [
  1567. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1568. ],
  1569. })
  1570. );
  1571. MockApiClient.addMockResponse({
  1572. url: EVENT_REQUEST_URL,
  1573. method: 'GET',
  1574. body: makeEvent({}, [makeSpan({span_id: 'span1', description: 'span1'})]),
  1575. });
  1576. tree.zoomIn(tree.list[1], true, {
  1577. api: new MockApiClient(),
  1578. organization: OrganizationFixture(),
  1579. });
  1580. await waitFor(() => {
  1581. assertSpanNode(tree.list[1].children[0]);
  1582. expect(tree.list[1].children[0].value.description).toBe('span1');
  1583. });
  1584. tree.zoomIn(tree.list[1], false, {
  1585. api: new MockApiClient(),
  1586. organization: OrganizationFixture(),
  1587. });
  1588. await waitFor(() => {
  1589. // Assert child no longer points to children
  1590. expect(tree.list[1].zoomedIn).toBe(false);
  1591. expect(tree.list[1].children[0]).toBe(undefined);
  1592. expect(tree.list[2]).toBe(undefined);
  1593. });
  1594. });
  1595. it('zooms in and out', async () => {
  1596. const tree = TraceTree.FromTrace(
  1597. makeTrace({
  1598. transactions: [
  1599. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1600. ],
  1601. })
  1602. );
  1603. MockApiClient.addMockResponse({
  1604. url: EVENT_REQUEST_URL,
  1605. method: 'GET',
  1606. body: makeEvent({}, [makeSpan({span_id: 'span 1', description: 'span1'})]),
  1607. });
  1608. // Zoom in
  1609. tree.zoomIn(tree.list[1], true, {
  1610. api: new MockApiClient(),
  1611. organization: OrganizationFixture(),
  1612. });
  1613. await waitFor(() => {
  1614. expect(tree.list[1].zoomedIn).toBe(true);
  1615. assertSpanNode(tree.list[1].children[0]);
  1616. expect(tree.list[1].children[0].value.description).toBe('span1');
  1617. });
  1618. // Zoom out
  1619. tree.zoomIn(tree.list[1], false, {
  1620. api: new MockApiClient(),
  1621. organization: OrganizationFixture(),
  1622. });
  1623. await waitFor(() => {
  1624. expect(tree.list[2]).toBe(undefined);
  1625. });
  1626. // Zoom in
  1627. tree.zoomIn(tree.list[1], true, {
  1628. api: new MockApiClient(),
  1629. organization: OrganizationFixture(),
  1630. });
  1631. await waitFor(() => {
  1632. assertSpanNode(tree.list[1].children[0]);
  1633. expect(tree.list[1].children[0].value?.description).toBe('span1');
  1634. });
  1635. });
  1636. it('zooms in and out preserving siblings', async () => {
  1637. const tree = TraceTree.FromTrace(
  1638. makeTrace({
  1639. transactions: [
  1640. makeTransaction({
  1641. project_slug: 'project',
  1642. event_id: 'event_id',
  1643. start_timestamp: 0,
  1644. children: [
  1645. makeTransaction({
  1646. start_timestamp: 1,
  1647. timestamp: 2,
  1648. project_slug: 'other_project',
  1649. event_id: 'event_id',
  1650. }),
  1651. makeTransaction({start_timestamp: 2, timestamp: 3}),
  1652. ],
  1653. }),
  1654. ],
  1655. })
  1656. );
  1657. const request = MockApiClient.addMockResponse({
  1658. url: '/organizations/org-slug/events/other_project:event_id/?averageColumn=span.self_time&averageColumn=span.duration',
  1659. method: 'GET',
  1660. body: makeEvent({}, [makeSpan({description: 'span1'})]),
  1661. });
  1662. tree.expand(tree.list[1], true);
  1663. tree.zoomIn(tree.list[2], true, {
  1664. api: new MockApiClient(),
  1665. organization: OrganizationFixture(),
  1666. });
  1667. expect(request).toHaveBeenCalled();
  1668. // Zoom in
  1669. await waitFor(() => {
  1670. expect(tree.list.length).toBe(5);
  1671. });
  1672. // Zoom out
  1673. tree.zoomIn(tree.list[2], false, {
  1674. api: new MockApiClient(),
  1675. organization: OrganizationFixture(),
  1676. });
  1677. await waitFor(() => {
  1678. expect(tree.list.length).toBe(4);
  1679. });
  1680. });
  1681. it('preserves expanded state when zooming in and out', async () => {
  1682. const tree = TraceTree.FromTrace(
  1683. makeTrace({
  1684. transactions: [
  1685. makeTransaction({
  1686. project_slug: 'project',
  1687. event_id: 'event_id',
  1688. transaction: 'root',
  1689. children: [
  1690. makeTransaction({
  1691. project_slug: 'other_project',
  1692. event_id: 'event_id',
  1693. transaction: 'child',
  1694. }),
  1695. ],
  1696. }),
  1697. ],
  1698. })
  1699. );
  1700. MockApiClient.addMockResponse({
  1701. url: EVENT_REQUEST_URL,
  1702. method: 'GET',
  1703. body: makeEvent({}, [
  1704. makeSpan({description: 'span1'}),
  1705. makeSpan({description: 'span2'}),
  1706. ]),
  1707. });
  1708. tree.expand(tree.list[1], true);
  1709. expect(tree.list.length).toBe(3);
  1710. tree.zoomIn(tree.list[1], true, {
  1711. api: new MockApiClient(),
  1712. organization: OrganizationFixture(),
  1713. });
  1714. await waitFor(() => {
  1715. expect(tree.list.length).toBe(5);
  1716. });
  1717. tree.zoomIn(tree.list[1], false, {
  1718. api: new MockApiClient(),
  1719. organization: OrganizationFixture(),
  1720. });
  1721. await waitFor(() => {
  1722. expect(tree.list.length).toBe(3);
  1723. });
  1724. expect(tree.list[1].expanded).toBe(true);
  1725. });
  1726. });
  1727. describe('autogrouping', () => {
  1728. it('auto groups sibling spans and preserves tail spans', () => {
  1729. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1730. project_slug: '',
  1731. event_id: '',
  1732. });
  1733. for (let i = 0; i < 5; i++) {
  1734. root.children.push(
  1735. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1736. project_slug: '',
  1737. event_id: '',
  1738. })
  1739. );
  1740. }
  1741. root.children.push(
  1742. new TraceTreeNode(root, makeSpan({description: 'span', op: 'http'}), {
  1743. project_slug: '',
  1744. event_id: '',
  1745. })
  1746. );
  1747. expect(root.children.length).toBe(6);
  1748. TraceTree.AutogroupSiblingSpanNodes(root);
  1749. expect(root.children.length).toBe(2);
  1750. });
  1751. it('autogroups when number of children is exactly 5', () => {
  1752. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1753. project_slug: '',
  1754. event_id: '',
  1755. });
  1756. for (let i = 0; i < 5; i++) {
  1757. root.children.push(
  1758. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1759. project_slug: '',
  1760. event_id: '',
  1761. })
  1762. );
  1763. }
  1764. expect(root.children.length).toBe(5);
  1765. TraceTree.AutogroupSiblingSpanNodes(root);
  1766. expect(root.children.length).toBe(1);
  1767. });
  1768. it('collects errors and performance issues for sibling autogrouped node', () => {
  1769. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1770. project_slug: '',
  1771. event_id: '',
  1772. });
  1773. for (let i = 0; i < 5; i++) {
  1774. const node = new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1775. project_slug: '',
  1776. event_id: '',
  1777. });
  1778. node.errors.add(makeTraceError());
  1779. node.performance_issues.add(makeTracePerformanceIssue());
  1780. root.children.push(node);
  1781. }
  1782. expect(root.children.length).toBe(5);
  1783. TraceTree.AutogroupSiblingSpanNodes(root);
  1784. expect(root.children.length).toBe(1);
  1785. const autogroupedNode = root.children[0];
  1786. assertSiblingAutogroupedNode(autogroupedNode);
  1787. expect(autogroupedNode.has_errors).toBe(true);
  1788. expect(autogroupedNode.errors.size).toBe(5);
  1789. expect(autogroupedNode.performance_issues.size).toBe(5);
  1790. });
  1791. it('adds autogrouped siblings as children under autogrouped node', () => {
  1792. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1793. project_slug: '',
  1794. event_id: '',
  1795. });
  1796. for (let i = 0; i < 5; i++) {
  1797. root.children.push(
  1798. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1799. project_slug: '',
  1800. event_id: '',
  1801. })
  1802. );
  1803. }
  1804. expect(root.children.length).toBe(5);
  1805. TraceTree.AutogroupSiblingSpanNodes(root);
  1806. expect(root.children.length).toBe(1);
  1807. const autoGroupedNode = root.children[0];
  1808. assertAutogroupedNode(autoGroupedNode);
  1809. expect(autoGroupedNode.groupCount).toBe(5);
  1810. expect(autoGroupedNode.children.length).toBe(5);
  1811. });
  1812. it('autogroups when number of children is > 5', () => {
  1813. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1814. project_slug: '',
  1815. event_id: '',
  1816. });
  1817. for (let i = 0; i < 7; i++) {
  1818. root.children.push(
  1819. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1820. project_slug: '',
  1821. event_id: '',
  1822. })
  1823. );
  1824. }
  1825. expect(root.children.length).toBe(7);
  1826. TraceTree.AutogroupSiblingSpanNodes(root);
  1827. expect(root.children.length).toBe(1);
  1828. });
  1829. it('autogroups direct children case', () => {
  1830. // db db db
  1831. // http -> parent autogroup (3) -> parent autogroup (3)
  1832. // http http
  1833. // http http
  1834. // http
  1835. const root: TraceTreeNode<TraceTree.Span> = new TraceTreeNode(
  1836. null,
  1837. makeSpan({
  1838. description: `span1`,
  1839. span_id: `1`,
  1840. op: 'db',
  1841. }),
  1842. {project_slug: '', event_id: ''}
  1843. );
  1844. let last: TraceTreeNode<any> = root;
  1845. for (let i = 0; i < 3; i++) {
  1846. const node = new TraceTreeNode(
  1847. last,
  1848. makeSpan({
  1849. description: `span${i}`,
  1850. span_id: `${i}`,
  1851. op: 'http',
  1852. }),
  1853. {
  1854. project_slug: '',
  1855. event_id: '',
  1856. }
  1857. );
  1858. last.children.push(node);
  1859. last = node;
  1860. }
  1861. if (!root) {
  1862. throw new Error('root is null');
  1863. }
  1864. expect(root.children.length).toBe(1);
  1865. expect(root.children[0].children.length).toBe(1);
  1866. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1867. expect(root.children.length).toBe(1);
  1868. assertAutogroupedNode(root.children[0]);
  1869. expect(root.children[0].children.length).toBe(0);
  1870. root.children[0].expanded = true;
  1871. expect((root.children[0].children[0].value as RawSpanType).description).toBe(
  1872. 'span0'
  1873. );
  1874. });
  1875. it('collects errors and performance issues for parent autogrouped node', () => {
  1876. // db db db
  1877. // http -> parent autogroup (3) -> parent autogroup (3)
  1878. // http http
  1879. // http http
  1880. // http
  1881. const root: TraceTreeNode<TraceTree.Span> = new TraceTreeNode(
  1882. null,
  1883. makeSpan({
  1884. description: `span1`,
  1885. span_id: `1`,
  1886. op: 'db',
  1887. }),
  1888. {project_slug: '', event_id: ''}
  1889. );
  1890. let last: TraceTreeNode<any> = root;
  1891. for (let i = 0; i < 3; i++) {
  1892. const node = new TraceTreeNode(
  1893. last,
  1894. makeSpan({
  1895. description: `span${i}`,
  1896. span_id: `${i}`,
  1897. op: 'http',
  1898. }),
  1899. {
  1900. project_slug: '',
  1901. event_id: '',
  1902. }
  1903. );
  1904. node.errors.add(makeTraceError());
  1905. node.performance_issues.add(makeTracePerformanceIssue());
  1906. last.children.push(node);
  1907. last = node;
  1908. }
  1909. if (!root) {
  1910. throw new Error('root is null');
  1911. }
  1912. expect(root.children.length).toBe(1);
  1913. expect(root.children[0].children.length).toBe(1);
  1914. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1915. expect(root.children.length).toBe(1);
  1916. assertAutogroupedNode(root.children[0]);
  1917. expect(root.children[0].has_errors).toBe(true);
  1918. expect(root.children[0].errors.size).toBe(3);
  1919. expect(root.children[0].performance_issues.size).toBe(3);
  1920. });
  1921. it('autogrouping direct children skips rendering intermediary nodes', () => {
  1922. // db db db
  1923. // http autogrouped (3) autogrouped (3)
  1924. // http -> db -> http
  1925. // http http
  1926. // db http
  1927. // db
  1928. const root = new TraceTreeNode(
  1929. null,
  1930. makeSpan({span_id: 'span1', description: 'span1', op: 'db'}),
  1931. {
  1932. project_slug: '',
  1933. event_id: '',
  1934. }
  1935. );
  1936. let last = root;
  1937. for (let i = 0; i < 4; i++) {
  1938. const node = new TraceTreeNode(
  1939. last,
  1940. makeSpan({
  1941. span_id: `span`,
  1942. description: `span`,
  1943. op: i === 3 ? 'db' : 'http',
  1944. }),
  1945. {
  1946. project_slug: '',
  1947. event_id: '',
  1948. }
  1949. );
  1950. last.children.push(node);
  1951. last = node;
  1952. }
  1953. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1954. const autoGroupedNode = root.children[0];
  1955. assertAutogroupedNode(autoGroupedNode);
  1956. expect(autoGroupedNode.children.length).toBe(1);
  1957. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('db');
  1958. autoGroupedNode.expanded = true;
  1959. expect(autoGroupedNode.children.length).toBe(1);
  1960. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('http');
  1961. });
  1962. it('nested direct autogrouping', () => {
  1963. // db db db
  1964. // http -> parent autogroup (3) -> parent autogroup (3)
  1965. // http db http
  1966. // http parent autogroup (3) http
  1967. // db http
  1968. // http db
  1969. // http parent autogrouped (3)
  1970. // http http
  1971. // http
  1972. // http
  1973. const root = new TraceTreeNode(
  1974. null,
  1975. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1976. {
  1977. project_slug: '',
  1978. event_id: '',
  1979. }
  1980. );
  1981. let last = root;
  1982. for (let i = 0; i < 3; i++) {
  1983. if (i === 1) {
  1984. const autogroupBreakingSpan = new TraceTreeNode(
  1985. last,
  1986. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1987. {
  1988. project_slug: '',
  1989. event_id: '',
  1990. }
  1991. );
  1992. last.children.push(autogroupBreakingSpan);
  1993. last = autogroupBreakingSpan;
  1994. } else {
  1995. for (let j = 0; j < 3; j++) {
  1996. const node = new TraceTreeNode(
  1997. last,
  1998. makeSpan({span_id: `span${j}`, description: `span${j}`, op: 'http'}),
  1999. {
  2000. project_slug: '',
  2001. event_id: '',
  2002. }
  2003. );
  2004. last.children.push(node);
  2005. last = node;
  2006. }
  2007. }
  2008. }
  2009. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  2010. assertAutogroupedNode(root.children[0]);
  2011. assertAutogroupedNode(root.children[0].children[0].children[0]);
  2012. });
  2013. it('sibling autogrouping', () => {
  2014. // db db
  2015. // http sibling autogrouped (5)
  2016. // http
  2017. // http ->
  2018. // http
  2019. // http
  2020. const root = new TraceTreeNode(
  2021. null,
  2022. makeTransaction({start_timestamp: 0, timestamp: 10}),
  2023. {
  2024. project_slug: '',
  2025. event_id: '',
  2026. }
  2027. );
  2028. for (let i = 0; i < 5; i++) {
  2029. root.children.push(
  2030. new TraceTreeNode(
  2031. root,
  2032. makeSpan({start_timestamp: i, op: 'db', timestamp: i + 1}),
  2033. {
  2034. project_slug: '',
  2035. event_id: '',
  2036. }
  2037. )
  2038. );
  2039. }
  2040. TraceTree.AutogroupSiblingSpanNodes(root);
  2041. expect(root.children.length).toBe(1);
  2042. assertAutogroupedNode(root.children[0]);
  2043. });
  2044. it('multiple sibling autogrouping', () => {
  2045. // db db
  2046. // http sibling autogrouped (5)
  2047. // http db
  2048. // http -> sibling autogrouped (5)
  2049. // http
  2050. // http
  2051. // db
  2052. // http
  2053. // http
  2054. // http
  2055. // http
  2056. // http
  2057. const root = new TraceTreeNode(
  2058. null,
  2059. makeTransaction({start_timestamp: 0, timestamp: 10}),
  2060. {
  2061. project_slug: '',
  2062. event_id: '',
  2063. }
  2064. );
  2065. for (let i = 0; i < 10; i++) {
  2066. if (i === 5) {
  2067. root.children.push(
  2068. new TraceTreeNode(
  2069. root,
  2070. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'db'}),
  2071. {
  2072. project_slug: '',
  2073. event_id: '',
  2074. }
  2075. )
  2076. );
  2077. }
  2078. root.children.push(
  2079. new TraceTreeNode(
  2080. root,
  2081. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'http'}),
  2082. {
  2083. project_slug: '',
  2084. event_id: '',
  2085. }
  2086. )
  2087. );
  2088. }
  2089. TraceTree.AutogroupSiblingSpanNodes(root);
  2090. assertAutogroupedNode(root.children[0]);
  2091. expect(root.children).toHaveLength(3);
  2092. assertAutogroupedNode(root.children[2]);
  2093. });
  2094. it('renders children of autogrouped direct children nodes', async () => {
  2095. const tree = TraceTree.FromTrace(
  2096. makeTrace({
  2097. transactions: [
  2098. makeTransaction({
  2099. transaction: '/',
  2100. project_slug: 'project',
  2101. event_id: 'event_id',
  2102. }),
  2103. ],
  2104. })
  2105. );
  2106. MockApiClient.addMockResponse({
  2107. url: EVENT_REQUEST_URL,
  2108. method: 'GET',
  2109. body: makeEvent({}, [
  2110. makeSpan({description: 'parent span', op: 'http', span_id: '1'}),
  2111. makeSpan({description: 'span', op: 'db', span_id: '2', parent_span_id: '1'}),
  2112. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  2113. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  2114. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  2115. makeSpan({
  2116. description: 'span',
  2117. op: 'redis',
  2118. span_id: '6',
  2119. parent_span_id: '5',
  2120. }),
  2121. makeSpan({description: 'span', op: 'https', parent_span_id: '1'}),
  2122. ]),
  2123. });
  2124. expect(tree.list.length).toBe(2);
  2125. tree.zoomIn(tree.list[1], true, {
  2126. api: new MockApiClient(),
  2127. organization: OrganizationFixture(),
  2128. });
  2129. await waitFor(() => {
  2130. expect(tree.list.length).toBe(6);
  2131. });
  2132. const autogroupedNode = tree.list[tree.list.length - 3];
  2133. assertParentAutogroupedNode(autogroupedNode);
  2134. expect('autogrouped_by' in autogroupedNode?.value).toBeTruthy();
  2135. expect(autogroupedNode.groupCount).toBe(4);
  2136. expect(autogroupedNode.head.value.span_id).toBe('2');
  2137. expect(autogroupedNode.tail.value.span_id).toBe('5');
  2138. // Expand autogrouped node
  2139. expect(tree.expand(autogroupedNode, true)).toBe(true);
  2140. expect(tree.list.length).toBe(10);
  2141. // Collapse autogrouped node
  2142. expect(tree.expand(autogroupedNode, false)).toBe(true);
  2143. expect(tree.list.length).toBe(6);
  2144. expect(autogroupedNode.children[0].depth).toBe(4);
  2145. });
  2146. });
  2147. });