123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- import {Fragment, useCallback, useMemo} from 'react';
- import styled from '@emotion/styled';
- import type {Location} from 'history';
- import omit from 'lodash/omit';
- import {CompactSelect} from 'sentry/components/compactSelect';
- import SearchBar from 'sentry/components/events/searchBar';
- import * as Layout from 'sentry/components/layouts/thirds';
- import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
- import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
- import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import Pagination from 'sentry/components/pagination';
- import {SpanSearchQueryBuilder} from 'sentry/components/performance/spanSearchQueryBuilder';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {Organization} from 'sentry/types/organization';
- import {defined} from 'sentry/utils';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import {browserHistory} from 'sentry/utils/browserHistory';
- import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
- import type EventView from 'sentry/utils/discover/eventView';
- import {DiscoverDatasets} from 'sentry/utils/discover/types';
- import SuspectSpansQuery from 'sentry/utils/performance/suspectSpans/suspectSpansQuery';
- import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
- import {decodeScalar} from 'sentry/utils/queryString';
- import useProjects from 'sentry/utils/useProjects';
- import SpanMetricsTable from 'sentry/views/performance/transactionSummary/transactionSpans/spanMetricsTable';
- import {useSpanMetricsFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
- import type {SetStateAction} from '../types';
- import OpsFilter from './opsFilter';
- import SuspectSpansTable from './suspectSpansTable';
- import type {SpanSort, SpansTotalValues} from './types';
- import {
- getSuspectSpanSortFromEventView,
- getTotalsView,
- SPAN_RELATIVE_PERIODS,
- SPAN_RETENTION_DAYS,
- SPAN_SORT_OPTIONS,
- SPAN_SORT_TO_FIELDS,
- } from './utils';
- const ANALYTICS_VALUES = {
- spanOp: (organization: Organization, value: string | undefined) =>
- trackAnalytics('performance_views.spans.change_op', {
- organization,
- operation_name: value,
- }),
- sort: (organization: Organization, value: string | undefined) =>
- trackAnalytics('performance_views.spans.change_sort', {
- organization,
- sort_column: value,
- }),
- };
- type Props = {
- eventView: EventView;
- location: Location;
- organization: Organization;
- projectId: string;
- setError: SetStateAction<string | undefined>;
- transactionName: string;
- };
- function SpansContent(props: Props) {
- const {location, organization, eventView, projectId, transactionName} = props;
- const query = decodeScalar(location.query.query, '');
- const handleChange = useCallback(
- (key: string) => {
- return function (value: string | undefined) {
- ANALYTICS_VALUES[key]?.(organization, value);
- const queryParams = normalizeDateTimeParams({
- ...(location.query || {}),
- [key]: value,
- });
- // do not propagate pagination when making a new search
- const toOmit = ['cursor'];
- if (!defined(value)) {
- toOmit.push(key);
- }
- const searchQueryParams = omit(queryParams, toOmit);
- browserHistory.push({
- ...location,
- query: searchQueryParams,
- });
- };
- },
- [location, organization]
- );
- const spanOp = decodeScalar(location.query.spanOp);
- const spanGroup = decodeScalar(location.query.spanGroup);
- const sort = getSuspectSpanSortFromEventView(eventView);
- const spansView = getSpansEventView(eventView, sort.field);
- const totalsView = getTotalsView(eventView);
- const {projects} = useProjects();
- const onSearch = useMemo(() => handleChange('query'), [handleChange]);
- const projectIds = useMemo(() => eventView.project.slice(), [eventView]);
- const hasNewSpansUIFlag =
- organization.features.includes('performance-spans-new-ui') &&
- organization.features.includes('insights-initial-modules');
- // TODO: Remove this flag when the feature is GA'd and replace the old content entirely
- if (hasNewSpansUIFlag) {
- return <SpansContentV2 {...props} />;
- }
- return (
- <Layout.Main fullWidth>
- <FilterActions>
- <OpsFilter
- location={location}
- eventView={eventView}
- organization={organization}
- handleOpChange={handleChange('spanOp')}
- transactionName={transactionName}
- />
- <PageFilterBar condensed>
- <EnvironmentPageFilter />
- <DatePageFilter
- maxPickableDays={SPAN_RETENTION_DAYS}
- relativeOptions={SPAN_RELATIVE_PERIODS}
- />
- </PageFilterBar>
- <StyledSearchBarWrapper>
- {organization.features.includes('search-query-builder-performance') ? (
- <SpanSearchQueryBuilder
- projects={projectIds}
- initialQuery={query}
- onSearch={onSearch}
- searchSource="transaction_spans"
- />
- ) : (
- <SearchBar
- organization={organization}
- projectIds={eventView.project}
- query={query}
- fields={eventView.fields}
- onSearch={onSearch}
- />
- )}
- <CompactSelect
- value={sort.field}
- options={SPAN_SORT_OPTIONS.map(opt => ({value: opt.field, label: opt.label}))}
- onChange={opt => handleChange('sort')(opt.value)}
- triggerProps={{prefix: sort.prefix}}
- triggerLabel={sort.label}
- />
- </StyledSearchBarWrapper>
- </FilterActions>
- <DiscoverQuery
- eventView={totalsView}
- orgSlug={organization.slug}
- location={location}
- referrer="api.performance.transaction-spans"
- cursor="0:0:1"
- noPagination
- >
- {({tableData}) => {
- const totals: SpansTotalValues | null =
- (tableData?.data?.[0] as SpansTotalValues | undefined) ?? null;
- return (
- <SuspectSpansQuery
- location={location}
- orgSlug={organization.slug}
- eventView={spansView}
- limit={10}
- perSuspect={0}
- spanOps={defined(spanOp) ? [spanOp] : []}
- spanGroups={defined(spanGroup) ? [spanGroup] : []}
- >
- {({suspectSpans, isLoading, pageLinks}) => (
- <Fragment>
- <VisuallyCompleteWithData
- id="TransactionSpans-SuspectSpansTable"
- hasData={!!suspectSpans?.length}
- isLoading={isLoading}
- >
- <SuspectSpansTable
- location={location}
- organization={organization}
- transactionName={transactionName}
- project={projects.find(p => p.id === projectId)}
- isLoading={isLoading}
- suspectSpans={suspectSpans ?? []}
- totals={totals}
- sort={sort.field}
- />
- </VisuallyCompleteWithData>
- <Pagination pageLinks={pageLinks ?? null} />
- </Fragment>
- )}
- </SuspectSpansQuery>
- );
- }}
- </DiscoverQuery>
- </Layout.Main>
- );
- }
- // TODO: Temporary component while we make the switch to spans only. Will fully replace the old Spans tab when GA'd
- function SpansContentV2(props: Props) {
- const {location, organization, eventView, projectId, transactionName} = props;
- const supportedTags = useSpanMetricsFieldSupportedTags();
- const {projects} = useProjects();
- const project = projects.find(p => p.id === projectId);
- const spansQuery = decodeScalar(location.query.spansQuery, '');
- const handleChange = useCallback(
- (key: string) => {
- return function (value: string | undefined) {
- ANALYTICS_VALUES[key]?.(organization, value);
- const queryParams = normalizeDateTimeParams({
- ...(location.query || {}),
- [key]: value,
- });
- // do not propagate pagination when making a new search
- const toOmit = ['cursor'];
- if (!defined(value)) {
- toOmit.push(key);
- }
- const searchQueryParams = omit(queryParams, toOmit);
- browserHistory.push({
- ...location,
- query: searchQueryParams,
- });
- };
- },
- [location, organization]
- );
- const onSearch = useMemo(() => handleChange('spansQuery'), [handleChange]);
- const projectIds = useMemo(() => eventView.project.slice(), [eventView]);
- return (
- <Layout.Main fullWidth>
- <FilterActions>
- <OpsFilter
- location={location}
- eventView={eventView}
- organization={organization}
- handleOpChange={handleChange('spanOp')}
- transactionName={transactionName}
- />
- <PageFilterBar condensed>
- <EnvironmentPageFilter />
- <DatePageFilter />
- </PageFilterBar>
- {organization.features.includes('search-query-builder-performance') ? (
- <SpanSearchQueryBuilder
- projects={projectIds}
- initialQuery={spansQuery}
- onSearch={onSearch}
- searchSource="transaction_spans"
- />
- ) : (
- <SearchBar
- organization={organization}
- projectIds={eventView.project}
- query={spansQuery}
- fields={eventView.fields}
- placeholder={t('Search for span attributes')}
- supportedTags={supportedTags}
- // This dataset is separate from the query itself which is on metrics; it's for obtaining autocomplete recommendations
- dataset={DiscoverDatasets.SPANS_INDEXED}
- onSearch={handleChange('spansQuery')}
- />
- )}
- </FilterActions>
- <SpanMetricsTable
- project={project}
- transactionName={transactionName}
- query={spansQuery ?? ''}
- />
- </Layout.Main>
- );
- }
- function getSpansEventView(eventView: EventView, sort: SpanSort): EventView {
- eventView = eventView.clone();
- const fields = SPAN_SORT_TO_FIELDS[sort];
- eventView.fields = fields ? fields.map(field => ({field})) : [];
- return eventView;
- }
- const FilterActions = styled('div')`
- display: grid;
- gap: ${space(2)};
- margin-bottom: ${space(2)};
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- grid-template-columns: repeat(3, min-content);
- }
- @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
- grid-template-columns: auto auto 1fr auto;
- }
- `;
- const StyledSearchBarWrapper = styled('div')`
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- order: 1;
- grid-column: 1/5;
- }
- @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
- order: initial;
- grid-column: auto;
- }
- `;
- export default SpansContent;
|