Skip to content

Conversation

@k-fish
Copy link
Member

@k-fish k-fish commented Dec 6, 2025

This elides metric counts when the metric name is empty, and improves testing around metric panel etc.

This elides metric counts when the metric name is empty, and improves
testing around metric panel etc.
@k-fish k-fish requested a review from a team as a code owner December 6, 2025 01:30
@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Dec 6, 2025
Comment on lines 912 to 914
const metricPanelsWithGroupBys = metricQueries
.filter(mq => !isEmptyTraceMetric(mq.metric))
.map(mq => mq.queryParams.groupBys.some((gb: string) => gb.trim().length > 0)).length;

This comment was marked as outdated.

table_result_length: resultLengthBox.current,
table_result_missing_root: 0,
table_result_mode: 'metric samples',
table_result_mode: resultMode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Aggregate mode reports wrong table result length

The getAttributes function always uses resultLengthBox.current (samples table length) for table_result_length, regardless of whether resultMode is 'aggregates' or 'metric samples'. When in aggregate mode, it should use aggregatesResultLengthBox.current instead. This causes incorrect analytics data to be reported for the aggregates table result length.

Fix in Cursor Fix in Web

table_result_length: resultLengthBox.current,
table_result_missing_root: 0,
table_result_mode: 'metric samples',
table_result_mode: resultMode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Aggregate mode reports wrong table result sort

The getAttributes function always uses formattedSortBysBox.current (samples sort) for table_result_sort, regardless of whether resultMode is 'aggregates' or 'metric samples'. When in aggregate mode, it should use formattedAggregateSortBysBox.current instead. The formattedAggregateSortBysBox is defined and included in the aggregates useEffect dependency array but is never actually used in the analytics payload.

Fix in Cursor Fix in Web

Comment on lines 912 to 917
const metricPanelsWithGroupBys = metricQueries
.filter(mq => !isEmptyTraceMetric(mq.metric))
.map(mq => mq.queryParams.groupBys.some((gb: string) => gb.trim().length > 0)).length;
const metricPanelsWithFilters = metricQueries
.filter(mq => !isEmptyTraceMetric(mq.metric))
.map(mq => mq.queryParams.query.trim().length > 0).length;
Copy link

Choose a reason for hiding this comment

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

Bug: metricPanelsWithGroupBys and metricPanelsWithFilters incorrectly count all non-empty metrics due to .map().length instead of filtering for specific conditions.
Severity: MEDIUM | Confidence: High

🔍 Detailed Analysis

The metricPanelsWithGroupBys and metricPanelsWithFilters variables are intended to count the number of metricQueries that satisfy specific conditions (having group-bys or filters) after filtering out empty trace metrics. However, the current implementation uses .map() to transform the filtered metricQueries into an array of booleans, and then .length is called on this array. This incorrectly counts the total number of non-empty metricQueries (the length of the boolean array) instead of counting only those metricQueries for which the boolean condition was true. For example, if there are two non-empty metrics but only one has group-bys, the code will return 2 instead of 1, leading to an overcounting of these panel types.

💡 Suggested Fix

Replace the .map().length pattern with a single .filter() call that combines the initial non-empty metric check with the specific condition. For example: metricQueries.filter(mq => !isEmptyTraceMetric(mq.metric) && mq.queryParams.groupBys.some((gb: string) => gb.trim().length > 0)).length;

🤖 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/hooks/useAnalytics.tsx#L912-L917

Potential issue: The `metricPanelsWithGroupBys` and `metricPanelsWithFilters` variables
are intended to count the number of `metricQueries` that satisfy specific conditions
(having group-bys or filters) after filtering out empty trace metrics. However, the
current implementation uses `.map()` to transform the filtered `metricQueries` into an
array of booleans, and then `.length` is called on this array. This incorrectly counts
the total number of non-empty `metricQueries` (the length of the boolean array) instead
of counting only those `metricQueries` for which the boolean condition was `true`. For
example, if there are two non-empty metrics but only one has group-bys, the code will
return `2` instead of `1`, leading to an overcounting of these panel types.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 6298179

metric_queries_count: ${String(metricQueries.length)}
metric_panels_with_group_bys_count: ${String(metricPanelsWithGroupBysCount.current)}
metric_panels_with_filters_count: ${String(metricPanelsWithFiltersCount.current)}
metric_panels_with_group_bys_count: ${String(metricPanelsWithGroupBys)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Info log uses total count, analytics uses filtered count

The trackAnalytics call uses nonEmptyMetricQueries.length for metric_queries_count, but the info log uses metricQueries.length. This inconsistency means the analytics event and the info log report different values for the same metric. Based on the PR's intent to elide empty metrics, the info call likely needs to use nonEmptyMetricQueries.length as well.

Additional Locations (1)

Fix in Cursor Fix in Web

@k-fish k-fish merged commit 905a502 into master Dec 8, 2025
48 checks passed
@k-fish k-fish deleted the fix/tracemetrics/yet-more-analytics branch December 8, 2025 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants