databaseSpanSummaryPage.tsx 10 KB

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