index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import {Fragment, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as qs from 'query-string';
  5. import Breadcrumbs from 'sentry/components/breadcrumbs';
  6. import {Button} from 'sentry/components/button';
  7. import _EventsRequest from 'sentry/components/charts/eventsRequest';
  8. import {getInterval} from 'sentry/components/charts/utils';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import {PerformanceLayoutBodyRow} from 'sentry/components/performance/layouts';
  13. import Placeholder from 'sentry/components/placeholder';
  14. import {SegmentedControl} from 'sentry/components/segmentedControl';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {IssueCategory, NewQuery} from 'sentry/types';
  18. import {Series} from 'sentry/types/echarts';
  19. import {defined} from 'sentry/utils';
  20. import {tooltipFormatterUsingAggregateOutputType} from 'sentry/utils/discover/charts';
  21. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  22. import EventView from 'sentry/utils/discover/eventView';
  23. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  24. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import usePageFilters from 'sentry/utils/usePageFilters';
  28. import withApi from 'sentry/utils/withApi';
  29. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  30. import {SidebarSpacer} from 'sentry/views/performance/transactionSummary/utils';
  31. import {ERRORS_COLOR, P95_COLOR, THROUGHPUT_COLOR} from 'sentry/views/starfish/colours';
  32. import Chart, {useSynchronizeCharts} from 'sentry/views/starfish/components/chart';
  33. import StarfishDatePicker from 'sentry/views/starfish/components/datePicker';
  34. import {TransactionSamplesTable} from 'sentry/views/starfish/components/samplesTable/transactionSamplesTable';
  35. import {ModuleName} from 'sentry/views/starfish/types';
  36. import formatThroughput from 'sentry/views/starfish/utils/chartValueFormatters/formatThroughput';
  37. import {STARFISH_CHART_INTERVAL_FIDELITY} from 'sentry/views/starfish/utils/constants';
  38. import {getDateConditions} from 'sentry/views/starfish/utils/getDateConditions';
  39. import SpansTable from 'sentry/views/starfish/views/spans/spansTable';
  40. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  41. import IssuesTable from 'sentry/views/starfish/views/webServiceView/endpointOverview/issuesTable';
  42. import {SpanGroupBreakdownContainer} from 'sentry/views/starfish/views/webServiceView/spanGroupBreakdownContainer';
  43. const SPANS_TABLE_LIMIT = 5;
  44. const EventsRequest = withApi(_EventsRequest);
  45. export type SampleFilter = 'ALL' | '500s';
  46. type State = {
  47. samplesFilter: SampleFilter;
  48. spansFilter: ModuleName;
  49. };
  50. export default function EndpointOverview() {
  51. const location = useLocation();
  52. const organization = useOrganization();
  53. const {endpoint, 'http.method': httpMethod, statsPeriod} = location.query;
  54. const transaction = endpoint
  55. ? Array.isArray(endpoint)
  56. ? endpoint[0]
  57. : endpoint
  58. : undefined;
  59. const method = httpMethod
  60. ? Array.isArray(httpMethod)
  61. ? httpMethod[0]
  62. : httpMethod
  63. : undefined;
  64. const pageFilter = usePageFilters();
  65. const [state, setState] = useState<State>({
  66. spansFilter: ModuleName.ALL,
  67. samplesFilter: 'ALL',
  68. });
  69. const [issueFilter, setIssueFilter] = useState<IssueCategory | 'ALL'>('ALL');
  70. const queryConditions = [
  71. 'has:http.method',
  72. 'transaction.op:http.server',
  73. `transaction:${transaction}`,
  74. `http.method:${method}`,
  75. ];
  76. const query = new MutableSearch(queryConditions);
  77. const savedQuery: NewQuery = {
  78. id: undefined,
  79. name: t('Endpoint Overview'),
  80. query: query.formatString(),
  81. fields: ['tps()', 'p95(transaction.duration)', 'http_error_count()'],
  82. dataset: DiscoverDatasets.METRICS,
  83. start: pageFilter.selection.datetime.start ?? undefined,
  84. end: pageFilter.selection.datetime.end ?? undefined,
  85. range: pageFilter.selection.datetime.period ?? undefined,
  86. version: 2,
  87. };
  88. const eventView = EventView.fromNewQueryWithLocation(savedQuery, location);
  89. const {data: totals, isLoading: isTotalsLoading} = useDiscoverQuery({
  90. eventView,
  91. orgSlug: organization.slug,
  92. location,
  93. });
  94. function renderSidebarCharts() {
  95. return (
  96. <EventsRequest
  97. query={query.formatString()}
  98. includePrevious={false}
  99. partial
  100. limit={5}
  101. interval={getInterval(
  102. pageFilter.selection.datetime,
  103. STARFISH_CHART_INTERVAL_FIDELITY
  104. )}
  105. includeTransformedData
  106. environment={eventView.environment}
  107. project={eventView.project}
  108. period={eventView.statsPeriod}
  109. referrer="starfish-endpoint-overview"
  110. start={eventView.start}
  111. end={eventView.end}
  112. organization={organization}
  113. yAxis={['tps()', 'http_error_count()', 'p95(transaction.duration)']}
  114. dataset={DiscoverDatasets.METRICS}
  115. >
  116. {({loading, results}) => {
  117. if (!results) {
  118. return null;
  119. }
  120. // Force label to be Requests
  121. const throughputResults = {seriesName: 'Requests', data: results[0].data};
  122. const percentileData: Series = {
  123. seriesName: t('Requests'),
  124. data: results[2].data,
  125. };
  126. return (
  127. <Fragment>
  128. <Header>
  129. <ChartLabel>{DataTitles.p95}</ChartLabel>
  130. </Header>
  131. <ChartSummaryValue
  132. isLoading={isTotalsLoading}
  133. value={
  134. defined(totals)
  135. ? t(
  136. '%sms',
  137. (totals.data[0]['p95(transaction.duration)'] as number).toFixed(2)
  138. )
  139. : undefined
  140. }
  141. />
  142. <Chart
  143. statsPeriod={eventView.statsPeriod}
  144. height={80}
  145. data={[percentileData]}
  146. start={eventView.start as string}
  147. end={eventView.end as string}
  148. loading={loading}
  149. utc={false}
  150. grid={{
  151. left: '8px',
  152. right: '0',
  153. top: '8px',
  154. bottom: '0',
  155. }}
  156. disableXAxis
  157. definedAxisTicks={2}
  158. isLineChart
  159. chartColors={[P95_COLOR]}
  160. tooltipFormatterOptions={{
  161. valueFormatter: value =>
  162. tooltipFormatterUsingAggregateOutputType(value, 'duration'),
  163. }}
  164. />
  165. <Header>
  166. <ChartLabel>{DataTitles.throughput}</ChartLabel>
  167. </Header>
  168. <ChartSummaryValue
  169. isLoading={isTotalsLoading}
  170. value={
  171. defined(totals)
  172. ? t('%s/s', (totals.data[0]['tps()'] as number).toFixed(2))
  173. : undefined
  174. }
  175. />
  176. <Chart
  177. statsPeriod={(statsPeriod as string) ?? '24h'}
  178. height={80}
  179. data={[throughputResults]}
  180. start=""
  181. end=""
  182. loading={loading}
  183. utc={false}
  184. isLineChart
  185. definedAxisTicks={2}
  186. disableXAxis
  187. chartColors={[THROUGHPUT_COLOR]}
  188. aggregateOutputFormat="rate"
  189. grid={{
  190. left: '8px',
  191. right: '0',
  192. top: '8px',
  193. bottom: '0',
  194. }}
  195. tooltipFormatterOptions={{
  196. valueFormatter: value => formatThroughput(value),
  197. }}
  198. />
  199. <SidebarSpacer />
  200. <Header>
  201. <ChartLabel>{DataTitles.errorCount}</ChartLabel>
  202. </Header>
  203. <ChartSummaryValue
  204. isLoading={isTotalsLoading}
  205. value={
  206. defined(totals)
  207. ? tooltipFormatterUsingAggregateOutputType(
  208. totals.data[0]['http_error_count()'] as number,
  209. 'integer'
  210. )
  211. : undefined
  212. }
  213. />
  214. <Chart
  215. statsPeriod={eventView.statsPeriod}
  216. height={80}
  217. data={[results[1]]}
  218. start={eventView.start as string}
  219. end={eventView.end as string}
  220. loading={loading}
  221. utc={false}
  222. grid={{
  223. left: '8px',
  224. right: '0',
  225. top: '8px',
  226. bottom: '0',
  227. }}
  228. definedAxisTicks={2}
  229. isLineChart
  230. chartColors={[ERRORS_COLOR]}
  231. />
  232. <SidebarSpacer />
  233. </Fragment>
  234. );
  235. }}
  236. </EventsRequest>
  237. );
  238. }
  239. const handleViewAllEventsClick = () => {
  240. const issuesQuery = new MutableSearch([
  241. ...(issueFilter === 'ALL' ? [] : [`issue.category:${issueFilter}`]),
  242. `transaction:${transaction}`,
  243. `http.method:${method}`,
  244. ]);
  245. browserHistory.push({
  246. pathname: `/issues/?${qs.stringify({
  247. ...getDateConditions(pageFilter.selection),
  248. query: issuesQuery.formatString(),
  249. })}`,
  250. });
  251. };
  252. useSynchronizeCharts();
  253. return (
  254. <PageFiltersContainer>
  255. <Layout.Page>
  256. <Layout.Header>
  257. <Layout.HeaderContent>
  258. <Breadcrumbs
  259. crumbs={[
  260. {
  261. label: t('Web Service'),
  262. to: normalizeUrl(`/organizations/${organization.slug}/starfish/`),
  263. },
  264. {
  265. label: t('Endpoint Overview'),
  266. },
  267. ]}
  268. />
  269. <Layout.Title>{`${method} ${transaction}`}</Layout.Title>
  270. </Layout.HeaderContent>
  271. </Layout.Header>
  272. <Layout.Body>
  273. <SearchContainerWithFilterAndMetrics>
  274. <PageFilterBar condensed>
  275. <StarfishDatePicker />
  276. </PageFilterBar>
  277. </SearchContainerWithFilterAndMetrics>
  278. <Layout.Main>
  279. <StyledRow minSize={200}>
  280. <SpanGroupBreakdownContainer
  281. transaction={transaction as string}
  282. // transactionMethod={method}
  283. />
  284. </StyledRow>
  285. <SegmentedControlContainer>
  286. <SegmentedControl
  287. size="xs"
  288. aria-label={t('Filter Spans')}
  289. value={state.spansFilter}
  290. onChange={key => setState({...state, spansFilter: key})}
  291. >
  292. <SegmentedControl.Item key="">{t('All Spans')}</SegmentedControl.Item>
  293. <SegmentedControl.Item key="http">{t('http')}</SegmentedControl.Item>
  294. <SegmentedControl.Item key="db">{t('db')}</SegmentedControl.Item>
  295. </SegmentedControl>
  296. </SegmentedControlContainer>
  297. <SpanMetricsTable
  298. filter={state.spansFilter}
  299. transaction={transaction}
  300. method={method}
  301. />
  302. <RowContainer>
  303. <SegmentedControlContainer>
  304. <SegmentedControl
  305. size="xs"
  306. aria-label={t('Filter events')}
  307. value={state.samplesFilter}
  308. onChange={key => setState({...state, samplesFilter: key})}
  309. >
  310. <SegmentedControl.Item key="ALL">
  311. {t('Sample Events')}
  312. </SegmentedControl.Item>
  313. <SegmentedControl.Item key="500s">{t('5XXs')}</SegmentedControl.Item>
  314. </SegmentedControl>
  315. </SegmentedControlContainer>
  316. <TransactionSamplesTable
  317. queryConditions={queryConditions}
  318. sampleFilter={state.samplesFilter}
  319. />
  320. </RowContainer>
  321. <SegmentedControlContainer>
  322. <SegmentedControl
  323. size="xs"
  324. aria-label={t('Filter issue types')}
  325. value={issueFilter}
  326. onChange={key => setIssueFilter(key)}
  327. >
  328. <SegmentedControl.Item key="ALL">{t('All Issues')}</SegmentedControl.Item>
  329. <SegmentedControl.Item key={IssueCategory.ERROR}>
  330. {t('Errors Only')}
  331. </SegmentedControl.Item>
  332. <SegmentedControl.Item key={IssueCategory.PERFORMANCE}>
  333. {t('Performance Only')}
  334. </SegmentedControl.Item>
  335. </SegmentedControl>
  336. <Button size="sm" onClick={handleViewAllEventsClick}>
  337. {t('View All')}
  338. </Button>
  339. </SegmentedControlContainer>
  340. <IssuesTable
  341. issueCategory={issueFilter === 'ALL' ? undefined : issueFilter}
  342. httpMethod={method as string}
  343. transactionName={transaction}
  344. />
  345. </Layout.Main>
  346. <Layout.Side>
  347. {renderSidebarCharts()}
  348. <SidebarSpacer />
  349. </Layout.Side>
  350. </Layout.Body>
  351. </Layout.Page>
  352. </PageFiltersContainer>
  353. );
  354. }
  355. function SpanMetricsTable({
  356. filter,
  357. transaction,
  358. method,
  359. }: {
  360. filter: ModuleName;
  361. transaction: string | undefined;
  362. method?: string;
  363. }) {
  364. return (
  365. <SpansTable
  366. moduleName={filter ?? ModuleName.ALL}
  367. sort={{
  368. field: 'time_spent_percentage(local)',
  369. kind: 'desc',
  370. }}
  371. endpoint={transaction}
  372. method={method}
  373. limit={SPANS_TABLE_LIMIT}
  374. />
  375. );
  376. }
  377. type ChartValueProps = {
  378. isLoading: boolean;
  379. value: React.ReactNode;
  380. };
  381. function ChartSummaryValue({isLoading, value}: ChartValueProps) {
  382. if (isLoading) {
  383. return <Placeholder height="24px" />;
  384. }
  385. return <ChartValue>{value}</ChartValue>;
  386. }
  387. const ChartValue = styled('div')`
  388. font-size: ${p => p.theme.fontSizeExtraLarge};
  389. `;
  390. const SearchContainerWithFilterAndMetrics = styled('div')`
  391. display: grid;
  392. grid-template-rows: auto auto auto;
  393. gap: ${space(2)};
  394. margin-bottom: ${space(2)};
  395. @media (min-width: ${p => p.theme.breakpoints.small}) {
  396. grid-template-rows: auto;
  397. grid-template-columns: auto 1fr auto;
  398. }
  399. `;
  400. const RowContainer = styled('div')`
  401. padding-bottom: ${space(4)};
  402. `;
  403. const StyledRow = styled(PerformanceLayoutBodyRow)`
  404. margin-bottom: ${space(4)};
  405. `;
  406. const SegmentedControlContainer = styled('div')`
  407. margin-bottom: ${space(2)};
  408. display: flex;
  409. justify-content: space-between;
  410. height: 32px;
  411. align-items: center;
  412. `;
  413. const ChartLabel = styled('div')`
  414. ${p => p.theme.text.cardTitle}
  415. `;
  416. const Header = styled('div')`
  417. padding: 0 ${space(1)} 0 0;
  418. min-height: 24px;
  419. width: 100%;
  420. display: flex;
  421. align-items: center;
  422. justify-content: space-between;
  423. `;