traceView.tsx 12 KB

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