messageConsumerSamplesPanel.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import styled from '@emotion/styled';
  2. import * as qs from 'query-string';
  3. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  4. import {Button} from 'sentry/components/button';
  5. import Link from 'sentry/components/links/link';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {DurationUnit, SizeUnit} from 'sentry/utils/discover/fields';
  9. import {PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
  10. import {decodeScalar} from 'sentry/utils/queryString';
  11. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  12. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import useProjects from 'sentry/utils/useProjects';
  15. import useRouter from 'sentry/utils/useRouter';
  16. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  17. import {AverageValueMarkLine} from 'sentry/views/performance/charts/averageValueMarkLine';
  18. import {DurationChart} from 'sentry/views/performance/http/charts/durationChart';
  19. import {useSpanSamples} from 'sentry/views/performance/http/data/useSpanSamples';
  20. import {useDebouncedState} from 'sentry/views/performance/http/useDebouncedState';
  21. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  22. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  23. import {MessageSpanSamplesTable} from 'sentry/views/performance/queues/messageSpanSamplesTable';
  24. import {useQueuesMetricsQuery} from 'sentry/views/performance/queues/queries/useQueuesMetricsQuery';
  25. import {computeAxisMax} from 'sentry/views/starfish/components/chart';
  26. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  27. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useDiscoverSeries';
  28. import {SpanIndexedField} from 'sentry/views/starfish/types';
  29. import {useSampleScatterPlotSeries} from 'sentry/views/starfish/views/spanSummaryPage/sampleList/durationChart/useSampleScatterPlotSeries';
  30. // We're defining our own query filter here, apart from settings.ts because the spans endpoint doesn't accept IN operations
  31. const DEFAULT_QUERY_FILTER = 'span.op:queue.process OR span.op:queue.publish';
  32. export function MessageConsumerSamplesPanel() {
  33. const router = useRouter();
  34. const query = useLocationQuery({
  35. fields: {
  36. project: decodeScalar,
  37. destination: decodeScalar,
  38. transaction: decodeScalar,
  39. },
  40. });
  41. const {projects} = useProjects();
  42. const project = projects.find(p => query.project === p.id);
  43. const organization = useOrganization();
  44. const [highlightedSpanId, setHighlightedSpanId] = useDebouncedState<string | undefined>(
  45. undefined,
  46. [],
  47. SAMPLE_HOVER_DEBOUNCE
  48. );
  49. // `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
  50. const detailKey = query.transaction
  51. ? [query.destination, query.transaction].filter(Boolean).join(':')
  52. : undefined;
  53. const isPanelOpen = Boolean(detailKey);
  54. const search = new MutableSearch(DEFAULT_QUERY_FILTER);
  55. search.addFilterValue('transaction', query.transaction);
  56. search.addFilterValue('messaging.destination.name', query.destination);
  57. const {data: transactionMetrics, isFetching: aretransactionMetricsFetching} =
  58. useQueuesMetricsQuery({
  59. destination: query.destination,
  60. transaction: query.transaction,
  61. enabled: isPanelOpen,
  62. });
  63. const avg = transactionMetrics?.[0]?.['avg(span.duration)'];
  64. const {
  65. isFetching: isDurationDataFetching,
  66. data: durationData,
  67. error: durationError,
  68. } = useSpanMetricsSeries(
  69. {
  70. search,
  71. yAxis: [`avg(span.duration)`],
  72. enabled: isPanelOpen,
  73. },
  74. 'api.performance.queues.avg-duration-chart'
  75. );
  76. const durationAxisMax = computeAxisMax([durationData?.[`avg(span.duration)`]]);
  77. const {
  78. data: durationSamplesData,
  79. isFetching: isDurationSamplesDataFetching,
  80. error: durationSamplesDataError,
  81. refetch: refetchDurationSpanSamples,
  82. } = useSpanSamples({
  83. search,
  84. min: 0,
  85. max: durationAxisMax,
  86. enabled: isPanelOpen && durationAxisMax > 0,
  87. fields: [
  88. SpanIndexedField.TRACE,
  89. SpanIndexedField.TRANSACTION_ID,
  90. SpanIndexedField.SPAN_DESCRIPTION,
  91. SpanIndexedField.MESSAGING_MESSAGE_BODY_SIZE,
  92. SpanIndexedField.MESSAGING_MESSAGE_RECEIVE_LATENCY,
  93. SpanIndexedField.MESSAGING_MESSAGE_ID,
  94. SpanIndexedField.TRACE_STATUS,
  95. SpanIndexedField.SPAN_DURATION,
  96. ],
  97. });
  98. const sampledSpanDataSeries = useSampleScatterPlotSeries(
  99. durationSamplesData,
  100. transactionMetrics?.[0]?.['avg(span.duration)'],
  101. highlightedSpanId,
  102. 'span.duration'
  103. );
  104. const findSampleFromDataPoint = (dataPoint: {name: string | number; value: number}) => {
  105. return durationSamplesData.find(
  106. s => s.timestamp === dataPoint.name && s['span.duration'] === dataPoint.value
  107. );
  108. };
  109. const handleClose = () => {
  110. router.replace({
  111. pathname: router.location.pathname,
  112. query: {
  113. ...router.location.query,
  114. transaction: undefined,
  115. transactionMethod: undefined,
  116. },
  117. });
  118. };
  119. return (
  120. <PageAlertProvider>
  121. <DetailPanel detailKey={detailKey} onClose={handleClose}>
  122. <ModuleLayout.Layout>
  123. <ModuleLayout.Full>
  124. <HeaderContainer>
  125. {project && (
  126. <SpanSummaryProjectAvatar
  127. project={project}
  128. direction="left"
  129. size={40}
  130. hasTooltip
  131. tooltip={project.slug}
  132. />
  133. )}
  134. <TitleContainer>
  135. <Title>
  136. <Link
  137. to={normalizeUrl(
  138. `/organizations/${organization.slug}/performance/summary?${qs.stringify(
  139. {
  140. project: query.project,
  141. transaction: query.transaction,
  142. }
  143. )}`
  144. )}
  145. >
  146. {query.transaction}
  147. </Link>
  148. </Title>
  149. </TitleContainer>
  150. </HeaderContainer>
  151. </ModuleLayout.Full>
  152. <ModuleLayout.Full>
  153. <MetricsRibbon>
  154. <MetricReadout
  155. align="left"
  156. title={t('Processed')}
  157. value={transactionMetrics?.[0]?.['count()']}
  158. unit={'count'}
  159. isLoading={aretransactionMetricsFetching}
  160. />
  161. <MetricReadout
  162. align="left"
  163. title={t('Error Rate')}
  164. value={undefined}
  165. unit={'percentage'}
  166. isLoading={aretransactionMetricsFetching}
  167. />
  168. <MetricReadout
  169. title={t('Avg Time In Queue')}
  170. value={transactionMetrics[0]?.['avg(messaging.message.receive.latency)']}
  171. unit={DurationUnit.MILLISECOND}
  172. isLoading={false}
  173. />
  174. <MetricReadout
  175. title={t('Avg Processing Latency')}
  176. value={
  177. transactionMetrics[0]?.['avg_if(span.duration,span.op,queue.process)']
  178. }
  179. unit={DurationUnit.MILLISECOND}
  180. isLoading={false}
  181. />
  182. </MetricsRibbon>
  183. </ModuleLayout.Full>
  184. <ModuleLayout.Full>
  185. <DurationChart
  186. series={[
  187. {
  188. ...durationData[`avg(span.duration)`],
  189. markLine: AverageValueMarkLine({value: avg}),
  190. },
  191. ]}
  192. scatterPlot={sampledSpanDataSeries}
  193. onHighlight={highlights => {
  194. const firstHighlight = highlights[0];
  195. if (!firstHighlight) {
  196. setHighlightedSpanId(undefined);
  197. return;
  198. }
  199. const sample = findSampleFromDataPoint(firstHighlight.dataPoint);
  200. setHighlightedSpanId(sample?.span_id);
  201. }}
  202. isLoading={isDurationDataFetching}
  203. error={durationError}
  204. />
  205. </ModuleLayout.Full>
  206. <ModuleLayout.Full>
  207. <MessageSpanSamplesTable
  208. data={durationSamplesData}
  209. isLoading={isDurationDataFetching || isDurationSamplesDataFetching}
  210. highlightedSpanId={highlightedSpanId}
  211. onSampleMouseOver={sample => setHighlightedSpanId(sample.span_id)}
  212. onSampleMouseOut={() => setHighlightedSpanId(undefined)}
  213. error={durationSamplesDataError}
  214. // Samples endpoint doesn't provide meta data, so we need to provide it here
  215. meta={{
  216. fields: {
  217. 'span.duration': 'duration',
  218. 'measurements.messaging.message.body.size': 'size',
  219. },
  220. units: {
  221. 'span.duration': DurationUnit.MILLISECOND,
  222. 'measurements.messaging.message.body.size': SizeUnit.BYTE,
  223. },
  224. }}
  225. />
  226. </ModuleLayout.Full>
  227. <ModuleLayout.Full>
  228. <Button onClick={() => refetchDurationSpanSamples()}>
  229. {t('Try Different Samples')}
  230. </Button>
  231. </ModuleLayout.Full>
  232. </ModuleLayout.Layout>
  233. </DetailPanel>
  234. </PageAlertProvider>
  235. );
  236. }
  237. const SAMPLE_HOVER_DEBOUNCE = 10;
  238. const SpanSummaryProjectAvatar = styled(ProjectAvatar)`
  239. padding-right: ${space(1)};
  240. `;
  241. const HeaderContainer = styled('div')`
  242. display: grid;
  243. grid-template-rows: auto auto auto;
  244. @media (min-width: ${p => p.theme.breakpoints.small}) {
  245. grid-template-rows: auto;
  246. grid-template-columns: auto 1fr auto;
  247. }
  248. `;
  249. const TitleContainer = styled('div')`
  250. width: 100%;
  251. position: relative;
  252. height: 40px;
  253. `;
  254. const Title = styled('h4')`
  255. position: absolute;
  256. bottom: 0;
  257. margin-bottom: 0;
  258. `;
  259. const MetricsRibbon = styled('div')`
  260. display: flex;
  261. flex-wrap: wrap;
  262. gap: ${space(4)};
  263. `;