spanTreeModel.tsx 26 KB

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