spanTreeModel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. EnhancedProcessedSpanType,
  8. EnhancedSpan,
  9. FetchEmbeddedChildrenState,
  10. FilterSpans,
  11. OrphanTreeDepth,
  12. RawSpanType,
  13. SpanChildrenLookupType,
  14. SpanType,
  15. TraceBound,
  16. TreeDepthType,
  17. } from './types';
  18. import {
  19. generateRootSpan,
  20. getSpanID,
  21. getSpanOperation,
  22. isEventFromBrowserJavaScriptSDK,
  23. isOrphanSpan,
  24. parseTrace,
  25. SpanBoundsType,
  26. SpanGeneratedBoundsType,
  27. } from './utils';
  28. class SpanTreeModel {
  29. api: Client;
  30. // readonly state
  31. span: Readonly<SpanType>;
  32. children: Array<SpanTreeModel> = [];
  33. isRoot: boolean;
  34. // readable/writable state
  35. fetchEmbeddedChildrenState: FetchEmbeddedChildrenState = 'idle';
  36. showEmbeddedChildren: boolean = false;
  37. embeddedChildren: Array<SpanTreeModel> = [];
  38. showSpanGroup: boolean = false;
  39. constructor(
  40. parentSpan: SpanType,
  41. childSpans: SpanChildrenLookupType,
  42. api: Client,
  43. isRoot: boolean = false
  44. ) {
  45. this.api = api;
  46. this.span = parentSpan;
  47. this.isRoot = isRoot;
  48. const spanID = getSpanID(parentSpan);
  49. const spanChildren: Array<RawSpanType> = childSpans?.[spanID] ?? [];
  50. // Mark descendents as being rendered. This is to address potential recursion issues due to malformed data.
  51. // For example if a span has a span_id that's identical to its parent_span_id.
  52. childSpans = {
  53. ...childSpans,
  54. };
  55. delete childSpans[spanID];
  56. this.children = spanChildren.map(span => {
  57. return new SpanTreeModel(span, childSpans, api);
  58. });
  59. makeObservable(this, {
  60. operationNameCounts: computed.struct,
  61. showEmbeddedChildren: observable,
  62. embeddedChildren: observable,
  63. fetchEmbeddedChildrenState: observable,
  64. toggleEmbeddedChildren: action,
  65. fetchEmbeddedTransactions: action,
  66. showSpanGroup: observable,
  67. toggleSpanGroup: action,
  68. });
  69. }
  70. get operationNameCounts(): Map<string, number> {
  71. const result = new Map<string, number>();
  72. const operationName = this.span.op;
  73. if (typeof operationName === 'string' && operationName.length > 0) {
  74. result.set(operationName, 1);
  75. }
  76. for (const directChild of this.children) {
  77. const operationNameCounts = directChild.operationNameCounts;
  78. for (const [key, count] of operationNameCounts) {
  79. result.set(key, (result.get(key) ?? 0) + count);
  80. }
  81. }
  82. // sort alphabetically using case insensitive comparison
  83. return new Map(
  84. [...result].sort((a, b) =>
  85. String(a[0]).localeCompare(b[0], undefined, {sensitivity: 'base'})
  86. )
  87. );
  88. }
  89. isSpanFilteredOut = (props: {
  90. operationNameFilters: ActiveOperationFilter;
  91. filterSpans: FilterSpans | undefined;
  92. }): boolean => {
  93. const {operationNameFilters, filterSpans} = props;
  94. if (operationNameFilters.type === 'active_filter') {
  95. const operationName = getSpanOperation(this.span);
  96. if (
  97. typeof operationName === 'string' &&
  98. !operationNameFilters.operationNames.has(operationName)
  99. ) {
  100. return true;
  101. }
  102. }
  103. if (!filterSpans) {
  104. return false;
  105. }
  106. return !filterSpans.spanIDs.has(getSpanID(this.span));
  107. };
  108. generateSpanGap(
  109. event: Readonly<EventTransaction>,
  110. previousSiblingEndTimestamp: number | undefined,
  111. treeDepth: number,
  112. continuingTreeDepths: Array<TreeDepthType>
  113. ): EnhancedProcessedSpanType | undefined {
  114. // hide gap spans (i.e. "missing instrumentation" spans) for browser js transactions,
  115. // since they're not useful to indicate
  116. const shouldIncludeGap = !isEventFromBrowserJavaScriptSDK(event);
  117. const isValidGap =
  118. shouldIncludeGap &&
  119. typeof previousSiblingEndTimestamp === 'number' &&
  120. previousSiblingEndTimestamp < this.span.start_timestamp &&
  121. // gap is at least 100 ms
  122. this.span.start_timestamp - previousSiblingEndTimestamp >= 0.1;
  123. if (!isValidGap) {
  124. return undefined;
  125. }
  126. const gapSpan: EnhancedProcessedSpanType = {
  127. type: 'gap',
  128. span: {
  129. type: 'gap',
  130. start_timestamp: previousSiblingEndTimestamp || this.span.start_timestamp,
  131. timestamp: this.span.start_timestamp, // this is essentially end_timestamp
  132. description: t('Missing instrumentation'),
  133. isOrphan: isOrphanSpan(this.span),
  134. },
  135. numOfSpanChildren: 0,
  136. treeDepth,
  137. isLastSibling: false,
  138. continuingTreeDepths,
  139. fetchEmbeddedChildrenState: 'idle',
  140. showEmbeddedChildren: false,
  141. toggleEmbeddedChildren: undefined,
  142. };
  143. return gapSpan;
  144. }
  145. getSpansList = (props: {
  146. operationNameFilters: ActiveOperationFilter;
  147. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  148. treeDepth: number;
  149. isLastSibling: boolean;
  150. continuingTreeDepths: Array<TreeDepthType>;
  151. hiddenSpanGroups: Set<String>;
  152. spanGroups: Set<String>;
  153. filterSpans: FilterSpans | undefined;
  154. previousSiblingEndTimestamp: number | undefined;
  155. event: Readonly<EventTransaction>;
  156. isOnlySibling: boolean;
  157. spanGrouping: EnhancedSpan[] | undefined;
  158. toggleSpanGroup: (() => void) | undefined;
  159. showSpanGroup: boolean;
  160. addTraceBounds: (bounds: TraceBound) => void;
  161. removeTraceBounds: (eventSlug: string) => void;
  162. }): EnhancedProcessedSpanType[] => {
  163. const {
  164. operationNameFilters,
  165. generateBounds,
  166. isLastSibling,
  167. hiddenSpanGroups,
  168. // The set of ancestor span IDs whose sub-tree that the span belongs to
  169. spanGroups,
  170. filterSpans,
  171. previousSiblingEndTimestamp,
  172. event,
  173. isOnlySibling,
  174. spanGrouping,
  175. toggleSpanGroup,
  176. showSpanGroup,
  177. addTraceBounds,
  178. removeTraceBounds,
  179. } = props;
  180. let {treeDepth, continuingTreeDepths} = props;
  181. const parentSpanID = getSpanID(this.span);
  182. const childSpanGroup = new Set(spanGroups);
  183. childSpanGroup.add(parentSpanID);
  184. const descendantsSource = this.showEmbeddedChildren
  185. ? [...this.embeddedChildren, ...this.children]
  186. : this.children;
  187. const lastIndex = descendantsSource.length - 1;
  188. const isNotLastSpanOfGroup =
  189. isOnlySibling && !this.isRoot && descendantsSource.length === 1;
  190. const shouldGroup = isNotLastSpanOfGroup;
  191. const hideSpanTree = hiddenSpanGroups.has(parentSpanID);
  192. const isLastSpanOfGroup =
  193. isOnlySibling && !this.isRoot && (descendantsSource.length !== 1 || hideSpanTree);
  194. const isFirstSpanOfGroup =
  195. shouldGroup &&
  196. (spanGrouping === undefined ||
  197. (Array.isArray(spanGrouping) && spanGrouping.length === 0));
  198. if (
  199. isLastSpanOfGroup &&
  200. Array.isArray(spanGrouping) &&
  201. spanGrouping.length >= 1 &&
  202. !showSpanGroup
  203. ) {
  204. // We always want to indent the last span of the span group chain
  205. treeDepth = treeDepth + 1;
  206. // For a collapsed span group chain to be useful, we prefer span groupings
  207. // that are two or more spans.
  208. // Since there is no concept of "backtracking" when constructing the span tree,
  209. // we will need to reconstruct the tree depth information. This is only neccessary
  210. // when the span group chain is hidden/collapsed.
  211. if (spanGrouping.length === 1) {
  212. const treeDepthEntryFoo = isOrphanSpan(spanGrouping[0].span)
  213. ? ({type: 'orphan', depth: spanGrouping[0].treeDepth} as OrphanTreeDepth)
  214. : spanGrouping[0].treeDepth;
  215. if (!spanGrouping[0].isLastSibling) {
  216. continuingTreeDepths = [...continuingTreeDepths, treeDepthEntryFoo];
  217. }
  218. }
  219. }
  220. // Criteria for propagating information about the span group to the last span of the span group chain
  221. const spanGroupingCriteria =
  222. isLastSpanOfGroup && Array.isArray(spanGrouping) && spanGrouping.length > 1;
  223. const wrappedSpan: EnhancedSpan = {
  224. type: this.isRoot ? 'root_span' : 'span',
  225. span: this.span,
  226. numOfSpanChildren: descendantsSource.length,
  227. treeDepth,
  228. isLastSibling,
  229. continuingTreeDepths,
  230. fetchEmbeddedChildrenState: this.fetchEmbeddedChildrenState,
  231. showEmbeddedChildren: this.showEmbeddedChildren,
  232. toggleEmbeddedChildren: this.toggleEmbeddedChildren({
  233. addTraceBounds,
  234. removeTraceBounds,
  235. }),
  236. toggleSpanGroup:
  237. spanGroupingCriteria && toggleSpanGroup && !showSpanGroup
  238. ? toggleSpanGroup
  239. : isFirstSpanOfGroup && this.showSpanGroup && !hideSpanTree
  240. ? this.toggleSpanGroup
  241. : undefined,
  242. };
  243. if (wrappedSpan.type === 'root_span') {
  244. // @ts-expect-error
  245. delete wrappedSpan.toggleSpanGroup;
  246. }
  247. const treeDepthEntry = isOrphanSpan(this.span)
  248. ? ({type: 'orphan', depth: treeDepth} as OrphanTreeDepth)
  249. : treeDepth;
  250. const shouldHideSpanOfGroup =
  251. shouldGroup &&
  252. !isLastSpanOfGroup &&
  253. ((toggleSpanGroup === undefined && !this.showSpanGroup) ||
  254. (toggleSpanGroup !== undefined && !showSpanGroup));
  255. const descendantContinuingTreeDepths =
  256. isLastSibling || shouldHideSpanOfGroup
  257. ? continuingTreeDepths
  258. : [...continuingTreeDepths, treeDepthEntry];
  259. for (const hiddenSpanGroup of hiddenSpanGroups) {
  260. if (spanGroups.has(hiddenSpanGroup)) {
  261. // If this span is hidden, then all the descendants are hidden as well
  262. return [];
  263. }
  264. }
  265. const {descendants} = (hideSpanTree ? [] : descendantsSource).reduce(
  266. (
  267. acc: {
  268. descendants: EnhancedProcessedSpanType[];
  269. previousSiblingEndTimestamp: number | undefined;
  270. },
  271. span,
  272. index
  273. ) => {
  274. acc.descendants.push(
  275. ...span.getSpansList({
  276. operationNameFilters,
  277. generateBounds,
  278. treeDepth: shouldHideSpanOfGroup ? treeDepth : treeDepth + 1,
  279. isLastSibling: index === lastIndex,
  280. continuingTreeDepths: descendantContinuingTreeDepths,
  281. hiddenSpanGroups,
  282. spanGroups: new Set(childSpanGroup),
  283. filterSpans,
  284. previousSiblingEndTimestamp: acc.previousSiblingEndTimestamp,
  285. event,
  286. isOnlySibling: descendantsSource.length === 1,
  287. spanGrouping: shouldGroup
  288. ? [...(spanGrouping ?? []), wrappedSpan]
  289. : undefined,
  290. toggleSpanGroup: isNotLastSpanOfGroup
  291. ? toggleSpanGroup === undefined
  292. ? this.toggleSpanGroup
  293. : toggleSpanGroup
  294. : undefined,
  295. showSpanGroup: isNotLastSpanOfGroup
  296. ? toggleSpanGroup === undefined
  297. ? this.showSpanGroup
  298. : showSpanGroup
  299. : false,
  300. addTraceBounds,
  301. removeTraceBounds,
  302. })
  303. );
  304. acc.previousSiblingEndTimestamp = span.span.timestamp;
  305. return acc;
  306. },
  307. {
  308. descendants: [],
  309. previousSiblingEndTimestamp: undefined,
  310. }
  311. );
  312. if (this.isSpanFilteredOut(props)) {
  313. return [
  314. {
  315. type: 'filtered_out',
  316. span: this.span,
  317. },
  318. ...descendants,
  319. ];
  320. }
  321. const bounds = generateBounds({
  322. startTimestamp: this.span.start_timestamp,
  323. endTimestamp: this.span.timestamp,
  324. });
  325. const isCurrentSpanOutOfView = !bounds.isSpanVisibleInView;
  326. if (isCurrentSpanOutOfView) {
  327. return [
  328. {
  329. type: 'out_of_view',
  330. span: this.span,
  331. },
  332. ...descendants,
  333. ];
  334. }
  335. if (shouldHideSpanOfGroup) {
  336. return [...descendants];
  337. }
  338. if (
  339. isLastSpanOfGroup &&
  340. Array.isArray(spanGrouping) &&
  341. spanGrouping.length > 1 &&
  342. !showSpanGroup &&
  343. wrappedSpan.type === 'span'
  344. ) {
  345. const spanGroupChain: EnhancedProcessedSpanType = {
  346. type: 'span_group_chain',
  347. span: this.span,
  348. treeDepth: treeDepth - 1,
  349. continuingTreeDepths,
  350. spanGrouping,
  351. showSpanGroup,
  352. toggleSpanGroup: wrappedSpan.toggleSpanGroup,
  353. };
  354. return [
  355. spanGroupChain,
  356. {...wrappedSpan, toggleSpanGroup: undefined},
  357. ...descendants,
  358. ];
  359. }
  360. if (
  361. isFirstSpanOfGroup &&
  362. this.showSpanGroup &&
  363. !hideSpanTree &&
  364. descendants.length <= 1 &&
  365. wrappedSpan.type === 'span'
  366. ) {
  367. // If we know the descendants will be one span or less, we remove the "regroup" feature (therefore hide it)
  368. // by setting toggleSpanGroup to be undefined for the first span of the group chain.
  369. wrappedSpan.toggleSpanGroup = undefined;
  370. }
  371. // Do not autogroup groups that will only have two spans
  372. if (isLastSpanOfGroup && Array.isArray(spanGrouping) && spanGrouping.length === 1) {
  373. if (!showSpanGroup) {
  374. const parentSpan = spanGrouping[0].span;
  375. const parentSpanBounds = generateBounds({
  376. startTimestamp: parentSpan.start_timestamp,
  377. endTimestamp: parentSpan.timestamp,
  378. });
  379. const isParentSpanOutOfView = !parentSpanBounds.isSpanVisibleInView;
  380. if (!isParentSpanOutOfView) {
  381. return [spanGrouping[0], wrappedSpan, ...descendants];
  382. }
  383. }
  384. return [wrappedSpan, ...descendants];
  385. }
  386. const gapSpan = this.generateSpanGap(
  387. event,
  388. previousSiblingEndTimestamp,
  389. treeDepth,
  390. continuingTreeDepths
  391. );
  392. if (gapSpan) {
  393. return [gapSpan, wrappedSpan, ...descendants];
  394. }
  395. return [wrappedSpan, ...descendants];
  396. };
  397. toggleEmbeddedChildren =
  398. ({
  399. addTraceBounds,
  400. removeTraceBounds,
  401. }: {
  402. addTraceBounds: (bounds: TraceBound) => void;
  403. removeTraceBounds: (eventSlug: string) => void;
  404. }) =>
  405. (props: {orgSlug: string; eventSlug: string}) => {
  406. this.showEmbeddedChildren = !this.showEmbeddedChildren;
  407. this.fetchEmbeddedChildrenState = 'idle';
  408. if (!this.showEmbeddedChildren) {
  409. if (this.embeddedChildren.length > 0) {
  410. this.embeddedChildren.forEach(child => {
  411. removeTraceBounds(child.generateTraceBounds().spanId);
  412. });
  413. }
  414. }
  415. if (this.showEmbeddedChildren) {
  416. if (this.embeddedChildren.length === 0) {
  417. return this.fetchEmbeddedTransactions({...props, addTraceBounds});
  418. }
  419. this.embeddedChildren.forEach(child => {
  420. addTraceBounds(child.generateTraceBounds());
  421. });
  422. }
  423. return Promise.resolve(undefined);
  424. };
  425. fetchEmbeddedTransactions({
  426. orgSlug,
  427. eventSlug,
  428. addTraceBounds,
  429. }: {
  430. orgSlug: string;
  431. eventSlug: string;
  432. addTraceBounds: (bounds: TraceBound) => void;
  433. }) {
  434. const url = `/organizations/${orgSlug}/events/${eventSlug}/`;
  435. this.fetchEmbeddedChildrenState = 'loading_embedded_transactions';
  436. return this.api
  437. .requestPromise(url, {
  438. method: 'GET',
  439. query: {},
  440. })
  441. .then(
  442. action('fetchEmbeddedTransactionsSuccess', (event: EventTransaction) => {
  443. if (!event) {
  444. return;
  445. }
  446. const parsedTrace = parseTrace(event);
  447. const rootSpan = generateRootSpan(parsedTrace);
  448. const parsedRootSpan = new SpanTreeModel(
  449. rootSpan,
  450. parsedTrace.childSpans,
  451. this.api,
  452. false
  453. );
  454. this.embeddedChildren = [parsedRootSpan];
  455. this.fetchEmbeddedChildrenState = 'idle';
  456. addTraceBounds(parsedRootSpan.generateTraceBounds());
  457. })
  458. )
  459. .catch(
  460. action('fetchEmbeddedTransactionsError', () => {
  461. this.embeddedChildren = [];
  462. this.fetchEmbeddedChildrenState = 'error_fetching_embedded_transactions';
  463. })
  464. );
  465. }
  466. toggleSpanGroup = () => {
  467. this.showSpanGroup = !this.showSpanGroup;
  468. };
  469. generateTraceBounds = (): TraceBound => {
  470. return {
  471. spanId: this.span.span_id,
  472. traceStartTimestamp: this.span.start_timestamp,
  473. traceEndTimestamp: this.span.timestamp,
  474. };
  475. };
  476. }
  477. export default SpanTreeModel;