Skip to content

Commit 4b341f6

Browse files
authored
fix(tracemetrics): Wait for datascanned (#103993)
### Summary This will not fire an analytics unless datascanned is provided.
1 parent 147bec1 commit 4b341f6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

static/app/views/explore/hooks/useAnalytics.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,10 @@ export function useMetricsPanelAnalytics({
713713
const organization = useOrganization();
714714

715715
const dataset = DiscoverDatasets.METRICS;
716-
const dataScanned = metricSamplesTableResult.result.meta?.dataScanned ?? '';
716+
const dataScanned =
717+
mode === Mode.AGGREGATE
718+
? (metricAggregatesTableResult.result.meta?.dataScanned ?? '')
719+
: (metricSamplesTableResult.result.meta?.dataScanned ?? '');
717720
const search = useQueryParamsSearch();
718721
const query = useQueryParamsQuery();
719722
const fields = useQueryParamsFields();
@@ -740,7 +743,11 @@ export function useMetricsPanelAnalytics({
740743
return;
741744
}
742745

743-
if (metricSamplesTableResult.result.isFetching || metricTimeseriesResult.isPending) {
746+
if (
747+
metricSamplesTableResult.result.isFetching ||
748+
metricTimeseriesResult.isPending ||
749+
!dataScanned
750+
) {
744751
return;
745752
}
746753

@@ -810,7 +817,8 @@ export function useMetricsPanelAnalytics({
810817

811818
if (
812819
metricAggregatesTableResult.result.isPending ||
813-
metricTimeseriesResult.isPending
820+
metricTimeseriesResult.isPending ||
821+
!dataScanned
814822
) {
815823
return;
816824
}

static/app/views/explore/metrics/metricsTab.spec.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import {
1010
} from 'sentry-test/reactTestingLibrary';
1111

1212
import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter';
13+
import {trackAnalytics} from 'sentry/utils/analytics';
1314
import {MetricsTabContent} from 'sentry/views/explore/metrics/metricsTab';
1415
import {MultiMetricsQueryParamsProvider} from 'sentry/views/explore/metrics/multiMetricsQueryParams';
1516

17+
jest.mock('sentry/utils/analytics');
18+
1619
const datePageFilterProps: DatePageFilterProps = {
1720
defaultPeriod: '7d' as const,
1821
maxPickableDays: 7,
@@ -217,4 +220,35 @@ describe('MetricsTabContent', () => {
217220
expect(within(toolbars[2]!).getByRole('button', {name: 'foo'})).toBeInTheDocument();
218221
expect(screen.getAllByTestId('metric-panel')).toHaveLength(3);
219222
});
223+
224+
it('should fire analytics for metadata', async () => {
225+
render(
226+
<ProviderWrapper>
227+
<MetricsTabContent datePageFilterProps={datePageFilterProps} />
228+
</ProviderWrapper>,
229+
{
230+
initialRouterConfig,
231+
organization,
232+
}
233+
);
234+
235+
const toolbars = screen.getAllByTestId('metric-toolbar');
236+
expect(toolbars).toHaveLength(1);
237+
238+
await waitFor(() => {
239+
expect(within(toolbars[0]!).getByRole('button', {name: 'bar'})).toBeInTheDocument();
240+
});
241+
242+
await waitFor(() => {
243+
expect(trackAnalytics).toHaveBeenCalledWith(
244+
'metrics.explorer.metadata',
245+
expect.objectContaining({
246+
organization,
247+
metric_queries_count: 1,
248+
})
249+
);
250+
});
251+
252+
expect(trackAnalytics).toHaveBeenCalledTimes(1);
253+
});
220254
});

0 commit comments

Comments
 (0)