databaseSpanSummaryPage.tsx 9.3 KB

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