traceTree.spec.tsx 80 KB

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