spanTreeModel.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. isEventFromBrowserJavaScriptSDK,
  25. isOrphanSpan,
  26. parseTrace,
  27. SpanBoundsType,
  28. SpanGeneratedBoundsType,
  29. } from './utils';
  30. export const MIN_SIBLING_GROUP_SIZE = 5;
  31. class SpanTreeModel {
  32. api: Client;
  33. // readonly state
  34. span: Readonly<SpanType>;
  35. children: Array<SpanTreeModel> = [];
  36. isRoot: boolean;
  37. // readable/writable state
  38. fetchEmbeddedChildrenState: FetchEmbeddedChildrenState = 'idle';
  39. showEmbeddedChildren: boolean = false;
  40. embeddedChildren: Array<SpanTreeModel> = [];
  41. isEmbeddedTransactionTimeAdjusted: boolean = false;
  42. // This controls if a chain of nested spans that are the only sibling to be visually grouped together or not.
  43. // On initial render, they're visually grouped together.
  44. isNestedSpanGroupExpanded: boolean = false;
  45. // Entries in this set will follow the format 'op.description'.
  46. // An entry in this set indicates that all siblings with the op and description should be left ungrouped
  47. expandedSiblingGroups: Set<string> = new Set();
  48. constructor(
  49. parentSpan: SpanType,
  50. childSpans: SpanChildrenLookupType,
  51. api: Client,
  52. isRoot: boolean = false
  53. ) {
  54. this.api = api;
  55. this.span = parentSpan;
  56. this.isRoot = isRoot;
  57. const spanID = getSpanID(parentSpan);
  58. const spanChildren: Array<RawSpanType> = childSpans?.[spanID] ?? [];
  59. // Mark descendents as being rendered. This is to address potential recursion issues due to malformed data.
  60. // For example if a span has a span_id that's identical to its parent_span_id.
  61. childSpans = {
  62. ...childSpans,
  63. };
  64. delete childSpans[spanID];
  65. this.children = spanChildren.map(span => {
  66. return new SpanTreeModel(span, childSpans, api);
  67. });
  68. makeObservable(this, {
  69. operationNameCounts: computed.struct,
  70. showEmbeddedChildren: observable,
  71. embeddedChildren: observable,
  72. fetchEmbeddedChildrenState: observable,
  73. toggleEmbeddedChildren: action,
  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.toggleEmbeddedChildren({
  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.toggleEmbeddedChildren({
  411. addTraceBounds,
  412. removeTraceBounds,
  413. }),
  414. toggleNestedSpanGroup: undefined,
  415. toggleSiblingSpanGroup:
  416. index === 0 ? this.toggleSiblingSpanGroup : undefined,
  417. isEmbeddedTransactionTimeAdjusted:
  418. spanModel.isEmbeddedTransactionTimeAdjusted,
  419. };
  420. acc.previousSiblingEndTimestamp = spanModel.span.timestamp;
  421. acc.descendants.push(enhancedSibling);
  422. }
  423. });
  424. return acc;
  425. }
  426. // Since we are not recursively traversing elements in this group, need to check
  427. // if the spans are filtered or out of bounds here
  428. if (this.isSpanFilteredOut(props, group[0])) {
  429. group.forEach(spanModel => {
  430. acc.descendants.push({
  431. type: 'filtered_out',
  432. span: spanModel.span,
  433. });
  434. });
  435. return acc;
  436. }
  437. // TODO: Check within the group if any of the focusedSpanIDs are present
  438. const bounds = generateBounds({
  439. startTimestamp: group[0].span.start_timestamp,
  440. endTimestamp: group[group.length - 1].span.timestamp,
  441. });
  442. if (!bounds.isSpanVisibleInView) {
  443. group.forEach(spanModel =>
  444. acc.descendants.push({
  445. type: 'out_of_view',
  446. span: spanModel.span,
  447. })
  448. );
  449. return acc;
  450. }
  451. // Since the group is not expanded, return a singular grouped span bar
  452. const wrappedSiblings: EnhancedSpan[] = group.map((spanModel, index) => {
  453. const enhancedSibling: EnhancedSpan = {
  454. type: 'span',
  455. span: spanModel.span,
  456. numOfSpanChildren: 0,
  457. treeDepth: treeDepth + 1,
  458. isLastSibling:
  459. index === group.length - 1 && groupIndex === groupedDescendants.length - 1,
  460. isFirstSiblingOfGroup: index === 0,
  461. groupOccurrence: occurrence,
  462. continuingTreeDepths: descendantContinuingTreeDepths,
  463. fetchEmbeddedChildrenState: spanModel.fetchEmbeddedChildrenState,
  464. showEmbeddedChildren: spanModel.showEmbeddedChildren,
  465. toggleEmbeddedChildren: spanModel.toggleEmbeddedChildren({
  466. addTraceBounds,
  467. removeTraceBounds,
  468. }),
  469. toggleNestedSpanGroup: undefined,
  470. toggleSiblingSpanGroup: index === 0 ? this.toggleSiblingSpanGroup : undefined,
  471. isEmbeddedTransactionTimeAdjusted:
  472. spanModel.isEmbeddedTransactionTimeAdjusted,
  473. };
  474. return enhancedSibling;
  475. });
  476. const groupedSiblingsSpan: EnhancedProcessedSpanType = {
  477. type: 'span_group_siblings',
  478. span: this.span,
  479. treeDepth: treeDepth + 1,
  480. continuingTreeDepths: descendantContinuingTreeDepths,
  481. spanSiblingGrouping: wrappedSiblings,
  482. isLastSibling: groupIndex === groupedDescendants.length - 1,
  483. occurrence: occurrence ?? 0,
  484. toggleSiblingSpanGroup: this.toggleSiblingSpanGroup,
  485. };
  486. acc.previousSiblingEndTimestamp =
  487. wrappedSiblings[wrappedSiblings.length - 1].span.timestamp;
  488. acc.descendants.push(groupedSiblingsSpan);
  489. return acc;
  490. },
  491. {
  492. descendants: [],
  493. previousSiblingEndTimestamp: undefined,
  494. }
  495. ).descendants;
  496. if (
  497. this.isSpanFilteredOut(props, this) ||
  498. (focusedSpanIds && !focusedSpanIds.has(this.span.span_id))
  499. ) {
  500. return [
  501. {
  502. type: 'filtered_out',
  503. span: this.span,
  504. },
  505. ...descendants,
  506. ];
  507. }
  508. const bounds = generateBounds({
  509. startTimestamp: this.span.start_timestamp,
  510. endTimestamp: this.span.timestamp,
  511. });
  512. const isCurrentSpanOutOfView = !bounds.isSpanVisibleInView;
  513. if (isCurrentSpanOutOfView) {
  514. return [
  515. {
  516. type: 'out_of_view',
  517. span: this.span,
  518. },
  519. ...descendants,
  520. ];
  521. }
  522. if (shouldHideSpanOfGroup) {
  523. return [...descendants];
  524. }
  525. if (
  526. isLastSpanOfGroup &&
  527. Array.isArray(spanNestedGrouping) &&
  528. spanNestedGrouping.length > 1 &&
  529. !isNestedSpanGroupExpanded &&
  530. wrappedSpan.type === 'span'
  531. ) {
  532. const spanGroupChain: EnhancedProcessedSpanType = {
  533. type: 'span_group_chain',
  534. span: this.span,
  535. treeDepth: treeDepth - 1,
  536. continuingTreeDepths,
  537. spanNestedGrouping,
  538. isNestedSpanGroupExpanded,
  539. toggleNestedSpanGroup: wrappedSpan.toggleNestedSpanGroup,
  540. toggleSiblingSpanGroup: undefined,
  541. };
  542. return [
  543. spanGroupChain,
  544. {...wrappedSpan, toggleNestedSpanGroup: undefined},
  545. ...descendants,
  546. ];
  547. }
  548. if (
  549. isFirstSpanOfGroup &&
  550. this.isNestedSpanGroupExpanded &&
  551. !hideSpanTree &&
  552. descendants.length <= 1 &&
  553. wrappedSpan.type === 'span'
  554. ) {
  555. // If we know the descendants will be one span or less, we remove the "regroup" feature (therefore hide it)
  556. // by setting toggleNestedSpanGroup to be undefined for the first span of the group chain.
  557. wrappedSpan.toggleNestedSpanGroup = undefined;
  558. }
  559. // Do not autogroup groups that will only have two spans
  560. if (
  561. isLastSpanOfGroup &&
  562. Array.isArray(spanNestedGrouping) &&
  563. spanNestedGrouping.length === 1
  564. ) {
  565. if (!isNestedSpanGroupExpanded) {
  566. const parentSpan = spanNestedGrouping[0].span;
  567. const parentSpanBounds = generateBounds({
  568. startTimestamp: parentSpan.start_timestamp,
  569. endTimestamp: parentSpan.timestamp,
  570. });
  571. const isParentSpanOutOfView = !parentSpanBounds.isSpanVisibleInView;
  572. if (!isParentSpanOutOfView) {
  573. return [spanNestedGrouping[0], wrappedSpan, ...descendants];
  574. }
  575. }
  576. return [wrappedSpan, ...descendants];
  577. }
  578. const gapSpan = this.generateSpanGap(
  579. event,
  580. previousSiblingEndTimestamp,
  581. treeDepth,
  582. continuingTreeDepths
  583. );
  584. if (gapSpan) {
  585. return [gapSpan, wrappedSpan, ...descendants];
  586. }
  587. return [wrappedSpan, ...descendants];
  588. };
  589. toggleEmbeddedChildren =
  590. ({
  591. addTraceBounds,
  592. removeTraceBounds,
  593. }: {
  594. addTraceBounds: (bounds: TraceBound) => void;
  595. removeTraceBounds: (eventSlug: string) => void;
  596. }) =>
  597. (props: {eventSlug: string; orgSlug: string}) => {
  598. this.showEmbeddedChildren = !this.showEmbeddedChildren;
  599. this.fetchEmbeddedChildrenState = 'idle';
  600. if (!this.showEmbeddedChildren) {
  601. if (this.embeddedChildren.length > 0) {
  602. this.embeddedChildren.forEach(child => {
  603. removeTraceBounds(child.generateTraceBounds().spanId);
  604. });
  605. }
  606. }
  607. if (this.showEmbeddedChildren) {
  608. if (this.embeddedChildren.length === 0) {
  609. return this.fetchEmbeddedTransactions({...props, addTraceBounds});
  610. }
  611. this.embeddedChildren.forEach(child => {
  612. addTraceBounds(child.generateTraceBounds());
  613. });
  614. }
  615. return Promise.resolve(undefined);
  616. };
  617. fetchEmbeddedTransactions({
  618. orgSlug,
  619. eventSlug,
  620. addTraceBounds,
  621. }: {
  622. addTraceBounds: (bounds: TraceBound) => void;
  623. eventSlug: string;
  624. orgSlug: string;
  625. }) {
  626. const url = `/organizations/${orgSlug}/events/${eventSlug}/`;
  627. this.fetchEmbeddedChildrenState = 'loading_embedded_transactions';
  628. return this.api
  629. .requestPromise(url, {
  630. method: 'GET',
  631. query: {},
  632. })
  633. .then(
  634. action('fetchEmbeddedTransactionsSuccess', (event: EventTransaction) => {
  635. if (!event) {
  636. return;
  637. }
  638. const parsedTrace = parseTrace(event);
  639. // We need to adjust the timestamps for this embedded transaction only if it is not within the bounds of its parent span
  640. if (
  641. parsedTrace.traceStartTimestamp < this.span.start_timestamp ||
  642. parsedTrace.traceEndTimestamp > this.span.timestamp
  643. ) {
  644. const startTimeDelta =
  645. this.span.start_timestamp - parsedTrace.traceStartTimestamp;
  646. parsedTrace.traceStartTimestamp += startTimeDelta;
  647. parsedTrace.traceEndTimestamp += startTimeDelta;
  648. parsedTrace.spans.forEach(span => {
  649. span.start_timestamp += startTimeDelta;
  650. span.timestamp += startTimeDelta;
  651. });
  652. this.isEmbeddedTransactionTimeAdjusted = true;
  653. }
  654. const rootSpan = generateRootSpan(parsedTrace);
  655. const parsedRootSpan = new SpanTreeModel(
  656. rootSpan,
  657. parsedTrace.childSpans,
  658. this.api,
  659. false
  660. );
  661. this.embeddedChildren = [parsedRootSpan];
  662. this.fetchEmbeddedChildrenState = 'idle';
  663. addTraceBounds(parsedRootSpan.generateTraceBounds());
  664. })
  665. )
  666. .catch(
  667. action('fetchEmbeddedTransactionsError', () => {
  668. this.embeddedChildren = [];
  669. this.fetchEmbeddedChildrenState = 'error_fetching_embedded_transactions';
  670. })
  671. );
  672. }
  673. toggleNestedSpanGroup = () => {
  674. this.isNestedSpanGroupExpanded = !this.isNestedSpanGroupExpanded;
  675. };
  676. toggleSiblingSpanGroup = (span: SpanType, occurrence?: number) => {
  677. const key = getSiblingGroupKey(span, occurrence);
  678. if (this.expandedSiblingGroups.has(key)) {
  679. this.expandedSiblingGroups.delete(key);
  680. } else {
  681. this.expandedSiblingGroups.add(key);
  682. }
  683. };
  684. generateTraceBounds = (): TraceBound => {
  685. return {
  686. spanId: this.span.span_id,
  687. traceStartTimestamp: this.span.start_timestamp,
  688. traceEndTimestamp: this.span.timestamp,
  689. };
  690. };
  691. }
  692. export default SpanTreeModel;