traceView.tsx 11 KB

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