traceTree.spec.tsx 68 KB

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