Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions static/app/views/explore/hooks/useProgressiveQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {CaseInsensitive} from 'sentry/components/searchQueryBuilder/hooks';
import type {TraceMetric} from 'sentry/views/explore/metrics/metricQuery';

export const SAMPLING_MODE = {
NORMAL: 'NORMAL',
Expand All @@ -25,7 +24,6 @@ export type RPCQueryExtras = {
caseInsensitive?: CaseInsensitive;
disableAggregateExtrapolation?: string;
samplingMode?: SamplingMode;
traceMetric?: TraceMetric;
};

interface ProgressiveQueryOptions<TQueryFn extends (...args: any[]) => any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ export function useMetricAggregatesTable({
enabled,
limit,
traceMetric,
queryExtras: {
...queryExtras,
traceMetric,
},
queryExtras,
},
queryOptions: {
canTriggerHighAccuracy,
Comment on lines 66 to 72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: useMetricAggregatesTable.tsx fails to add traceMetric filters (metric.name, metric.type) to the search query, leading to unfiltered aggregate results.
Severity: CRITICAL | Confidence: 0.95

🔍 Detailed Analysis

In useMetricAggregatesTable.tsx, the traceMetric parameters were removed from queryExtras, but the corresponding metric filters (TraceMetricKnownFieldKey.METRIC_NAME, TraceMetricKnownFieldKey.METRIC_TYPE) were not added to the search query. The backend expects explicit query parameters for metric filtering, not just parsing from aggregation expressions. Consequently, the aggregates table will return data for all metrics instead of filtering to the selected traceMetric, leading to incorrect and misleading results.

💡 Suggested Fix

Update useMetricAggregatesTable.tsx to add traceMetric.name and traceMetric.type as filters to the MutableSearch object when traceMetric is present, mirroring the implementation in useMetricSamplesTable.tsx.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: static/app/views/explore/metrics/hooks/useMetricAggregatesTable.tsx#L66-L72

Potential issue: In `useMetricAggregatesTable.tsx`, the `traceMetric` parameters were
removed from `queryExtras`, but the corresponding metric filters
(`TraceMetricKnownFieldKey.METRIC_NAME`, `TraceMetricKnownFieldKey.METRIC_TYPE`) were
not added to the search query. The backend expects explicit query parameters for metric
filtering, not just parsing from aggregation expressions. Consequently, the aggregates
table will return data for all metrics instead of filtering to the selected
`traceMetric`, leading to incorrect and misleading results.

Did we get this right? 👍 / 👎 to inform future reviews.

Reference_id: 2694881

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('useMetricSamplesTable', () => {
'/organizations/org-slug/events/',
expect.objectContaining({
query: expect.objectContaining({
query: '',
query: 'metric.name:test.metric metric.type:counter',
caseInsensitive: undefined,
dataset: 'tracemetrics',
disableAggregateExtrapolation: undefined,
Expand All @@ -150,8 +150,6 @@ describe('useMetricSamplesTable', () => {
referrer: 'api.explore.metric-samples-table',
sampling: SAMPLING_MODE.NORMAL,
sort: '-timestamp',
metricName: 'test.metric',
metricType: 'counter',
}),
})
);
Expand Down
35 changes: 16 additions & 19 deletions static/app/views/explore/metrics/hooks/useMetricSamplesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import {
useMetricsFrozenSearch,
useMetricsFrozenTracePeriod,
} from 'sentry/views/explore/metrics/metricsFrozenContext';
import {type TraceMetricEventsResponseItem} from 'sentry/views/explore/metrics/types';
import {
useQueryParamsQuery,
TraceMetricKnownFieldKey,
type TraceMetricEventsResponseItem,
} from 'sentry/views/explore/metrics/types';
import {
useQueryParamsSearch,
useQueryParamsSortBys,
} from 'sentry/views/explore/queryParams/context';
import {getEventView} from 'sentry/views/insights/common/queries/useDiscover';
Expand Down Expand Up @@ -74,7 +77,7 @@ function useMetricsQueryKey({
traceMetric?: TraceMetric;
}) {
const organization = useOrganization();
const query = useQueryParamsQuery();
const userSearch = useQueryParamsSearch();
const frozenSearch = useMetricsFrozenSearch();
const frozenTracePeriod = useMetricsFrozenTracePeriod();
const sortBys = useQueryParamsSortBys();
Expand All @@ -86,20 +89,19 @@ function useMetricsQueryKey({
[fields]
);
const queryString = useMemo(() => {
const queryStr = query;
const frozenSearchStr = frozenSearch?.formatString() ?? '';

const parts = [frozenSearchStr, queryStr].filter(Boolean);
const newSearch = userSearch.copy();

if (parts.length === 0) {
return '';
if (frozenSearch) {
newSearch.tokens.push(...frozenSearch.tokens);
}
if (parts.length === 1) {
return parts[0];

if (traceMetric) {
newSearch.addFilterValue(TraceMetricKnownFieldKey.METRIC_NAME, traceMetric.name);
newSearch.addFilterValue(TraceMetricKnownFieldKey.METRIC_TYPE, traceMetric.type);
}

return parts.join(' ');
}, [query, frozenSearch]);
return newSearch.formatString();
}, [userSearch, frozenSearch, traceMetric]);

const adjustedDatetime = useMemo(() => {
const baseDatetime = frozenTracePeriod
Expand Down Expand Up @@ -158,8 +160,6 @@ function useMetricsQueryKey({
orderby: orderby.length > 0 ? orderby : undefined,
per_page: limit,
referrer,
metricName: traceMetric?.name,
metricType: traceMetric?.type,
sampling: queryExtras?.samplingMode ?? SAMPLING_MODE.NORMAL,
caseInsensitive: queryExtras?.caseInsensitive,
disableAggregateExtrapolation: queryExtras?.disableAggregateExtrapolation
Expand Down Expand Up @@ -206,10 +206,7 @@ export function useMetricSamplesTable({
traceMetric,
fields,
ingestionDelaySeconds,
queryExtras: {
...queryExtras,
traceMetric,
},
queryExtras,
},
queryOptions: {
canTriggerHighAccuracy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function useMetricTimeseriesImpl({
topEvents,
orderby: sortBys.map(formatSort),
...queryExtras,
traceMetric,
},
'api.explore.metrics-stats',
DiscoverDatasets.TRACEMETRICS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import type {
TimeSeriesItem,
} from 'sentry/views/dashboards/widgets/common/types';
import type {SamplingMode} from 'sentry/views/explore/hooks/useProgressiveQuery';
import type {TraceMetric} from 'sentry/views/explore/metrics/metricQuery';
import {FALLBACK_SERIES_NAME} from 'sentry/views/explore/settings';
import {getSeriesEventView} from 'sentry/views/insights/common/queries/getSeriesEventView';
import {
Expand All @@ -64,7 +63,6 @@ interface Options<Fields> {
samplingMode?: SamplingMode;
search?: MutableSearch;
topEvents?: number;
traceMetric?: TraceMetric;
yAxis?: Fields;
}

Expand All @@ -89,7 +87,6 @@ export const useSortedTimeSeries = <
samplingMode,
disableAggregateExtrapolation,
caseInsensitive,
traceMetric,
} = options;

const pageFilters = usePageFilters();
Expand Down Expand Up @@ -166,8 +163,6 @@ export const useSortedTimeSeries = <
// pagination does not cause extra requests
cursor: undefined,
caseInsensitive,
metricName: traceMetric?.name ? traceMetric.name : undefined,
metricType: traceMetric?.type ? traceMetric.type : undefined,
}),
options: {
enabled: enabled && pageFilters.isReady,
Expand Down
9 changes: 0 additions & 9 deletions static/app/views/insights/common/queries/useSpansQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
RPCQueryExtras,
SamplingMode,
} from 'sentry/views/explore/hooks/useProgressiveQuery';
import type {TraceMetric} from 'sentry/views/explore/metrics/metricQuery';
import {
getRetryDelay,
shouldRetryHandler,
Expand Down Expand Up @@ -113,7 +112,6 @@ function useSpansQueryBase<T>({
caseInsensitive: queryExtras?.caseInsensitive,
samplingMode: queryExtras?.samplingMode,
disableAggregateExtrapolation: queryExtras?.disableAggregateExtrapolation,
traceMetric: queryExtras?.traceMetric,
});

if (trackResponseAnalytics) {
Expand Down Expand Up @@ -245,7 +243,6 @@ type WrappedDiscoverQueryProps<T> = {
referrer?: string;
refetchInterval?: number;
samplingMode?: SamplingMode;
traceMetric?: TraceMetric;
};

function useWrappedDiscoverQueryBase<T>({
Expand All @@ -264,7 +261,6 @@ function useWrappedDiscoverQueryBase<T>({
additionalQueryKey,
refetchInterval,
caseInsensitive,
traceMetric,
}: WrappedDiscoverQueryProps<T> & {
pageFiltersReady: boolean;
}) {
Expand Down Expand Up @@ -294,11 +290,6 @@ function useWrappedDiscoverQueryBase<T>({
queryExtras.allowAggregateConditions = allowAggregateConditions ? '1' : '0';
}

if (traceMetric?.name && traceMetric?.type) {
queryExtras.metricName = traceMetric.name;
queryExtras.metricType = traceMetric.type;
}

const result = useDiscoverQuery({
eventView,
orgSlug: organization.slug,
Expand Down
Loading