traceView.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import React, {createRef, useEffect} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import * as Sentry from '@sentry/react';
  4. import * as AnchorLinkManager from 'sentry/components/events/interfaces/spans/anchorLinkManager';
  5. import * as DividerHandlerManager from 'sentry/components/events/interfaces/spans/dividerHandlerManager';
  6. import * as ScrollbarManager from 'sentry/components/events/interfaces/spans/scrollbarManager';
  7. import {MessageRow} from 'sentry/components/performance/waterfall/messageRow';
  8. import {
  9. DividerSpacer,
  10. ScrollbarContainer,
  11. VirtualScrollbar,
  12. VirtualScrollbarGrip,
  13. } from 'sentry/components/performance/waterfall/miniHeader';
  14. import {pickBarColor, toPercent} from 'sentry/components/performance/waterfall/utils';
  15. import {tct} from 'sentry/locale';
  16. import {Organization} from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import EventView from 'sentry/utils/discover/eventView';
  19. import {TraceFullDetailed, TraceMeta} from 'sentry/utils/performance/quickTrace/types';
  20. import {
  21. TraceDetailBody,
  22. TracePanel,
  23. TraceViewContainer,
  24. TraceViewHeaderContainer,
  25. } from 'sentry/views/performance/traceDetails/styles';
  26. import TransactionGroup from 'sentry/views/performance/traceDetails/transactionGroup';
  27. import {TraceInfo, TreeDepth} from 'sentry/views/performance/traceDetails/types';
  28. import {
  29. getTraceInfo,
  30. isRootTransaction,
  31. } from 'sentry/views/performance/traceDetails/utils';
  32. import LimitExceededMessage from './limitExceededMessage';
  33. import TraceNotFound from './traceNotFound';
  34. type AccType = {
  35. lastIndex: number;
  36. numberOfHiddenTransactionsAbove: number;
  37. renderedChildren: React.ReactNode[];
  38. };
  39. type Props = Pick<RouteComponentProps<{}, {}>, 'location'> & {
  40. meta: TraceMeta | null;
  41. organization: Organization;
  42. traceEventView: EventView;
  43. traceSlug: string;
  44. traces: TraceFullDetailed[] | null;
  45. filteredTransactionIds?: Set<string>;
  46. traceInfo?: TraceInfo;
  47. };
  48. function TraceHiddenMessage({
  49. isVisible,
  50. numberOfHiddenTransactionsAbove,
  51. }: {
  52. isVisible: boolean;
  53. numberOfHiddenTransactionsAbove: number;
  54. }) {
  55. if (!isVisible || numberOfHiddenTransactionsAbove < 1) {
  56. return null;
  57. }
  58. return (
  59. <MessageRow>
  60. <span key="trace-info-message">
  61. {numberOfHiddenTransactionsAbove === 1
  62. ? tct('[numOfTransaction] hidden transaction', {
  63. numOfTransaction: <strong>{numberOfHiddenTransactionsAbove}</strong>,
  64. })
  65. : tct('[numOfTransaction] hidden transactions', {
  66. numOfTransaction: <strong>{numberOfHiddenTransactionsAbove}</strong>,
  67. })}
  68. </span>
  69. </MessageRow>
  70. );
  71. }
  72. function isTransactionVisible(
  73. transaction: TraceFullDetailed,
  74. filteredTransactionIds?: Set<string>
  75. ): boolean {
  76. return filteredTransactionIds ? filteredTransactionIds.has(transaction.event_id) : true;
  77. }
  78. export default function TraceView({
  79. location,
  80. meta,
  81. organization,
  82. traces,
  83. traceSlug,
  84. traceEventView,
  85. filteredTransactionIds,
  86. ...props
  87. }: Props) {
  88. const sentryTransaction = Sentry.getCurrentHub().getScope()?.getTransaction();
  89. const sentrySpan = sentryTransaction?.startChild({
  90. op: 'trace.render',
  91. description: 'trace-view-content',
  92. });
  93. useEffect(() => {
  94. trackAdvancedAnalyticsEvent('performance_views.trace_view.view', {
  95. organization,
  96. });
  97. }, [organization]);
  98. function renderTransaction(
  99. transaction: TraceFullDetailed,
  100. {
  101. continuingDepths,
  102. isOrphan,
  103. isLast,
  104. index,
  105. numberOfHiddenTransactionsAbove,
  106. traceInfo,
  107. hasGuideAnchor,
  108. }: {
  109. continuingDepths: TreeDepth[];
  110. hasGuideAnchor: boolean;
  111. index: number;
  112. isLast: boolean;
  113. isOrphan: boolean;
  114. numberOfHiddenTransactionsAbove: number;
  115. traceInfo: TraceInfo;
  116. }
  117. ) {
  118. const {children, event_id: eventId} = transaction;
  119. // Add 1 to the generation to make room for the "root trace"
  120. const generation = transaction.generation + 1;
  121. const isVisible = isTransactionVisible(transaction, filteredTransactionIds);
  122. const accumulated: AccType = children.reduce(
  123. (acc: AccType, child: TraceFullDetailed, idx: number) => {
  124. const isLastChild = idx === children.length - 1;
  125. const hasChildren = child.children.length > 0;
  126. const result = renderTransaction(child, {
  127. continuingDepths:
  128. !isLastChild && hasChildren
  129. ? [...continuingDepths, {depth: generation, isOrphanDepth: isOrphan}]
  130. : continuingDepths,
  131. isOrphan,
  132. isLast: isLastChild,
  133. index: acc.lastIndex + 1,
  134. numberOfHiddenTransactionsAbove: acc.numberOfHiddenTransactionsAbove,
  135. traceInfo,
  136. hasGuideAnchor: false,
  137. });
  138. acc.lastIndex = result.lastIndex;
  139. acc.numberOfHiddenTransactionsAbove = result.numberOfHiddenTransactionsAbove;
  140. acc.renderedChildren.push(result.transactionGroup);
  141. return acc;
  142. },
  143. {
  144. renderedChildren: [],
  145. lastIndex: index,
  146. numberOfHiddenTransactionsAbove: isVisible
  147. ? 0
  148. : numberOfHiddenTransactionsAbove + 1,
  149. }
  150. );
  151. return {
  152. transactionGroup: (
  153. <React.Fragment key={eventId}>
  154. <TraceHiddenMessage
  155. isVisible={isVisible}
  156. numberOfHiddenTransactionsAbove={numberOfHiddenTransactionsAbove}
  157. />
  158. <TransactionGroup
  159. location={location}
  160. organization={organization}
  161. traceInfo={traceInfo}
  162. transaction={{
  163. ...transaction,
  164. generation,
  165. }}
  166. continuingDepths={continuingDepths}
  167. isOrphan={isOrphan}
  168. isLast={isLast}
  169. index={index}
  170. isVisible={isVisible}
  171. hasGuideAnchor={hasGuideAnchor}
  172. renderedChildren={accumulated.renderedChildren}
  173. barColor={pickBarColor(transaction['transaction.op'])}
  174. />
  175. </React.Fragment>
  176. ),
  177. lastIndex: accumulated.lastIndex,
  178. numberOfHiddenTransactionsAbove: accumulated.numberOfHiddenTransactionsAbove,
  179. };
  180. }
  181. const traceViewRef = createRef<HTMLDivElement>();
  182. const virtualScrollbarContainerRef = createRef<HTMLDivElement>();
  183. if (traces === null || traces.length <= 0) {
  184. return (
  185. <TraceNotFound
  186. meta={meta}
  187. traceEventView={traceEventView}
  188. traceSlug={traceSlug}
  189. location={location}
  190. organization={organization}
  191. />
  192. );
  193. }
  194. const traceInfo = props.traceInfo || getTraceInfo(traces);
  195. const accumulator: {
  196. index: number;
  197. numberOfHiddenTransactionsAbove: number;
  198. traceInfo: TraceInfo;
  199. transactionGroups: React.ReactNode[];
  200. } = {
  201. index: 1,
  202. numberOfHiddenTransactionsAbove: 0,
  203. traceInfo,
  204. transactionGroups: [],
  205. };
  206. const {transactionGroups, numberOfHiddenTransactionsAbove} = traces.reduce(
  207. (acc, trace, index) => {
  208. const isLastTransaction = index === traces.length - 1;
  209. const hasChildren = trace.children.length > 0;
  210. const isNextChildOrphaned =
  211. !isLastTransaction && traces[index + 1].parent_span_id !== null;
  212. const result = renderTransaction(trace, {
  213. ...acc,
  214. // if the root of a subtrace has a parent_span_id, then it must be an orphan
  215. isOrphan: !isRootTransaction(trace),
  216. isLast: isLastTransaction,
  217. continuingDepths:
  218. !isLastTransaction && hasChildren
  219. ? [{depth: 0, isOrphanDepth: isNextChildOrphaned}]
  220. : [],
  221. hasGuideAnchor: index === 0,
  222. });
  223. acc.index = result.lastIndex + 1;
  224. acc.numberOfHiddenTransactionsAbove = result.numberOfHiddenTransactionsAbove;
  225. acc.transactionGroups.push(result.transactionGroup);
  226. return acc;
  227. },
  228. accumulator
  229. );
  230. const traceView = (
  231. <TraceDetailBody>
  232. <DividerHandlerManager.Provider interactiveLayerRef={traceViewRef}>
  233. <DividerHandlerManager.Consumer>
  234. {({dividerPosition}) => (
  235. <ScrollbarManager.Provider
  236. dividerPosition={dividerPosition}
  237. interactiveLayerRef={virtualScrollbarContainerRef}
  238. >
  239. <TracePanel>
  240. <TraceViewHeaderContainer>
  241. <ScrollbarManager.Consumer>
  242. {({virtualScrollbarRef, scrollBarAreaRef, onDragStart, onScroll}) => {
  243. return (
  244. <ScrollbarContainer
  245. ref={virtualScrollbarContainerRef}
  246. style={{
  247. // the width of this component is shrunk to compensate for half of the width of the divider line
  248. width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
  249. }}
  250. onScroll={onScroll}
  251. >
  252. <div
  253. style={{
  254. width: 0,
  255. height: '1px',
  256. }}
  257. ref={scrollBarAreaRef}
  258. />
  259. <VirtualScrollbar
  260. data-type="virtual-scrollbar"
  261. ref={virtualScrollbarRef}
  262. onMouseDown={onDragStart}
  263. >
  264. <VirtualScrollbarGrip />
  265. </VirtualScrollbar>
  266. </ScrollbarContainer>
  267. );
  268. }}
  269. </ScrollbarManager.Consumer>
  270. <DividerSpacer />
  271. </TraceViewHeaderContainer>
  272. <TraceViewContainer ref={traceViewRef}>
  273. <AnchorLinkManager.Provider>
  274. <TransactionGroup
  275. location={location}
  276. organization={organization}
  277. traceInfo={traceInfo}
  278. transaction={{
  279. traceSlug,
  280. generation: 0,
  281. 'transaction.duration':
  282. traceInfo.endTimestamp - traceInfo.startTimestamp,
  283. children: traces,
  284. start_timestamp: traceInfo.startTimestamp,
  285. timestamp: traceInfo.endTimestamp,
  286. }}
  287. continuingDepths={[]}
  288. isOrphan={false}
  289. isLast={false}
  290. index={0}
  291. isVisible
  292. hasGuideAnchor={false}
  293. renderedChildren={transactionGroups}
  294. barColor={pickBarColor('')}
  295. />
  296. </AnchorLinkManager.Provider>
  297. <TraceHiddenMessage
  298. isVisible
  299. numberOfHiddenTransactionsAbove={numberOfHiddenTransactionsAbove}
  300. />
  301. <LimitExceededMessage
  302. traceInfo={traceInfo}
  303. organization={organization}
  304. traceEventView={traceEventView}
  305. meta={meta}
  306. />
  307. </TraceViewContainer>
  308. </TracePanel>
  309. </ScrollbarManager.Provider>
  310. )}
  311. </DividerHandlerManager.Consumer>
  312. </DividerHandlerManager.Provider>
  313. </TraceDetailBody>
  314. );
  315. sentrySpan?.finish();
  316. return traceView;
  317. }