content.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import {Component, createRef, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import Alert from 'sentry/components/alert';
  4. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import DiscoverButton from 'sentry/components/discoverButton';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import TimeSince from 'sentry/components/timeSince';
  12. import {t, tct, tn} from 'sentry/locale';
  13. import {Organization} from 'sentry/types';
  14. import {defined} from 'sentry/utils';
  15. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  16. import EventView from 'sentry/utils/discover/eventView';
  17. import {QueryError} from 'sentry/utils/discover/genericDiscoverQuery';
  18. import {getDuration} from 'sentry/utils/formatters';
  19. import {createFuzzySearch, Fuse} from 'sentry/utils/fuzzySearch';
  20. import getDynamicText from 'sentry/utils/getDynamicText';
  21. import {TraceFullDetailed, TraceMeta} from 'sentry/utils/performance/quickTrace/types';
  22. import {filterTrace, reduceTrace} from 'sentry/utils/performance/quickTrace/utils';
  23. import Breadcrumb from 'sentry/views/performance/breadcrumb';
  24. import {MetaData} from 'sentry/views/performance/transactionDetails/styles';
  25. import {TraceDetailHeader, TraceSearchBar, TraceSearchContainer} from './styles';
  26. import TraceNotFound from './traceNotFound';
  27. import TraceView from './traceView';
  28. import {TraceInfo} from './types';
  29. import {getTraceInfo, isRootTransaction} from './utils';
  30. type IndexedFusedTransaction = {
  31. indexed: string[];
  32. transaction: TraceFullDetailed;
  33. };
  34. type Props = Pick<RouteComponentProps<{traceSlug: string}, {}>, 'params' | 'location'> & {
  35. dateSelected: boolean;
  36. error: QueryError | null;
  37. isLoading: boolean;
  38. meta: TraceMeta | null;
  39. organization: Organization;
  40. traceEventView: EventView;
  41. traceSlug: string;
  42. traces: TraceFullDetailed[] | null;
  43. };
  44. type State = {
  45. filteredTransactionIds: Set<string> | undefined;
  46. searchQuery: string | undefined;
  47. };
  48. class TraceDetailsContent extends Component<Props, State> {
  49. state: State = {
  50. searchQuery: undefined,
  51. filteredTransactionIds: undefined,
  52. };
  53. componentDidMount() {
  54. this.initFuse();
  55. }
  56. componentDidUpdate(prevProps: Props) {
  57. if (this.props.traces !== prevProps.traces) {
  58. this.initFuse();
  59. }
  60. }
  61. fuse: Fuse<IndexedFusedTransaction> | null = null;
  62. traceViewRef = createRef<HTMLDivElement>();
  63. virtualScrollbarContainerRef = createRef<HTMLDivElement>();
  64. async initFuse() {
  65. if (defined(this.props.traces) && this.props.traces.length > 0) {
  66. const transformed: IndexedFusedTransaction[] = this.props.traces.flatMap(trace =>
  67. reduceTrace<IndexedFusedTransaction[]>(
  68. trace,
  69. (acc, transaction) => {
  70. const indexed: string[] = [
  71. transaction['transaction.op'],
  72. transaction.transaction,
  73. transaction.project_slug,
  74. ];
  75. acc.push({
  76. transaction,
  77. indexed,
  78. });
  79. return acc;
  80. },
  81. []
  82. )
  83. );
  84. this.fuse = await createFuzzySearch(transformed, {
  85. keys: ['indexed'],
  86. includeMatches: true,
  87. threshold: 0.6,
  88. location: 0,
  89. distance: 100,
  90. maxPatternLength: 32,
  91. });
  92. }
  93. }
  94. renderTraceLoading() {
  95. return <LoadingIndicator />;
  96. }
  97. renderTraceRequiresDateRangeSelection() {
  98. return <LoadingError message={t('Trace view requires a date range selection.')} />;
  99. }
  100. handleTransactionFilter = (searchQuery: string) => {
  101. this.setState({searchQuery: searchQuery || undefined}, this.filterTransactions);
  102. };
  103. filterTransactions = () => {
  104. const {traces} = this.props;
  105. const {filteredTransactionIds, searchQuery} = this.state;
  106. if (!searchQuery || traces === null || traces.length <= 0 || !defined(this.fuse)) {
  107. if (filteredTransactionIds !== undefined) {
  108. this.setState({
  109. filteredTransactionIds: undefined,
  110. });
  111. }
  112. return;
  113. }
  114. const fuseMatches = this.fuse
  115. .search<IndexedFusedTransaction>(searchQuery)
  116. /**
  117. * Sometimes, there can be matches that don't include any
  118. * indices. These matches are often noise, so exclude them.
  119. */
  120. .filter(({matches}) => matches?.length)
  121. .map(({item}) => item.transaction.event_id);
  122. /**
  123. * Fuzzy search on ids result in seemingly random results. So switch to
  124. * doing substring matches on ids to provide more meaningful results.
  125. */
  126. const idMatches = traces
  127. .flatMap(trace =>
  128. filterTrace(
  129. trace,
  130. ({event_id, span_id}) =>
  131. event_id.includes(searchQuery) || span_id.includes(searchQuery)
  132. )
  133. )
  134. .map(transaction => transaction.event_id);
  135. this.setState({
  136. filteredTransactionIds: new Set([...fuseMatches, ...idMatches]),
  137. });
  138. };
  139. renderSearchBar() {
  140. return (
  141. <TraceSearchContainer>
  142. <TraceSearchBar
  143. defaultQuery=""
  144. query={this.state.searchQuery || ''}
  145. placeholder={t('Search for transactions')}
  146. onSearch={this.handleTransactionFilter}
  147. />
  148. </TraceSearchContainer>
  149. );
  150. }
  151. renderTraceHeader(traceInfo: TraceInfo) {
  152. const {meta} = this.props;
  153. return (
  154. <TraceDetailHeader>
  155. <GuideAnchor target="trace_view_guide_breakdown">
  156. <MetaData
  157. headingText={t('Event Breakdown')}
  158. tooltipText={t(
  159. 'The number of transactions and errors there are in this trace.'
  160. )}
  161. bodyText={tct('[transactions] | [errors]', {
  162. transactions: tn(
  163. '%s Transaction',
  164. '%s Transactions',
  165. meta?.transactions ?? traceInfo.transactions.size
  166. ),
  167. errors: tn('%s Error', '%s Errors', meta?.errors ?? traceInfo.errors.size),
  168. })}
  169. subtext={tn(
  170. 'Across %s project',
  171. 'Across %s projects',
  172. meta?.projects ?? traceInfo.projects.size
  173. )}
  174. />
  175. </GuideAnchor>
  176. <MetaData
  177. headingText={t('Total Duration')}
  178. tooltipText={t('The time elapsed between the start and end of this trace.')}
  179. bodyText={getDuration(
  180. traceInfo.endTimestamp - traceInfo.startTimestamp,
  181. 2,
  182. true
  183. )}
  184. subtext={getDynamicText({
  185. value: <TimeSince date={(traceInfo.endTimestamp || 0) * 1000} />,
  186. fixed: '5 days ago',
  187. })}
  188. />
  189. </TraceDetailHeader>
  190. );
  191. }
  192. renderTraceWarnings() {
  193. const {traces} = this.props;
  194. const {roots, orphans} = (traces ?? []).reduce(
  195. (counts, trace) => {
  196. if (isRootTransaction(trace)) {
  197. counts.roots++;
  198. } else {
  199. counts.orphans++;
  200. }
  201. return counts;
  202. },
  203. {roots: 0, orphans: 0}
  204. );
  205. let warning: React.ReactNode = null;
  206. if (roots === 0 && orphans > 0) {
  207. warning = (
  208. <Alert type="info" showIcon>
  209. <ExternalLink href="https://docs.sentry.io/product/performance/trace-view/#orphan-traces-and-broken-subtraces">
  210. {t(
  211. 'A root transaction is missing. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  212. )}
  213. </ExternalLink>
  214. </Alert>
  215. );
  216. } else if (roots === 1 && orphans > 0) {
  217. warning = (
  218. <Alert type="info" showIcon>
  219. <ExternalLink href="https://docs.sentry.io/product/performance/trace-view/#orphan-traces-and-broken-subtraces">
  220. {t(
  221. 'This trace has broken subtraces. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  222. )}
  223. </ExternalLink>
  224. </Alert>
  225. );
  226. } else if (roots > 1) {
  227. warning = (
  228. <Alert type="info" showIcon>
  229. <ExternalLink href="https://docs.sentry.io/product/performance/trace-view/#multiple-roots">
  230. {t('Multiple root transactions have been found with this trace ID.')}
  231. </ExternalLink>
  232. </Alert>
  233. );
  234. }
  235. return warning;
  236. }
  237. renderContent() {
  238. const {
  239. dateSelected,
  240. isLoading,
  241. error,
  242. organization,
  243. location,
  244. traceEventView,
  245. traceSlug,
  246. traces,
  247. meta,
  248. } = this.props;
  249. if (!dateSelected) {
  250. return this.renderTraceRequiresDateRangeSelection();
  251. }
  252. if (isLoading) {
  253. return this.renderTraceLoading();
  254. }
  255. if (error !== null || traces === null || traces.length <= 0) {
  256. return (
  257. <TraceNotFound
  258. meta={meta}
  259. traceEventView={traceEventView}
  260. traceSlug={traceSlug}
  261. location={location}
  262. organization={organization}
  263. />
  264. );
  265. }
  266. const traceInfo = getTraceInfo(traces);
  267. return (
  268. <Fragment>
  269. {this.renderTraceWarnings()}
  270. {this.renderTraceHeader(traceInfo)}
  271. {this.renderSearchBar()}
  272. <TraceView
  273. filteredTransactionIds={this.state.filteredTransactionIds}
  274. traceInfo={traceInfo}
  275. location={location}
  276. organization={organization}
  277. traceEventView={traceEventView}
  278. traceSlug={traceSlug}
  279. traces={traces}
  280. meta={meta}
  281. />
  282. </Fragment>
  283. );
  284. }
  285. render() {
  286. const {organization, location, traceEventView, traceSlug} = this.props;
  287. return (
  288. <Fragment>
  289. <Layout.Header>
  290. <Layout.HeaderContent>
  291. <Breadcrumb
  292. organization={organization}
  293. location={location}
  294. traceSlug={traceSlug}
  295. />
  296. <Layout.Title data-test-id="trace-header">
  297. {t('Trace ID: %s', traceSlug)}
  298. </Layout.Title>
  299. </Layout.HeaderContent>
  300. <Layout.HeaderActions>
  301. <ButtonBar gap={1}>
  302. <DiscoverButton
  303. to={traceEventView.getResultsViewUrlTarget(organization.slug)}
  304. onClick={() => {
  305. trackAdvancedAnalyticsEvent(
  306. 'performance_views.trace_view.open_in_discover',
  307. {
  308. organization,
  309. }
  310. );
  311. }}
  312. >
  313. {t('Open in Discover')}
  314. </DiscoverButton>
  315. </ButtonBar>
  316. </Layout.HeaderActions>
  317. </Layout.Header>
  318. <Layout.Body>
  319. <Layout.Main fullWidth>{this.renderContent()}</Layout.Main>
  320. </Layout.Body>
  321. </Fragment>
  322. );
  323. }
  324. }
  325. export default TraceDetailsContent;