spanTreeModel.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. import {action, computed, makeObservable, observable} from 'mobx';
  2. import type {Client} from 'sentry/api';
  3. import {t} from 'sentry/locale';
  4. import type {AggregateEventTransaction, EventTransaction} from 'sentry/types/event';
  5. import type {TraceInfo} from 'sentry/views/performance/traceDetails/types';
  6. import type {ActiveOperationFilter} from './filter';
  7. import type {
  8. DescendantGroup,
  9. EnhancedProcessedSpanType,
  10. EnhancedSpan,
  11. FetchEmbeddedChildrenState,
  12. FilterSpans,
  13. OrphanTreeDepth,
  14. RawSpanType,
  15. SpanChildrenLookupType,
  16. SpanType,
  17. TraceBound,
  18. TreeDepthType,
  19. } from './types';
  20. import type {SpanBoundsType, SpanGeneratedBoundsType} from './utils';
  21. import {
  22. generateRootSpan,
  23. getSiblingGroupKey,
  24. getSpanID,
  25. getSpanOperation,
  26. groupShouldBeHidden,
  27. isEventFromBrowserJavaScriptSDK,
  28. isOrphanSpan,
  29. parseTrace,
  30. SpanSubTimingMark,
  31. subTimingMarkToTime,
  32. } from './utils';
  33. export const MIN_SIBLING_GROUP_SIZE = 5;
  34. class SpanTreeModel {
  35. api: Client;
  36. // readonly state
  37. span: Readonly<SpanType>;
  38. children: Array<SpanTreeModel> = [];
  39. isRoot: boolean;
  40. // readable/writable state
  41. fetchEmbeddedChildrenState: FetchEmbeddedChildrenState = 'idle';
  42. showEmbeddedChildren: boolean = false;
  43. embeddedChildren: Array<SpanTreeModel> = [];
  44. isEmbeddedTransactionTimeAdjusted: boolean = false;
  45. // This controls if a chain of nested spans that are the only sibling to be visually grouped together or not.
  46. // On initial render, they're visually grouped together.
  47. isNestedSpanGroupExpanded: boolean = false;
  48. // Entries in this set will follow the format 'op.description'.
  49. // An entry in this set indicates that all siblings with the op and description should be left ungrouped
  50. expandedSiblingGroups: Set<string> = new Set();
  51. traceInfo: TraceInfo | undefined = undefined;
  52. constructor(
  53. parentSpan: SpanType,
  54. childSpans: SpanChildrenLookupType,
  55. api: Client,
  56. isRoot: boolean = false,
  57. traceInfo?: TraceInfo
  58. ) {
  59. this.api = api;
  60. this.span = parentSpan;
  61. this.isRoot = isRoot;
  62. this.traceInfo = traceInfo;
  63. const spanID = getSpanID(parentSpan);
  64. const spanChildren: Array<RawSpanType> = childSpans?.[spanID] ?? [];
  65. // Mark descendents as being rendered. This is to address potential recursion issues due to malformed data.
  66. // For example if a span has a span_id that's identical to its parent_span_id.
  67. childSpans = {
  68. ...childSpans,
  69. };
  70. delete childSpans[spanID];
  71. this.children = spanChildren.map(span => {
  72. return new SpanTreeModel(span, childSpans, api, false, this.traceInfo);
  73. });
  74. makeObservable(this, {
  75. operationNameCounts: computed.struct,
  76. showEmbeddedChildren: observable,
  77. embeddedChildren: observable,
  78. fetchEmbeddedChildrenState: observable,
  79. fetchEmbeddedTransactions: action,
  80. isNestedSpanGroupExpanded: observable,
  81. toggleNestedSpanGroup: action,
  82. expandedSiblingGroups: observable,
  83. toggleSiblingSpanGroup: action,
  84. isEmbeddedTransactionTimeAdjusted: observable,
  85. });
  86. }
  87. get operationNameCounts(): Map<string, number> {
  88. const result = new Map<string, number>();
  89. const operationName = this.span.op;
  90. if (typeof operationName === 'string' && operationName.length > 0) {
  91. result.set(operationName, 1);
  92. }
  93. for (const directChild of this.children) {
  94. const operationNameCounts = directChild.operationNameCounts;
  95. for (const [key, count] of operationNameCounts) {
  96. result.set(key, (result.get(key) ?? 0) + count);
  97. }
  98. }
  99. // sort alphabetically using case insensitive comparison
  100. return new Map(
  101. [...result].sort((a, b) =>
  102. String(a[0]).localeCompare(b[0], undefined, {sensitivity: 'base'})
  103. )
  104. );
  105. }
  106. isSpanFilteredOut = (
  107. props: {
  108. filterSpans: FilterSpans | undefined;
  109. operationNameFilters: ActiveOperationFilter;
  110. },
  111. spanModel: SpanTreeModel
  112. ): boolean => {
  113. const {operationNameFilters, filterSpans} = props;
  114. if (operationNameFilters.type === 'active_filter') {
  115. const operationName = getSpanOperation(spanModel.span);
  116. if (
  117. typeof operationName === 'string' &&
  118. !operationNameFilters.operationNames.has(operationName)
  119. ) {
  120. return true;
  121. }
  122. }
  123. if (!filterSpans) {
  124. return false;
  125. }
  126. return !filterSpans.spanIDs.has(getSpanID(spanModel.span));
  127. };
  128. generateSpanGap(
  129. span: SpanType,
  130. event: Readonly<EventTransaction | AggregateEventTransaction>,
  131. previousSiblingEndTimestamp: number | undefined,
  132. treeDepth: number,
  133. continuingTreeDepths: Array<TreeDepthType>
  134. ): EnhancedProcessedSpanType | undefined {
  135. // hide gap spans (i.e. "missing instrumentation" spans) for browser js transactions,
  136. // since they're not useful to indicate
  137. const shouldIncludeGap = !isEventFromBrowserJavaScriptSDK(event);
  138. const isValidGap =
  139. shouldIncludeGap &&
  140. typeof previousSiblingEndTimestamp === 'number' &&
  141. previousSiblingEndTimestamp < span.start_timestamp &&
  142. // gap is at least 100 ms
  143. span.start_timestamp - previousSiblingEndTimestamp >= 0.1;
  144. if (!isValidGap) {
  145. return undefined;
  146. }
  147. const gapSpan: EnhancedProcessedSpanType = {
  148. type: 'gap',
  149. span: {
  150. type: 'gap',
  151. start_timestamp: previousSiblingEndTimestamp || span.start_timestamp,
  152. timestamp: span.start_timestamp, // this is essentially end_timestamp
  153. description: t('Missing span instrumentation'),
  154. isOrphan: isOrphanSpan(span),
  155. },
  156. numOfSpanChildren: 0,
  157. treeDepth,
  158. isLastSibling: false,
  159. continuingTreeDepths,
  160. fetchEmbeddedChildrenState: 'idle',
  161. showEmbeddedChildren: false,
  162. toggleEmbeddedChildren: undefined,
  163. isEmbeddedTransactionTimeAdjusted: this.isEmbeddedTransactionTimeAdjusted,
  164. };
  165. return gapSpan;
  166. }
  167. getSpansList = (props: {
  168. addTraceBounds: (bounds: TraceBound) => void;
  169. continuingTreeDepths: Array<TreeDepthType>;
  170. directParent: SpanTreeModel | null;
  171. event: Readonly<EventTransaction | AggregateEventTransaction>;
  172. filterSpans: FilterSpans | undefined;
  173. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  174. hiddenSpanSubTrees: Set<string>;
  175. isLastSibling: boolean;
  176. isNestedSpanGroupExpanded: boolean;
  177. isOnlySibling: boolean;
  178. operationNameFilters: ActiveOperationFilter;
  179. previousSiblingEndTimestamp: number | undefined;
  180. removeTraceBounds: (eventSlug: string) => void;
  181. spanAncestors: Set<string>;
  182. spanNestedGrouping: EnhancedSpan[] | undefined;
  183. toggleNestedSpanGroup: (() => void) | undefined;
  184. treeDepth: number;
  185. focusedSpanIds?: Set<string>;
  186. }): EnhancedProcessedSpanType[] => {
  187. const {
  188. operationNameFilters,
  189. generateBounds,
  190. isLastSibling,
  191. hiddenSpanSubTrees,
  192. // The set of ancestor span IDs whose sub-tree that the span belongs to
  193. spanAncestors,
  194. filterSpans,
  195. previousSiblingEndTimestamp,
  196. event,
  197. isOnlySibling,
  198. spanNestedGrouping,
  199. toggleNestedSpanGroup,
  200. isNestedSpanGroupExpanded,
  201. addTraceBounds,
  202. removeTraceBounds,
  203. focusedSpanIds,
  204. } = props;
  205. let {treeDepth, continuingTreeDepths} = props;
  206. const parentSpanID = getSpanID(this.span);
  207. const nextSpanAncestors = new Set(spanAncestors);
  208. nextSpanAncestors.add(parentSpanID);
  209. const descendantsSource = this.showEmbeddedChildren
  210. ? [...this.embeddedChildren, ...this.children]
  211. : this.children;
  212. const isNotLastSpanOfGroup =
  213. isOnlySibling && !this.isRoot && descendantsSource.length === 1;
  214. const shouldGroup = isNotLastSpanOfGroup;
  215. const hideSpanTree = hiddenSpanSubTrees.has(parentSpanID);
  216. const isLastSpanOfGroup =
  217. isOnlySibling && !this.isRoot && (descendantsSource.length !== 1 || hideSpanTree);
  218. const isFirstSpanOfGroup =
  219. shouldGroup &&
  220. (spanNestedGrouping === undefined ||
  221. (Array.isArray(spanNestedGrouping) && spanNestedGrouping.length === 0));
  222. if (
  223. isLastSpanOfGroup &&
  224. Array.isArray(spanNestedGrouping) &&
  225. spanNestedGrouping.length >= 1 &&
  226. !isNestedSpanGroupExpanded
  227. ) {
  228. // We always want to indent the last span of the span group chain
  229. treeDepth = treeDepth + 1;
  230. // For a collapsed span group chain to be useful, we prefer span groupings
  231. // that are two or more spans.
  232. // Since there is no concept of "backtracking" when constructing the span tree,
  233. // we will need to reconstruct the tree depth information. This is only neccessary
  234. // when the span group chain is hidden/collapsed.
  235. if (spanNestedGrouping.length === 1) {
  236. const treeDepthEntry = isOrphanSpan(spanNestedGrouping[0].span)
  237. ? ({type: 'orphan', depth: spanNestedGrouping[0].treeDepth} as OrphanTreeDepth)
  238. : spanNestedGrouping[0].treeDepth;
  239. if (!spanNestedGrouping[0].isLastSibling) {
  240. continuingTreeDepths = [...continuingTreeDepths, treeDepthEntry];
  241. }
  242. }
  243. }
  244. // Criteria for propagating information about the span group to the last span of the span group chain
  245. const spanGroupingCriteria =
  246. isLastSpanOfGroup &&
  247. Array.isArray(spanNestedGrouping) &&
  248. spanNestedGrouping.length > 1;
  249. const wrappedSpan: EnhancedSpan = {
  250. type: this.isRoot ? 'root_span' : 'span',
  251. span: this.span,
  252. numOfSpanChildren: descendantsSource.length,
  253. treeDepth,
  254. isLastSibling,
  255. continuingTreeDepths,
  256. fetchEmbeddedChildrenState: this.fetchEmbeddedChildrenState,
  257. showEmbeddedChildren: this.showEmbeddedChildren,
  258. toggleEmbeddedChildren: this.makeToggleEmbeddedChildren({
  259. addTraceBounds,
  260. removeTraceBounds,
  261. }),
  262. toggleNestedSpanGroup:
  263. spanGroupingCriteria && toggleNestedSpanGroup && !isNestedSpanGroupExpanded
  264. ? toggleNestedSpanGroup
  265. : isFirstSpanOfGroup && this.isNestedSpanGroupExpanded && !hideSpanTree
  266. ? this.toggleNestedSpanGroup
  267. : undefined,
  268. toggleSiblingSpanGroup: undefined,
  269. isEmbeddedTransactionTimeAdjusted: this.isEmbeddedTransactionTimeAdjusted,
  270. };
  271. if (wrappedSpan.type === 'root_span') {
  272. // @ts-expect-error
  273. delete wrappedSpan.toggleNestedSpanGroup;
  274. }
  275. const treeDepthEntry = isOrphanSpan(this.span)
  276. ? ({type: 'orphan', depth: treeDepth} as OrphanTreeDepth)
  277. : treeDepth;
  278. const shouldHideSpanOfGroup =
  279. shouldGroup &&
  280. !isLastSpanOfGroup &&
  281. ((toggleNestedSpanGroup === undefined && !this.isNestedSpanGroupExpanded) ||
  282. (toggleNestedSpanGroup !== undefined && !isNestedSpanGroupExpanded));
  283. const descendantContinuingTreeDepths =
  284. isLastSibling || shouldHideSpanOfGroup
  285. ? continuingTreeDepths
  286. : [...continuingTreeDepths, treeDepthEntry];
  287. for (const hiddenSpanSubTree of hiddenSpanSubTrees) {
  288. if (spanAncestors.has(hiddenSpanSubTree)) {
  289. // If this span is hidden, then all the descendants are hidden as well
  290. return [];
  291. }
  292. }
  293. const groupedDescendants: DescendantGroup[] = [];
  294. // Used to number sibling groups in case there are multiple groups with the same op and description
  295. const siblingGroupOccurrenceMap = {};
  296. const addGroupToMap = (prevSpanModel: SpanTreeModel, group: SpanTreeModel[]) => {
  297. if (!group.length) {
  298. return;
  299. }
  300. const groupKey = `${prevSpanModel.span.op}.${prevSpanModel.span.description}`;
  301. if (!siblingGroupOccurrenceMap[groupKey]) {
  302. siblingGroupOccurrenceMap[groupKey] = 1;
  303. } else {
  304. siblingGroupOccurrenceMap[groupKey] += 1;
  305. }
  306. groupedDescendants.push({
  307. group,
  308. occurrence: siblingGroupOccurrenceMap[groupKey],
  309. });
  310. };
  311. if (descendantsSource?.length >= MIN_SIBLING_GROUP_SIZE) {
  312. let prevSpanModel = descendantsSource[0];
  313. let currentGroup = [prevSpanModel];
  314. for (let i = 1; i < descendantsSource.length; i++) {
  315. const currSpanModel = descendantsSource[i];
  316. // We want to group siblings only if they share the same op and description, and if they have no children
  317. if (
  318. prevSpanModel.span.op === currSpanModel.span.op &&
  319. prevSpanModel.span.description === currSpanModel.span.description &&
  320. currSpanModel.children.length === 0
  321. ) {
  322. currentGroup.push(currSpanModel);
  323. } else {
  324. addGroupToMap(prevSpanModel, currentGroup);
  325. if (currSpanModel.children.length) {
  326. currentGroup = [currSpanModel];
  327. groupedDescendants.push({group: currentGroup});
  328. currentGroup = [];
  329. } else {
  330. currentGroup = [currSpanModel];
  331. }
  332. }
  333. prevSpanModel = currSpanModel;
  334. }
  335. addGroupToMap(prevSpanModel, currentGroup);
  336. } else if (descendantsSource.length >= 1) {
  337. groupedDescendants.push({group: descendantsSource});
  338. }
  339. const descendants = (hideSpanTree ? [] : groupedDescendants).reduce(
  340. (
  341. acc: {
  342. descendants: EnhancedProcessedSpanType[];
  343. previousSiblingEndTimestamp: number | undefined;
  344. },
  345. {group, occurrence},
  346. groupIndex
  347. ) => {
  348. // Groups less than 5 indicate that the spans should be left ungrouped
  349. if (group.length < MIN_SIBLING_GROUP_SIZE) {
  350. group.forEach((spanModel, index) => {
  351. acc.descendants.push(
  352. ...spanModel.getSpansList({
  353. operationNameFilters,
  354. generateBounds,
  355. treeDepth: shouldHideSpanOfGroup ? treeDepth : treeDepth + 1,
  356. isLastSibling:
  357. groupIndex === groupedDescendants.length - 1 &&
  358. index === group.length - 1,
  359. continuingTreeDepths: descendantContinuingTreeDepths,
  360. hiddenSpanSubTrees,
  361. spanAncestors: new Set(nextSpanAncestors),
  362. filterSpans,
  363. previousSiblingEndTimestamp: acc.previousSiblingEndTimestamp,
  364. event,
  365. isOnlySibling: descendantsSource.length === 1,
  366. spanNestedGrouping: shouldGroup
  367. ? [...(spanNestedGrouping ?? []), wrappedSpan]
  368. : undefined,
  369. toggleNestedSpanGroup: isNotLastSpanOfGroup
  370. ? toggleNestedSpanGroup === undefined
  371. ? this.toggleNestedSpanGroup
  372. : toggleNestedSpanGroup
  373. : undefined,
  374. isNestedSpanGroupExpanded: isNotLastSpanOfGroup
  375. ? toggleNestedSpanGroup === undefined
  376. ? this.isNestedSpanGroupExpanded
  377. : isNestedSpanGroupExpanded
  378. : false,
  379. addTraceBounds,
  380. removeTraceBounds,
  381. focusedSpanIds,
  382. directParent: this,
  383. })
  384. );
  385. acc.previousSiblingEndTimestamp = spanModel.span.timestamp;
  386. });
  387. return acc;
  388. }
  389. const key = getSiblingGroupKey(group[0].span, occurrence);
  390. if (this.expandedSiblingGroups.has(key)) {
  391. // This check is needed here, since it is possible that a user could be filtering for a specific span ID.
  392. // In this case, we must add only the specified span into the accumulator's descendants
  393. group.forEach((spanModel, index) => {
  394. if (
  395. this.isSpanFilteredOut(props, spanModel) ||
  396. (focusedSpanIds && !focusedSpanIds.has(spanModel.span.span_id))
  397. ) {
  398. acc.descendants.push({
  399. type: 'filtered_out',
  400. span: spanModel.span,
  401. });
  402. } else {
  403. const enhancedSibling: EnhancedSpan = {
  404. type: 'span',
  405. span: spanModel.span,
  406. numOfSpanChildren: 0,
  407. treeDepth: treeDepth + 1,
  408. isLastSibling:
  409. index === group.length - 1 &&
  410. groupIndex === groupedDescendants.length - 1,
  411. isFirstSiblingOfGroup: index === 0,
  412. groupOccurrence: occurrence,
  413. continuingTreeDepths: descendantContinuingTreeDepths,
  414. fetchEmbeddedChildrenState: spanModel.fetchEmbeddedChildrenState,
  415. showEmbeddedChildren: spanModel.showEmbeddedChildren,
  416. toggleEmbeddedChildren: spanModel.makeToggleEmbeddedChildren({
  417. addTraceBounds,
  418. removeTraceBounds,
  419. }),
  420. toggleNestedSpanGroup: undefined,
  421. toggleSiblingSpanGroup:
  422. index === 0 ? this.toggleSiblingSpanGroup : undefined,
  423. isEmbeddedTransactionTimeAdjusted:
  424. spanModel.isEmbeddedTransactionTimeAdjusted,
  425. };
  426. const bounds = generateBounds({
  427. startTimestamp: spanModel.span.start_timestamp,
  428. endTimestamp: spanModel.span.timestamp,
  429. });
  430. const gapSpan = this.generateSpanGap(
  431. group[0].span,
  432. event,
  433. acc.previousSiblingEndTimestamp,
  434. treeDepth + 1,
  435. continuingTreeDepths
  436. );
  437. if (gapSpan) {
  438. acc.descendants.push(gapSpan);
  439. }
  440. acc.previousSiblingEndTimestamp = spanModel.span.timestamp;
  441. // It's possible that a section in the minimap is selected so some spans in this group may be out of view
  442. bounds.isSpanVisibleInView
  443. ? acc.descendants.push(enhancedSibling)
  444. : acc.descendants.push({
  445. type: 'filtered_out',
  446. span: spanModel.span,
  447. });
  448. }
  449. });
  450. return acc;
  451. }
  452. // Since we are not recursively traversing elements in this group, need to check
  453. // if the spans are filtered or out of bounds here
  454. if (
  455. this.isSpanFilteredOut(props, group[0]) ||
  456. groupShouldBeHidden(group, focusedSpanIds)
  457. ) {
  458. group.forEach(spanModel => {
  459. acc.descendants.push({
  460. type: 'filtered_out',
  461. span: spanModel.span,
  462. });
  463. });
  464. return acc;
  465. }
  466. const bounds = generateBounds({
  467. startTimestamp: group[0].span.start_timestamp,
  468. endTimestamp: group[group.length - 1].span.timestamp,
  469. });
  470. if (!bounds.isSpanVisibleInView) {
  471. group.forEach(spanModel =>
  472. acc.descendants.push({
  473. type: 'out_of_view',
  474. span: spanModel.span,
  475. })
  476. );
  477. return acc;
  478. }
  479. const gapSpan = this.generateSpanGap(
  480. group[0].span,
  481. event,
  482. acc.previousSiblingEndTimestamp,
  483. treeDepth + 1,
  484. continuingTreeDepths
  485. );
  486. if (gapSpan) {
  487. acc.descendants.push(gapSpan);
  488. }
  489. // Since the group is not expanded, return a singular grouped span bar
  490. const wrappedSiblings: EnhancedSpan[] = group.map((spanModel, index) => {
  491. const enhancedSibling: EnhancedSpan = {
  492. type: 'span',
  493. span: spanModel.span,
  494. numOfSpanChildren: 0,
  495. treeDepth: treeDepth + 1,
  496. isLastSibling:
  497. index === group.length - 1 && groupIndex === groupedDescendants.length - 1,
  498. isFirstSiblingOfGroup: index === 0,
  499. groupOccurrence: occurrence,
  500. continuingTreeDepths: descendantContinuingTreeDepths,
  501. fetchEmbeddedChildrenState: spanModel.fetchEmbeddedChildrenState,
  502. showEmbeddedChildren: spanModel.showEmbeddedChildren,
  503. toggleEmbeddedChildren: spanModel.makeToggleEmbeddedChildren({
  504. addTraceBounds,
  505. removeTraceBounds,
  506. }),
  507. toggleNestedSpanGroup: undefined,
  508. toggleSiblingSpanGroup: index === 0 ? this.toggleSiblingSpanGroup : undefined,
  509. isEmbeddedTransactionTimeAdjusted:
  510. spanModel.isEmbeddedTransactionTimeAdjusted,
  511. };
  512. return enhancedSibling;
  513. });
  514. const groupedSiblingsSpan: EnhancedProcessedSpanType = {
  515. type: 'span_group_siblings',
  516. span: this.span,
  517. treeDepth: treeDepth + 1,
  518. continuingTreeDepths: descendantContinuingTreeDepths,
  519. spanSiblingGrouping: wrappedSiblings,
  520. isLastSibling: groupIndex === groupedDescendants.length - 1,
  521. occurrence: occurrence ?? 0,
  522. toggleSiblingSpanGroup: this.toggleSiblingSpanGroup,
  523. };
  524. acc.previousSiblingEndTimestamp =
  525. wrappedSiblings[wrappedSiblings.length - 1].span.timestamp;
  526. acc.descendants.push(groupedSiblingsSpan);
  527. return acc;
  528. },
  529. {
  530. descendants: [],
  531. previousSiblingEndTimestamp: undefined,
  532. }
  533. ).descendants;
  534. if (
  535. this.isSpanFilteredOut(props, this) ||
  536. (focusedSpanIds && !focusedSpanIds.has(this.span.span_id))
  537. ) {
  538. return [
  539. {
  540. type: 'filtered_out',
  541. span: this.span,
  542. },
  543. ...descendants,
  544. ];
  545. }
  546. const bounds = generateBounds({
  547. startTimestamp: this.span.start_timestamp,
  548. endTimestamp: this.span.timestamp,
  549. });
  550. const isCurrentSpanOutOfView = !bounds.isSpanVisibleInView;
  551. if (isCurrentSpanOutOfView) {
  552. return [
  553. {
  554. type: 'out_of_view',
  555. span: this.span,
  556. },
  557. ...descendants,
  558. ];
  559. }
  560. if (shouldHideSpanOfGroup) {
  561. return [...descendants];
  562. }
  563. if (
  564. isLastSpanOfGroup &&
  565. Array.isArray(spanNestedGrouping) &&
  566. spanNestedGrouping.length > 1 &&
  567. !isNestedSpanGroupExpanded &&
  568. wrappedSpan.type === 'span'
  569. ) {
  570. const spanGroupChain: EnhancedProcessedSpanType = {
  571. type: 'span_group_chain',
  572. span: this.span,
  573. treeDepth: treeDepth - 1,
  574. continuingTreeDepths,
  575. spanNestedGrouping,
  576. isNestedSpanGroupExpanded,
  577. toggleNestedSpanGroup: wrappedSpan.toggleNestedSpanGroup,
  578. toggleSiblingSpanGroup: undefined,
  579. };
  580. return [
  581. spanGroupChain,
  582. {...wrappedSpan, toggleNestedSpanGroup: undefined},
  583. ...descendants,
  584. ];
  585. }
  586. if (
  587. isFirstSpanOfGroup &&
  588. this.isNestedSpanGroupExpanded &&
  589. !hideSpanTree &&
  590. descendants.length <= 1 &&
  591. wrappedSpan.type === 'span'
  592. ) {
  593. // If we know the descendants will be one span or less, we remove the "regroup" feature (therefore hide it)
  594. // by setting toggleNestedSpanGroup to be undefined for the first span of the group chain.
  595. wrappedSpan.toggleNestedSpanGroup = undefined;
  596. }
  597. // Do not autogroup groups that will only have two spans
  598. if (
  599. isLastSpanOfGroup &&
  600. Array.isArray(spanNestedGrouping) &&
  601. spanNestedGrouping.length === 1
  602. ) {
  603. if (!isNestedSpanGroupExpanded) {
  604. const parentSpan = spanNestedGrouping[0].span;
  605. const parentSpanBounds = generateBounds({
  606. startTimestamp: parentSpan.start_timestamp,
  607. endTimestamp: parentSpan.timestamp,
  608. });
  609. const isParentSpanOutOfView = !parentSpanBounds.isSpanVisibleInView;
  610. if (!isParentSpanOutOfView) {
  611. return [spanNestedGrouping[0], wrappedSpan, ...descendants];
  612. }
  613. }
  614. return [wrappedSpan, ...descendants];
  615. }
  616. const gapSpan = this.generateSpanGap(
  617. this.span,
  618. event,
  619. previousSiblingEndTimestamp,
  620. treeDepth,
  621. continuingTreeDepths
  622. );
  623. if (gapSpan) {
  624. return [gapSpan, wrappedSpan, ...descendants];
  625. }
  626. return [wrappedSpan, ...descendants];
  627. };
  628. makeToggleEmbeddedChildren = ({
  629. addTraceBounds,
  630. removeTraceBounds,
  631. }: {
  632. addTraceBounds: (bounds: TraceBound) => void;
  633. removeTraceBounds: (eventSlug: string) => void;
  634. }) =>
  635. action('toggleEmbeddedChildren', (orgSlug: string, eventSlugs: string[]) => {
  636. this.showEmbeddedChildren = !this.showEmbeddedChildren;
  637. this.fetchEmbeddedChildrenState = 'idle';
  638. if (!this.showEmbeddedChildren) {
  639. if (this.embeddedChildren.length > 0) {
  640. this.embeddedChildren.forEach(child => {
  641. removeTraceBounds(child.generateTraceBounds().spanId);
  642. });
  643. }
  644. }
  645. if (this.showEmbeddedChildren) {
  646. if (this.embeddedChildren.length === 0) {
  647. return this.fetchEmbeddedTransactions({
  648. orgSlug,
  649. eventSlugs,
  650. addTraceBounds,
  651. });
  652. }
  653. this.embeddedChildren.forEach(child => {
  654. addTraceBounds(child.generateTraceBounds());
  655. });
  656. }
  657. return Promise.resolve(undefined);
  658. });
  659. fetchEmbeddedTransactions({
  660. orgSlug,
  661. eventSlugs,
  662. addTraceBounds,
  663. }: {
  664. addTraceBounds: (bounds: TraceBound) => void;
  665. eventSlugs: string[];
  666. orgSlug: string;
  667. }) {
  668. const urls = eventSlugs.map(
  669. eventSlug => `/organizations/${orgSlug}/events/${eventSlug}/`
  670. );
  671. this.fetchEmbeddedChildrenState = 'loading_embedded_transactions';
  672. const promiseArray = urls.map(url =>
  673. this.api
  674. .requestPromise(url, {
  675. method: 'GET',
  676. query: {},
  677. })
  678. .then(
  679. action('fetchEmbeddedTransactionsSuccess', (event: EventTransaction) => {
  680. if (!event) {
  681. return;
  682. }
  683. const parsedTrace = parseTrace(event);
  684. // We need to adjust the timestamps for this embedded transaction only if it is not within the bounds of its parent span
  685. if (
  686. parsedTrace.traceStartTimestamp < this.span.start_timestamp ||
  687. parsedTrace.traceEndTimestamp > this.span.timestamp
  688. ) {
  689. const responseStart = subTimingMarkToTime(
  690. this.span,
  691. SpanSubTimingMark.HTTP_RESPONSE_START
  692. ); // Response start is a better approximation
  693. const spanTimeOffset =
  694. responseStart && !this.traceInfo
  695. ? responseStart - parsedTrace.traceEndTimestamp
  696. : this.span.start_timestamp - parsedTrace.traceStartTimestamp;
  697. parsedTrace.traceStartTimestamp += spanTimeOffset;
  698. parsedTrace.traceEndTimestamp += spanTimeOffset;
  699. parsedTrace.spans.forEach(span => {
  700. span.start_timestamp += spanTimeOffset;
  701. span.timestamp += spanTimeOffset;
  702. });
  703. this.isEmbeddedTransactionTimeAdjusted = true;
  704. }
  705. const rootSpan = generateRootSpan(parsedTrace);
  706. const parsedRootSpan = new SpanTreeModel(
  707. rootSpan,
  708. parsedTrace.childSpans,
  709. this.api,
  710. false,
  711. this.traceInfo
  712. );
  713. this.embeddedChildren.push(parsedRootSpan);
  714. this.fetchEmbeddedChildrenState = 'idle';
  715. addTraceBounds(parsedRootSpan.generateTraceBounds());
  716. })
  717. )
  718. .catch(
  719. action('fetchEmbeddedTransactionsError', () => {
  720. this.embeddedChildren = [];
  721. this.fetchEmbeddedChildrenState = 'error_fetching_embedded_transactions';
  722. })
  723. )
  724. );
  725. return Promise.all(promiseArray);
  726. }
  727. toggleNestedSpanGroup = () => {
  728. this.isNestedSpanGroupExpanded = !this.isNestedSpanGroupExpanded;
  729. };
  730. toggleSiblingSpanGroup = (span: SpanType, occurrence?: number) => {
  731. const key = getSiblingGroupKey(span, occurrence);
  732. if (this.expandedSiblingGroups.has(key)) {
  733. this.expandedSiblingGroups.delete(key);
  734. } else {
  735. this.expandedSiblingGroups.add(key);
  736. }
  737. };
  738. generateTraceBounds = (): TraceBound => {
  739. return {
  740. spanId: this.span.span_id,
  741. traceStartTimestamp: this.traceInfo
  742. ? this.traceInfo.startTimestamp
  743. : this.span.start_timestamp,
  744. traceEndTimestamp: this.traceInfo
  745. ? this.traceInfo.endTimestamp
  746. : this.span.timestamp,
  747. };
  748. };
  749. }
  750. export default SpanTreeModel;