spanTreeModel.tsx 26 KB

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