databaseSpanSummaryPage.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  9. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
  14. import {decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  19. import {DurationChart} from 'sentry/views/performance/database/durationChart';
  20. import {isAValidSort} from 'sentry/views/performance/database/queriesTable';
  21. import {QueryTransactionsTable} from 'sentry/views/performance/database/queryTransactionsTable';
  22. import {ThroughputChart} from 'sentry/views/performance/database/throughputChart';
  23. import {useSelectedDurationAggregate} from 'sentry/views/performance/database/useSelectedDurationAggregate';
  24. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  25. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  26. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  27. import {useSynchronizeCharts} from 'sentry/views/starfish/components/chart';
  28. import {DatabaseSpanDescription} from 'sentry/views/starfish/components/spanDescription';
  29. import {getTimeSpentExplanation} from 'sentry/views/starfish/components/tableCells/timeSpentCell';
  30. import {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  31. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useDiscoverSeries';
  32. import type {SpanMetricsQueryFilters} from 'sentry/views/starfish/types';
  33. import {SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  34. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  35. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  36. import {SampleList} from 'sentry/views/starfish/views/spanSummaryPage/sampleList';
  37. type Query = {
  38. transaction: string;
  39. transactionMethod: string;
  40. [QueryParameterNames.TRANSACTIONS_SORT]: string;
  41. aggregate?: string;
  42. };
  43. type Props = RouteComponentProps<Query, {groupId: string}>;
  44. export function DatabaseSpanSummaryPage({params}: Props) {
  45. const organization = useOrganization();
  46. const location = useLocation<Query>();
  47. const [selectedAggregate] = useSelectedDurationAggregate();
  48. const {groupId} = params;
  49. const {transaction, transactionMethod} = location.query;
  50. const filters: SpanMetricsQueryFilters = {
  51. 'span.group': groupId,
  52. };
  53. const cursor = decodeScalar(location.query?.[QueryParameterNames.TRANSACTIONS_CURSOR]);
  54. // TODO: Fetch sort information using `useLocationQuery`
  55. const sortField = decodeScalar(location.query?.[QueryParameterNames.TRANSACTIONS_SORT]);
  56. const sort = decodeSorts(sortField).filter(isAValidSort).at(0) ?? DEFAULT_SORT;
  57. const {data, isLoading: areSpanMetricsLoading} = useSpanMetrics(
  58. {
  59. search: MutableSearch.fromQueryObject(filters),
  60. fields: [
  61. SpanMetricsField.SPAN_OP,
  62. SpanMetricsField.SPAN_DESCRIPTION,
  63. SpanMetricsField.SPAN_ACTION,
  64. SpanMetricsField.SPAN_DOMAIN,
  65. 'count()',
  66. `${SpanFunction.SPM}()`,
  67. `sum(${SpanMetricsField.SPAN_SELF_TIME})`,
  68. `avg(${SpanMetricsField.SPAN_SELF_TIME})`,
  69. `${SpanFunction.TIME_SPENT_PERCENTAGE}()`,
  70. `${SpanFunction.HTTP_ERROR_COUNT}()`,
  71. ],
  72. enabled: Boolean(groupId),
  73. },
  74. 'api.starfish.span-summary-page-metrics'
  75. );
  76. const spanMetrics = data[0] ?? {};
  77. const {
  78. isLoading: isTransactionsListLoading,
  79. data: transactionsList,
  80. meta: transactionsListMeta,
  81. error: transactionsListError,
  82. pageLinks: transactionsListPageLinks,
  83. } = useSpanMetrics(
  84. {
  85. search: MutableSearch.fromQueryObject(filters),
  86. fields: [
  87. 'transaction',
  88. 'transaction.method',
  89. 'spm()',
  90. `sum(${SpanMetricsField.SPAN_SELF_TIME})`,
  91. `avg(${SpanMetricsField.SPAN_SELF_TIME})`,
  92. 'time_spent_percentage()',
  93. `${SpanFunction.HTTP_ERROR_COUNT}()`,
  94. ],
  95. sorts: [sort],
  96. limit: TRANSACTIONS_TABLE_ROW_COUNT,
  97. cursor,
  98. },
  99. 'api.starfish.span-transaction-metrics'
  100. );
  101. const span = {
  102. ...spanMetrics,
  103. [SpanMetricsField.SPAN_GROUP]: groupId,
  104. } as {
  105. [SpanMetricsField.SPAN_OP]: string;
  106. [SpanMetricsField.SPAN_DESCRIPTION]: string;
  107. [SpanMetricsField.SPAN_ACTION]: string;
  108. [SpanMetricsField.SPAN_DOMAIN]: string[];
  109. [SpanMetricsField.SPAN_GROUP]: string;
  110. };
  111. const {
  112. isLoading: isThroughputDataLoading,
  113. data: throughputData,
  114. error: throughputError,
  115. } = useSpanMetricsSeries(
  116. {
  117. search: MutableSearch.fromQueryObject(filters),
  118. yAxis: ['spm()'],
  119. enabled: Boolean(groupId),
  120. },
  121. 'api.starfish.span-summary-page-metrics-chart'
  122. );
  123. const {
  124. isLoading: isDurationDataLoading,
  125. data: durationData,
  126. error: durationError,
  127. } = useSpanMetricsSeries(
  128. {
  129. search: MutableSearch.fromQueryObject(filters),
  130. yAxis: [`${selectedAggregate}(${SpanMetricsField.SPAN_SELF_TIME})`],
  131. enabled: Boolean(groupId),
  132. },
  133. 'api.starfish.span-summary-page-metrics-chart'
  134. );
  135. useSynchronizeCharts([!isThroughputDataLoading && !isDurationDataLoading]);
  136. return (
  137. <Fragment>
  138. <Layout.Header>
  139. <Layout.HeaderContent>
  140. <Breadcrumbs
  141. crumbs={[
  142. {
  143. label: 'Performance',
  144. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  145. preservePageFilters: true,
  146. },
  147. {
  148. label: 'Queries',
  149. to: normalizeUrl(
  150. `/organizations/${organization.slug}/performance/database`
  151. ),
  152. preservePageFilters: true,
  153. },
  154. {
  155. label: 'Query Summary',
  156. },
  157. ]}
  158. />
  159. <Layout.Title>{t('Query Summary')}</Layout.Title>
  160. </Layout.HeaderContent>
  161. <Layout.HeaderActions>
  162. <ButtonBar gap={1}>
  163. <FeedbackWidgetButton />
  164. </ButtonBar>
  165. </Layout.HeaderActions>
  166. </Layout.Header>
  167. <Layout.Body>
  168. <Layout.Main fullWidth>
  169. <HeaderContainer>
  170. <PageFilterBar condensed>
  171. <EnvironmentPageFilter />
  172. <DatePageFilter />
  173. </PageFilterBar>
  174. <MetricsRibbon>
  175. <MetricReadout
  176. title={getThroughputTitle('db')}
  177. value={spanMetrics?.[`${SpanFunction.SPM}()`]}
  178. unit={RateUnit.PER_MINUTE}
  179. isLoading={areSpanMetricsLoading}
  180. />
  181. <MetricReadout
  182. title={DataTitles.avg}
  183. value={spanMetrics?.[`avg(${SpanMetricsField.SPAN_SELF_TIME})`]}
  184. unit={DurationUnit.MILLISECOND}
  185. isLoading={areSpanMetricsLoading}
  186. />
  187. <MetricReadout
  188. title={DataTitles.timeSpent}
  189. value={spanMetrics?.['sum(span.self_time)']}
  190. unit={DurationUnit.MILLISECOND}
  191. tooltip={getTimeSpentExplanation(
  192. spanMetrics?.['time_spent_percentage()'],
  193. 'db'
  194. )}
  195. isLoading={areSpanMetricsLoading}
  196. />
  197. </MetricsRibbon>
  198. </HeaderContainer>
  199. <ModuleLayout.Layout>
  200. {groupId && (
  201. <DescriptionContainer>
  202. <DatabaseSpanDescription
  203. groupId={groupId}
  204. preliminaryDescription={spanMetrics?.['span.description']}
  205. />
  206. </DescriptionContainer>
  207. )}
  208. <ModuleLayout.Full>
  209. <ChartContainer>
  210. <ThroughputChart
  211. series={throughputData['spm()']}
  212. isLoading={isThroughputDataLoading}
  213. error={throughputError}
  214. />
  215. <DurationChart
  216. series={[
  217. durationData[
  218. `${selectedAggregate}(${SpanMetricsField.SPAN_SELF_TIME})`
  219. ],
  220. ]}
  221. isLoading={isDurationDataLoading}
  222. error={durationError}
  223. />
  224. </ChartContainer>
  225. </ModuleLayout.Full>
  226. {span && (
  227. <ModuleLayout.Full>
  228. <QueryTransactionsTable
  229. span={span}
  230. data={transactionsList}
  231. error={transactionsListError}
  232. isLoading={isTransactionsListLoading}
  233. meta={transactionsListMeta}
  234. pageLinks={transactionsListPageLinks}
  235. sort={sort}
  236. />
  237. </ModuleLayout.Full>
  238. )}
  239. </ModuleLayout.Layout>
  240. <SampleList
  241. groupId={span[SpanMetricsField.SPAN_GROUP]}
  242. transactionName={transaction}
  243. transactionMethod={transactionMethod}
  244. />
  245. </Layout.Main>
  246. </Layout.Body>
  247. </Fragment>
  248. );
  249. }
  250. const DEFAULT_SORT = {
  251. kind: 'desc' as const,
  252. field: 'time_spent_percentage()' as const,
  253. };
  254. const TRANSACTIONS_TABLE_ROW_COUNT = 25;
  255. const ChartContainer = styled('div')`
  256. display: grid;
  257. gap: 0;
  258. grid-template-columns: 1fr;
  259. @media (min-width: ${p => p.theme.breakpoints.small}) {
  260. grid-template-columns: 1fr 1fr;
  261. gap: ${space(2)};
  262. }
  263. `;
  264. const HeaderContainer = styled('div')`
  265. display: flex;
  266. justify-content: space-between;
  267. flex-wrap: wrap;
  268. `;
  269. const DescriptionContainer = styled(ModuleLayout.Full)`
  270. line-height: 1.2;
  271. `;
  272. const MetricsRibbon = styled('div')`
  273. display: flex;
  274. flex-wrap: wrap;
  275. gap: ${space(4)};
  276. `;
  277. function PageWithProviders(props) {
  278. return (
  279. <ModulePageProviders
  280. title={[t('Performance'), t('Database'), t('Query Summary')].join(' — ')}
  281. baseURL="/performance/database"
  282. features="spans-first-ui"
  283. >
  284. <DatabaseSpanSummaryPage {...props} />
  285. </ModulePageProviders>
  286. );
  287. }
  288. export default PageWithProviders;