databaseSpanSummaryPage.tsx 9.5 KB

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