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
14 changes: 11 additions & 3 deletions static/app/views/explore/hooks/useAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ export function useMetricsPanelAnalytics({
const organization = useOrganization();

const dataset = DiscoverDatasets.METRICS;
const dataScanned = metricSamplesTableResult.result.meta?.dataScanned ?? '';
const dataScanned =
mode === Mode.AGGREGATE
? (metricAggregatesTableResult.result.meta?.dataScanned ?? '')
: (metricSamplesTableResult.result.meta?.dataScanned ?? '');
const search = useQueryParamsSearch();
const query = useQueryParamsQuery();
const fields = useQueryParamsFields();
Expand All @@ -740,7 +743,11 @@ export function useMetricsPanelAnalytics({
return;
}

if (metricSamplesTableResult.result.isFetching || metricTimeseriesResult.isPending) {
if (
metricSamplesTableResult.result.isFetching ||
metricTimeseriesResult.isPending ||
!dataScanned
Copy link
Member

Choose a reason for hiding this comment

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

this check shouldn't be necessary anymore but no harm in keeping it

) {
return;
}

Expand Down Expand Up @@ -810,7 +817,8 @@ export function useMetricsPanelAnalytics({

if (
metricAggregatesTableResult.result.isPending ||
metricTimeseriesResult.isPending
metricTimeseriesResult.isPending ||
!dataScanned
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Wrong data source for dataScanned in aggregate mode

The dataScanned check in AGGREGATE mode uses data from metricSamplesTableResult, but in AGGREGATE mode the relevant metadata comes from metricAggregatesTableResult. This causes the analytics to wait for the wrong data source - it checks whether samples table has dataScanned when it should check the aggregates table instead. This can prevent analytics from firing even when aggregate data is available, or allow it to fire with mismatched data.

Additional Locations (2)

Fix in Cursor Fix in Web

) {
return;
}
Expand Down
34 changes: 34 additions & 0 deletions static/app/views/explore/metrics/metricsTab.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import {
} from 'sentry-test/reactTestingLibrary';

import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter';
import {trackAnalytics} from 'sentry/utils/analytics';
import {MetricsTabContent} from 'sentry/views/explore/metrics/metricsTab';
import {MultiMetricsQueryParamsProvider} from 'sentry/views/explore/metrics/multiMetricsQueryParams';

jest.mock('sentry/utils/analytics');

const datePageFilterProps: DatePageFilterProps = {
defaultPeriod: '7d' as const,
maxPickableDays: 7,
Expand Down Expand Up @@ -217,4 +220,35 @@ describe('MetricsTabContent', () => {
expect(within(toolbars[2]!).getByRole('button', {name: 'foo'})).toBeInTheDocument();
expect(screen.getAllByTestId('metric-panel')).toHaveLength(3);
});

it('should fire analytics for metadata', async () => {
render(
<ProviderWrapper>
<MetricsTabContent datePageFilterProps={datePageFilterProps} />
</ProviderWrapper>,
{
initialRouterConfig,
organization,
}
);

const toolbars = screen.getAllByTestId('metric-toolbar');
expect(toolbars).toHaveLength(1);

await waitFor(() => {
expect(within(toolbars[0]!).getByRole('button', {name: 'bar'})).toBeInTheDocument();
});

await waitFor(() => {
expect(trackAnalytics).toHaveBeenCalledWith(
'metrics.explorer.metadata',
expect.objectContaining({
organization,
metric_queries_count: 1,
})
);
});

expect(trackAnalytics).toHaveBeenCalledTimes(1);
});
});
Loading