traceTree.spec.tsx 80 KB

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