123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- import {Fragment, useCallback} from 'react';
- import styled from '@emotion/styled';
- import * as qs from 'query-string';
- import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
- import {Button} from 'sentry/components/button';
- import {CompactSelect} from 'sentry/components/compactSelect';
- import SearchBar from 'sentry/components/events/searchBar';
- import Link from 'sentry/components/links/link';
- import {SpanSearchQueryBuilder} from 'sentry/components/performance/spanSearchQueryBuilder';
- import {SegmentedControl} from 'sentry/components/segmentedControl';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import {DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
- import {DiscoverDatasets} from 'sentry/utils/discover/types';
- import {PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
- import {decodeList, decodeScalar} from 'sentry/utils/queryString';
- import {
- EMPTY_OPTION_VALUE,
- escapeFilterValue,
- MutableSearch,
- } from 'sentry/utils/tokenizeSearch';
- import normalizeUrl from 'sentry/utils/url/normalizeUrl';
- import useLocationQuery from 'sentry/utils/url/useLocationQuery';
- import {useLocation} from 'sentry/utils/useLocation';
- import {useNavigate} from 'sentry/utils/useNavigate';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import useProjects from 'sentry/utils/useProjects';
- import {computeAxisMax} from 'sentry/views/insights/common/components/chart';
- import DetailPanel from 'sentry/views/insights/common/components/detailPanel';
- import {MetricReadout} from 'sentry/views/insights/common/components/metricReadout';
- import * as ModuleLayout from 'sentry/views/insights/common/components/moduleLayout';
- import {ReadoutRibbon} from 'sentry/views/insights/common/components/ribbon';
- import {getTimeSpentExplanation} from 'sentry/views/insights/common/components/tableCells/timeSpentCell';
- import {
- useSpanMetrics,
- useSpansIndexed,
- } from 'sentry/views/insights/common/queries/useDiscover';
- import {useSpanMetricsSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';
- import {useSpanMetricsTopNSeries} from 'sentry/views/insights/common/queries/useSpanMetricsTopNSeries';
- import {AverageValueMarkLine} from 'sentry/views/insights/common/utils/averageValueMarkLine';
- import {findSampleFromDataPoint} from 'sentry/views/insights/common/utils/findDataPoint';
- import {
- DataTitles,
- getThroughputTitle,
- } from 'sentry/views/insights/common/views/spans/types';
- import {useSampleScatterPlotSeries} from 'sentry/views/insights/common/views/spanSummaryPage/sampleList/durationChart/useSampleScatterPlotSeries';
- import {DurationChart} from 'sentry/views/insights/http/components/charts/durationChart';
- import {ResponseCodeCountChart} from 'sentry/views/insights/http/components/charts/responseCodeCountChart';
- import {SpanSamplesTable} from 'sentry/views/insights/http/components/tables/spanSamplesTable';
- import {HTTP_RESPONSE_STATUS_CODES} from 'sentry/views/insights/http/data/definitions';
- import {useSpanSamples} from 'sentry/views/insights/http/queries/useSpanSamples';
- import {Referrer} from 'sentry/views/insights/http/referrers';
- import {BASE_FILTERS} from 'sentry/views/insights/http/settings';
- import decodePanel from 'sentry/views/insights/http/utils/queryParameterDecoders/panel';
- import decodeResponseCodeClass from 'sentry/views/insights/http/utils/queryParameterDecoders/responseCodeClass';
- import {useDebouncedState} from 'sentry/views/insights/http/utils/useDebouncedState';
- import {
- ModuleName,
- SpanFunction,
- SpanIndexedField,
- SpanMetricsField,
- type SpanMetricsQueryFilters,
- } from 'sentry/views/insights/types';
- import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceMetadataHeader';
- import {useSpanFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
- export function HTTPSamplesPanel() {
- const navigate = useNavigate();
- const location = useLocation();
- const query = useLocationQuery({
- fields: {
- project: decodeScalar,
- domain: decodeScalar,
- transaction: decodeScalar,
- transactionMethod: decodeScalar,
- panel: decodePanel,
- responseCodeClass: decodeResponseCodeClass,
- spanSearchQuery: decodeScalar,
- [SpanMetricsField.USER_GEO_SUBREGION]: decodeList,
- },
- });
- const organization = useOrganization();
- const {projects} = useProjects();
- const {selection} = usePageFilters();
- const {data: supportedTags} = useSpanFieldSupportedTags();
- const project = projects.find(p => query.project === p.id);
- const [highlightedSpanId, setHighlightedSpanId] = useDebouncedState<string | undefined>(
- undefined,
- [],
- SAMPLE_HOVER_DEBOUNCE
- );
- // `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
- const detailKey = query.transaction
- ? [query.domain, query.transactionMethod, query.transaction].filter(Boolean).join(':')
- : undefined;
- const handlePanelChange = newPanelName => {
- trackAnalytics('performance_views.sample_spans.filter_updated', {
- filter: 'panel',
- new_state: newPanelName,
- organization,
- source: ModuleName.HTTP,
- });
- navigate({
- pathname: location.pathname,
- query: {
- ...location.query,
- panel: newPanelName,
- },
- });
- };
- const handleResponseCodeClassChange = newResponseCodeClass => {
- trackAnalytics('performance_views.sample_spans.filter_updated', {
- filter: 'status_code',
- new_state: newResponseCodeClass.value,
- organization,
- source: ModuleName.HTTP,
- });
- navigate({
- pathname: location.pathname,
- query: {
- ...location.query,
- responseCodeClass: newResponseCodeClass.value,
- },
- });
- };
- const isPanelOpen = Boolean(detailKey);
- const ADDITONAL_FILTERS = {
- 'span.domain':
- query.domain === '' ? EMPTY_OPTION_VALUE : escapeFilterValue(query.domain),
- transaction: query.transaction,
- ...(query[SpanMetricsField.USER_GEO_SUBREGION].length > 0
- ? {
- [SpanMetricsField.USER_GEO_SUBREGION]: `[${query[SpanMetricsField.USER_GEO_SUBREGION].join(',')}]`,
- }
- : {}),
- };
- // The ribbon is above the data selectors, and not affected by them. So, it has its own filters.
- const ribbonFilters: SpanMetricsQueryFilters = {
- ...BASE_FILTERS,
- ...ADDITONAL_FILTERS,
- ...new MutableSearch(query.spanSearchQuery).filters,
- };
- // These filters are for the charts and samples tables
- const filters: SpanMetricsQueryFilters = {
- ...BASE_FILTERS,
- ...ADDITONAL_FILTERS,
- ...new MutableSearch(query.spanSearchQuery).filters,
- };
- const responseCodeInRange = query.responseCodeClass
- ? Object.keys(HTTP_RESPONSE_STATUS_CODES).filter(code =>
- code.startsWith(query.responseCodeClass)
- )
- : [];
- if (responseCodeInRange.length > 0) {
- // TODO: Allow automatic array parameter concatenation
- filters['span.status_code'] = `[${responseCodeInRange.join(',')}]`;
- }
- const search = MutableSearch.fromQueryObject(filters);
- const {
- data: domainTransactionMetrics,
- isFetching: areDomainTransactionMetricsFetching,
- } = useSpanMetrics(
- {
- search: MutableSearch.fromQueryObject(ribbonFilters),
- fields: [
- `${SpanFunction.SPM}()`,
- `avg(${SpanMetricsField.SPAN_SELF_TIME})`,
- `sum(${SpanMetricsField.SPAN_SELF_TIME})`,
- 'http_response_rate(3)',
- 'http_response_rate(4)',
- 'http_response_rate(5)',
- `${SpanFunction.TIME_SPENT_PERCENTAGE}()`,
- ],
- enabled: isPanelOpen,
- },
- Referrer.SAMPLES_PANEL_METRICS_RIBBON
- );
- const {
- isFetching: isDurationDataFetching,
- data: durationData,
- error: durationError,
- } = useSpanMetricsSeries(
- {
- search,
- yAxis: [`avg(span.self_time)`],
- enabled: isPanelOpen && query.panel === 'duration',
- },
- Referrer.SAMPLES_PANEL_DURATION_CHART
- );
- const {
- isFetching: isResponseCodeDataLoading,
- data: responseCodeData,
- error: responseCodeError,
- } = useSpanMetricsTopNSeries({
- search,
- fields: ['span.status_code', 'count()'],
- yAxis: ['count()'],
- topEvents: 5,
- sorts: [
- {
- kind: 'desc',
- field: 'count()',
- },
- ],
- enabled: isPanelOpen && query.panel === 'status',
- referrer: Referrer.SAMPLES_PANEL_RESPONSE_CODE_CHART,
- });
- const durationAxisMax = computeAxisMax([durationData?.[`avg(span.self_time)`]]);
- const {
- data: durationSamplesData,
- isFetching: isDurationSamplesDataFetching,
- error: durationSamplesDataError,
- refetch: refetchDurationSpanSamples,
- } = useSpanSamples({
- search,
- fields: [
- SpanIndexedField.TRACE,
- SpanIndexedField.TRANSACTION_ID,
- SpanIndexedField.SPAN_DESCRIPTION,
- SpanIndexedField.RESPONSE_CODE,
- ],
- min: 0,
- max: durationAxisMax,
- enabled: isPanelOpen && query.panel === 'duration' && durationAxisMax > 0,
- referrer: Referrer.SAMPLES_PANEL_DURATION_SAMPLES,
- });
- const {
- data: responseCodeSamplesData,
- isFetching: isResponseCodeSamplesDataFetching,
- error: responseCodeSamplesDataError,
- refetch: refetchResponseCodeSpanSamples,
- } = useSpansIndexed(
- {
- search,
- fields: [
- SpanIndexedField.PROJECT,
- SpanIndexedField.TRACE,
- SpanIndexedField.TRANSACTION_ID,
- SpanIndexedField.ID,
- SpanIndexedField.TIMESTAMP,
- SpanIndexedField.SPAN_DESCRIPTION,
- SpanIndexedField.RESPONSE_CODE,
- ],
- sorts: [SPAN_SAMPLES_SORT],
- limit: SPAN_SAMPLE_LIMIT,
- enabled: isPanelOpen && query.panel === 'status',
- },
- Referrer.SAMPLES_PANEL_RESPONSE_CODE_SAMPLES
- );
- const sampledSpanDataSeries = useSampleScatterPlotSeries(
- durationSamplesData,
- domainTransactionMetrics?.[0]?.['avg(span.self_time)'],
- highlightedSpanId
- );
- const handleSearch = (newSpanSearchQuery: string) => {
- navigate({
- pathname: location.pathname,
- query: {
- ...location.query,
- spanSearchQuery: newSpanSearchQuery,
- },
- });
- if (query.panel === 'duration') {
- refetchDurationSpanSamples();
- } else {
- refetchResponseCodeSpanSamples();
- }
- };
- const handleClose = () => {
- navigate({
- pathname: location.pathname,
- query: {
- ...location.query,
- transaction: undefined,
- transactionMethod: undefined,
- },
- });
- };
- const handleOpen = useCallback(() => {
- if (query.transaction) {
- trackAnalytics('performance_views.sample_spans.opened', {
- organization,
- source: ModuleName.HTTP,
- });
- }
- }, [organization, query.transaction]);
- return (
- <PageAlertProvider>
- <DetailPanel detailKey={detailKey} onClose={handleClose} onOpen={handleOpen}>
- <ModuleLayout.Layout>
- <ModuleLayout.Full>
- <HeaderContainer>
- {project && (
- <SpanSummaryProjectAvatar
- project={project}
- direction="left"
- size={40}
- hasTooltip
- tooltip={project.slug}
- />
- )}
- <Title>
- <Link
- to={normalizeUrl(
- `/organizations/${organization.slug}/performance/summary?${qs.stringify(
- {
- project: query.project,
- transaction: query.transaction,
- }
- )}`
- )}
- >
- {query.transaction &&
- query.transactionMethod &&
- !query.transaction.startsWith(query.transactionMethod)
- ? `${query.transactionMethod} ${query.transaction}`
- : query.transaction}
- </Link>
- </Title>
- </HeaderContainer>
- </ModuleLayout.Full>
- <ModuleLayout.Full>
- <ReadoutRibbon>
- <MetricReadout
- title={getThroughputTitle('http')}
- value={domainTransactionMetrics?.[0]?.[`${SpanFunction.SPM}()`]}
- unit={RateUnit.PER_MINUTE}
- isLoading={areDomainTransactionMetricsFetching}
- />
- <MetricReadout
- title={DataTitles.avg}
- value={
- domainTransactionMetrics?.[0]?.[
- `avg(${SpanMetricsField.SPAN_SELF_TIME})`
- ]
- }
- unit={DurationUnit.MILLISECOND}
- isLoading={areDomainTransactionMetricsFetching}
- />
- <MetricReadout
- title={t('3XXs')}
- value={domainTransactionMetrics?.[0]?.[`http_response_rate(3)`]}
- unit="percentage"
- isLoading={areDomainTransactionMetricsFetching}
- />
- <MetricReadout
- title={t('4XXs')}
- value={domainTransactionMetrics?.[0]?.[`http_response_rate(4)`]}
- unit="percentage"
- isLoading={areDomainTransactionMetricsFetching}
- />
- <MetricReadout
- title={t('5XXs')}
- value={domainTransactionMetrics?.[0]?.[`http_response_rate(5)`]}
- unit="percentage"
- isLoading={areDomainTransactionMetricsFetching}
- />
- <MetricReadout
- title={DataTitles.timeSpent}
- value={domainTransactionMetrics?.[0]?.['sum(span.self_time)']}
- unit={DurationUnit.MILLISECOND}
- tooltip={getTimeSpentExplanation(
- domainTransactionMetrics?.[0]?.['time_spent_percentage()'],
- 'http.client'
- )}
- isLoading={areDomainTransactionMetricsFetching}
- />
- </ReadoutRibbon>
- </ModuleLayout.Full>
- <ModuleLayout.Full>
- <PanelControls>
- <SegmentedControl
- value={query.panel}
- onChange={handlePanelChange}
- aria-label={t('Choose breakdown type')}
- >
- <SegmentedControl.Item key="duration">
- {t('By Duration')}
- </SegmentedControl.Item>
- <SegmentedControl.Item key="status">
- {t('By Response Code')}
- </SegmentedControl.Item>
- </SegmentedControl>
- <CompactSelect
- value={query.responseCodeClass}
- options={HTTP_RESPONSE_CODE_CLASS_OPTIONS}
- onChange={handleResponseCodeClassChange}
- triggerProps={{
- prefix: t('Response Code'),
- }}
- />
- </PanelControls>
- </ModuleLayout.Full>
- {query.panel === 'duration' && (
- <Fragment>
- <ModuleLayout.Full>
- <DurationChart
- series={[
- {
- ...durationData[`avg(span.self_time)`],
- markLine: AverageValueMarkLine(),
- },
- ]}
- scatterPlot={sampledSpanDataSeries}
- onHighlight={highlights => {
- const firstHighlight = highlights[0];
- if (!firstHighlight) {
- setHighlightedSpanId(undefined);
- return;
- }
- const sample = findSampleFromDataPoint<
- (typeof durationSamplesData)[0]
- >(
- firstHighlight.dataPoint,
- durationSamplesData,
- SpanIndexedField.SPAN_SELF_TIME
- );
- setHighlightedSpanId(sample?.span_id);
- }}
- isLoading={isDurationDataFetching}
- error={durationError}
- filters={filters}
- />
- </ModuleLayout.Full>
- </Fragment>
- )}
- {query.panel === 'status' && (
- <Fragment>
- <ModuleLayout.Full>
- <ResponseCodeCountChart
- series={Object.values(responseCodeData).filter(Boolean)}
- isLoading={isResponseCodeDataLoading}
- error={responseCodeError}
- />
- </ModuleLayout.Full>
- </Fragment>
- )}
- <ModuleLayout.Full>
- {organization.features.includes('search-query-builder-performance') ? (
- <SpanSearchQueryBuilder
- projects={selection.projects}
- initialQuery={query.spanSearchQuery}
- onSearch={handleSearch}
- placeholder={t('Search for span attributes')}
- searchSource={`${ModuleName.HTTP}-sample-panel`}
- />
- ) : (
- <SearchBar
- searchSource={`${ModuleName.HTTP}-sample-panel`}
- query={query.spanSearchQuery}
- onSearch={handleSearch}
- placeholder={t('Search for span attributes')}
- organization={organization}
- supportedTags={supportedTags}
- dataset={DiscoverDatasets.SPANS_INDEXED}
- projectIds={selection.projects}
- />
- )}
- </ModuleLayout.Full>
- {query.panel === 'duration' && (
- <Fragment>
- <ModuleLayout.Full>
- <SpanSamplesTable
- data={durationSamplesData}
- isLoading={isDurationDataFetching || isDurationSamplesDataFetching}
- highlightedSpanId={highlightedSpanId}
- onSampleMouseOver={sample => setHighlightedSpanId(sample.span_id)}
- onSampleMouseOut={() => setHighlightedSpanId(undefined)}
- error={durationSamplesDataError}
- // TODO: The samples endpoint doesn't provide its own meta, so we need to create it manually
- meta={{
- fields: {
- 'span.response_code': 'number',
- },
- units: {},
- }}
- referrer={TraceViewSources.REQUESTS_MODULE}
- />
- </ModuleLayout.Full>
- <ModuleLayout.Full>
- <Button
- onClick={() => {
- trackAnalytics(
- 'performance_views.sample_spans.try_different_samples_clicked',
- {organization, source: ModuleName.HTTP}
- );
- refetchDurationSpanSamples();
- }}
- >
- {t('Try Different Samples')}
- </Button>
- </ModuleLayout.Full>
- </Fragment>
- )}
- {query.panel === 'status' && (
- <Fragment>
- <ModuleLayout.Full>
- <SpanSamplesTable
- data={responseCodeSamplesData ?? []}
- isLoading={isResponseCodeSamplesDataFetching}
- error={responseCodeSamplesDataError}
- // TODO: The samples endpoint doesn't provide its own meta, so we need to create it manually
- meta={{
- fields: {
- 'span.response_code': 'number',
- },
- units: {},
- }}
- />
- </ModuleLayout.Full>
- <ModuleLayout.Full>
- <Button
- onClick={() => {
- trackAnalytics(
- 'performance_views.sample_spans.try_different_samples_clicked',
- {organization, source: ModuleName.HTTP}
- );
- refetchResponseCodeSpanSamples();
- }}
- >
- {t('Try Different Samples')}
- </Button>
- </ModuleLayout.Full>
- </Fragment>
- )}
- </ModuleLayout.Layout>
- </DetailPanel>
- </PageAlertProvider>
- );
- }
- const SAMPLE_HOVER_DEBOUNCE = 10;
- const SPAN_SAMPLE_LIMIT = 10;
- // This is functionally a random sort, which is what we want
- const SPAN_SAMPLES_SORT = {
- field: 'span_id',
- kind: 'desc' as const,
- };
- const SpanSummaryProjectAvatar = styled(ProjectAvatar)`
- padding-right: ${space(1)};
- `;
- const HTTP_RESPONSE_CODE_CLASS_OPTIONS = [
- {
- value: '',
- label: t('All'),
- },
- {
- value: '2',
- label: t('2XXs'),
- },
- {
- value: '3',
- label: t('3XXs'),
- },
- {
- value: '4',
- label: t('4XXs'),
- },
- {
- value: '5',
- label: t('5XXs'),
- },
- ];
- // TODO - copy of static/app/views/starfish/views/spanSummaryPage/sampleList/index.tsx
- const HeaderContainer = styled('div')`
- display: grid;
- grid-template-rows: auto auto auto;
- align-items: center;
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- grid-template-rows: auto;
- grid-template-columns: auto 1fr;
- }
- `;
- const Title = styled('h4')`
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- margin: 0;
- `;
- const PanelControls = styled('div')`
- display: flex;
- justify-content: space-between;
- gap: ${space(2)};
- `;
|