httpSamplesPanel.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import {Fragment} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as qs from 'query-string';
  5. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  6. import {SegmentedControl} from 'sentry/components/segmentedControl';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import {DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
  10. import {PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
  11. import {decodeScalar} from 'sentry/utils/queryString';
  12. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  13. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. import useProjects from 'sentry/utils/useProjects';
  17. import useRouter from 'sentry/utils/useRouter';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  19. import {AverageValueMarkLine} from 'sentry/views/performance/charts/averageValueMarkLine';
  20. import {DurationChart} from 'sentry/views/performance/http/durationChart';
  21. import decodePanel from 'sentry/views/performance/http/queryParameterDecoders/panel';
  22. import {ResponseRateChart} from 'sentry/views/performance/http/responseRateChart';
  23. import {SpanSamplesTable} from 'sentry/views/performance/http/spanSamplesTable';
  24. import {useSpanSamples} from 'sentry/views/performance/http/useSpanSamples';
  25. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  26. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  27. import {computeAxisMax} from 'sentry/views/starfish/components/chart';
  28. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  29. import {getTimeSpentExplanation} from 'sentry/views/starfish/components/tableCells/timeSpentCell';
  30. import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics';
  31. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  32. import {
  33. ModuleName,
  34. SpanFunction,
  35. SpanIndexedField,
  36. SpanMetricsField,
  37. type SpanMetricsQueryFilters,
  38. } from 'sentry/views/starfish/types';
  39. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  40. export function HTTPSamplesPanel() {
  41. const router = useRouter();
  42. const location = useLocation();
  43. const query = useLocationQuery({
  44. fields: {
  45. project: decodeScalar,
  46. domain: decodeScalar,
  47. transaction: decodeScalar,
  48. transactionMethod: decodeScalar,
  49. panel: decodePanel,
  50. },
  51. });
  52. const organization = useOrganization();
  53. const {projects} = useProjects();
  54. const project = projects.find(p => query.project === p.id);
  55. // `detailKey` controls whether the panel is open. If all required properties are available, concat them to make a key, otherwise set to `undefined` and hide the panel
  56. const detailKey =
  57. query.transaction && query.domain
  58. ? [query.domain, query.transactionMethod, query.transaction]
  59. .filter(Boolean)
  60. .join(':')
  61. : undefined;
  62. const handlePanelChange = newPanelName => {
  63. router.replace({
  64. pathname: location.pathname,
  65. query: {
  66. ...location.query,
  67. panel: newPanelName,
  68. },
  69. });
  70. };
  71. const isPanelOpen = Boolean(detailKey);
  72. const filters: SpanMetricsQueryFilters = {
  73. 'span.module': ModuleName.HTTP,
  74. 'span.domain': query.domain,
  75. transaction: query.transaction,
  76. };
  77. const {
  78. data: domainTransactionMetrics,
  79. isFetching: areDomainTransactionMetricsFetching,
  80. } = useSpanMetrics({
  81. search: MutableSearch.fromQueryObject(filters),
  82. fields: [
  83. `${SpanFunction.SPM}()`,
  84. `avg(${SpanMetricsField.SPAN_SELF_TIME})`,
  85. `sum(${SpanMetricsField.SPAN_SELF_TIME})`,
  86. 'http_response_rate(3)',
  87. 'http_response_rate(4)',
  88. 'http_response_rate(5)',
  89. `${SpanFunction.TIME_SPENT_PERCENTAGE}()`,
  90. ],
  91. enabled: isPanelOpen,
  92. referrer: 'api.starfish.http-module-samples-panel-metrics-ribbon',
  93. });
  94. const {
  95. isFetching: isDurationDataFetching,
  96. data: durationData,
  97. error: durationError,
  98. } = useSpanMetricsSeries({
  99. search: MutableSearch.fromQueryObject(filters),
  100. yAxis: [`avg(span.self_time)`],
  101. enabled: isPanelOpen && query.panel === 'duration',
  102. referrer: 'api.starfish.http-module-samples-panel-duration-chart',
  103. });
  104. const {
  105. isFetching: isResponseCodeDataLoading,
  106. data: responseCodeData,
  107. error: responseCodeError,
  108. } = useSpanMetricsSeries({
  109. search: MutableSearch.fromQueryObject(filters),
  110. yAxis: ['http_response_rate(3)', 'http_response_rate(4)', 'http_response_rate(5)'],
  111. enabled: isPanelOpen && query.panel === 'status',
  112. referrer: 'api.starfish.http-module-samples-panel-response-code-chart',
  113. });
  114. const durationAxisMax = computeAxisMax([durationData?.[`avg(span.self_time)`]]);
  115. const {
  116. data: samplesData,
  117. isFetching: isSamplesDataFetching,
  118. error: samplesDataError,
  119. } = useSpanSamples({
  120. search: MutableSearch.fromQueryObject(filters),
  121. fields: [
  122. SpanIndexedField.TRANSACTION_ID,
  123. SpanIndexedField.SPAN_DESCRIPTION,
  124. SpanIndexedField.RESPONSE_CODE,
  125. ],
  126. min: 0,
  127. max: durationAxisMax,
  128. enabled: query.panel === 'duration' && durationAxisMax > 0,
  129. referrer: 'api.starfish.http-module-samples-panel-samples',
  130. });
  131. const handleClose = () => {
  132. router.replace({
  133. pathname: router.location.pathname,
  134. query: {
  135. ...router.location.query,
  136. transaction: undefined,
  137. transactionMethod: undefined,
  138. },
  139. });
  140. };
  141. return (
  142. <PageAlertProvider>
  143. <DetailPanel detailKey={detailKey} onClose={handleClose}>
  144. <ModuleLayout.Layout>
  145. <ModuleLayout.Full>
  146. <HeaderContainer>
  147. {project && (
  148. <SpanSummaryProjectAvatar
  149. project={project}
  150. direction="left"
  151. size={40}
  152. hasTooltip
  153. tooltip={project.slug}
  154. />
  155. )}
  156. <TitleContainer>
  157. <Title>
  158. <Link
  159. to={normalizeUrl(
  160. `/organizations/${organization.slug}/performance/summary?${qs.stringify(
  161. {
  162. project: query.project,
  163. transaction: query.transaction,
  164. }
  165. )}`
  166. )}
  167. >
  168. {query.transaction &&
  169. query.transactionMethod &&
  170. !query.transaction.startsWith(query.transactionMethod)
  171. ? `${query.transactionMethod} ${query.transaction}`
  172. : query.transaction}
  173. </Link>
  174. </Title>
  175. </TitleContainer>
  176. </HeaderContainer>
  177. </ModuleLayout.Full>
  178. <ModuleLayout.Full>
  179. <MetricsRibbon>
  180. <MetricReadout
  181. align="left"
  182. title={getThroughputTitle('http')}
  183. value={domainTransactionMetrics?.[0]?.[`${SpanFunction.SPM}()`]}
  184. unit={RateUnit.PER_MINUTE}
  185. isLoading={areDomainTransactionMetricsFetching}
  186. />
  187. <MetricReadout
  188. align="left"
  189. title={DataTitles.avg}
  190. value={
  191. domainTransactionMetrics?.[0]?.[
  192. `avg(${SpanMetricsField.SPAN_SELF_TIME})`
  193. ]
  194. }
  195. unit={DurationUnit.MILLISECOND}
  196. isLoading={areDomainTransactionMetricsFetching}
  197. />
  198. <MetricReadout
  199. align="left"
  200. title={t('3XXs')}
  201. value={domainTransactionMetrics?.[0]?.[`http_response_rate(3)`]}
  202. unit="percentage"
  203. isLoading={areDomainTransactionMetricsFetching}
  204. />
  205. <MetricReadout
  206. align="left"
  207. title={t('4XXs')}
  208. value={domainTransactionMetrics?.[0]?.[`http_response_rate(4)`]}
  209. unit="percentage"
  210. isLoading={areDomainTransactionMetricsFetching}
  211. />
  212. <MetricReadout
  213. align="left"
  214. title={t('5XXs')}
  215. value={domainTransactionMetrics?.[0]?.[`http_response_rate(5)`]}
  216. unit="percentage"
  217. isLoading={areDomainTransactionMetricsFetching}
  218. />
  219. <MetricReadout
  220. align="left"
  221. title={DataTitles.timeSpent}
  222. value={domainTransactionMetrics?.[0]?.['sum(span.self_time)']}
  223. unit={DurationUnit.MILLISECOND}
  224. tooltip={getTimeSpentExplanation(
  225. domainTransactionMetrics?.[0]?.['time_spent_percentage()'],
  226. 'db'
  227. )}
  228. isLoading={areDomainTransactionMetricsFetching}
  229. />
  230. </MetricsRibbon>
  231. </ModuleLayout.Full>
  232. <ModuleLayout.Full>
  233. <SegmentedControl
  234. value={query.panel}
  235. onChange={handlePanelChange}
  236. aria-label={t('Choose breakdown type')}
  237. >
  238. <SegmentedControl.Item key="duration">
  239. {t('By Duration')}
  240. </SegmentedControl.Item>
  241. <SegmentedControl.Item key="status">
  242. {t('By Response Code')}
  243. </SegmentedControl.Item>
  244. </SegmentedControl>
  245. </ModuleLayout.Full>
  246. {query.panel === 'duration' && (
  247. <Fragment>
  248. <ModuleLayout.Full>
  249. <DurationChart
  250. series={[
  251. {
  252. ...durationData[`avg(span.self_time)`],
  253. markLine: AverageValueMarkLine(),
  254. },
  255. ]}
  256. isLoading={isDurationDataFetching}
  257. error={durationError}
  258. />
  259. </ModuleLayout.Full>
  260. <ModuleLayout.Full>
  261. <SpanSamplesTable
  262. data={samplesData}
  263. isLoading={isDurationDataFetching || isSamplesDataFetching}
  264. error={samplesDataError}
  265. // TODO: The samples endpoint doesn't provide its own meta, so we need to create it manually
  266. meta={{
  267. fields: {
  268. 'span.response_code': 'number',
  269. },
  270. units: {},
  271. }}
  272. />
  273. </ModuleLayout.Full>
  274. </Fragment>
  275. )}
  276. {query.panel === 'status' && (
  277. <ModuleLayout.Full>
  278. <ResponseRateChart
  279. series={[
  280. {
  281. ...responseCodeData[`http_response_rate(3)`],
  282. seriesName: t('3XX'),
  283. },
  284. {
  285. ...responseCodeData[`http_response_rate(4)`],
  286. seriesName: t('4XX'),
  287. },
  288. {
  289. ...responseCodeData[`http_response_rate(5)`],
  290. seriesName: t('5XX'),
  291. },
  292. ]}
  293. isLoading={isResponseCodeDataLoading}
  294. error={responseCodeError}
  295. />
  296. </ModuleLayout.Full>
  297. )}
  298. </ModuleLayout.Layout>
  299. </DetailPanel>
  300. </PageAlertProvider>
  301. );
  302. }
  303. const SpanSummaryProjectAvatar = styled(ProjectAvatar)`
  304. padding-right: ${space(1)};
  305. `;
  306. const HeaderContainer = styled('div')`
  307. display: grid;
  308. grid-template-rows: auto auto auto;
  309. @media (min-width: ${p => p.theme.breakpoints.small}) {
  310. grid-template-rows: auto;
  311. grid-template-columns: auto 1fr auto;
  312. }
  313. `;
  314. const TitleContainer = styled('div')`
  315. width: 100%;
  316. position: relative;
  317. height: 40px;
  318. `;
  319. const Title = styled('h4')`
  320. position: absolute;
  321. bottom: 0;
  322. margin-bottom: 0;
  323. `;
  324. const MetricsRibbon = styled('div')`
  325. display: flex;
  326. flex-wrap: wrap;
  327. gap: ${space(4)};
  328. `;