SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(infra-monitoring): request loop when click to visualize volume (#10657)
Vinicius Lourençoa9cbf9a4df62
diff --git a/frontend/src/container/InfraMonitoringK8s/Volumes/K8sVolumesList.tsx b/frontend/src/container/InfraMonitoringK8s/Volumes/K8sVolumesList.tsxindex 16945b48aa..9fb6dcc285 100644--- a/frontend/src/container/InfraMonitoringK8s/Volumes/K8sVolumesList.tsx+++ b/frontend/src/container/InfraMonitoringK8s/Volumes/K8sVolumesList.tsx@@ -48,6 +48,8 @@ import {formatDataForTable,getK8sVolumesListColumns,getK8sVolumesListQuery,+ getVolumeListGroupedByRowDataQueryKey,+ getVolumesListQueryKey,K8sVolumesRowData,} from './utils';import VolumeDetails from './VolumeDetails';@@ -167,6 +169,26 @@ function K8sVolumesList({// eslint-disable-next-line react-hooks/exhaustive-deps}, [minTime, maxTime, orderBy, selectedRowData, groupBy]);+ const groupedByRowDataQueryKey = useMemo(+ () =>+ getVolumeListGroupedByRowDataQueryKey(+ selectedRowData?.groupedByMeta,+ queryFilters,+ orderBy,+ groupBy,+ minTime,+ maxTime,+ ),+ [+ selectedRowData?.groupedByMeta,+ queryFilters,+ orderBy,+ groupBy,+ minTime,+ maxTime,+ ],+ );+const {data: groupedByRowData,isFetching: isFetchingGroupedByRowData,@@ -176,7 +198,7 @@ function K8sVolumesList({} = useGetK8sVolumesList(fetchGroupedByRowDataQuery as K8sVolumesListPayload,{- queryKey: ['volumeList', fetchGroupedByRowDataQuery],+ queryKey: groupedByRowDataQueryKey,enabled: !!fetchGroupedByRowDataQuery && !!selectedRowData,},undefined,@@ -221,6 +243,28 @@ function K8sVolumesList({return queryPayload;}, [pageSize, currentPage, queryFilters, minTime, maxTime, orderBy, groupBy]);+ const volumesListQueryKey = useMemo(() => {+ return getVolumesListQueryKey(+ selectedVolumeUID,+ pageSize,+ currentPage,+ queryFilters,+ orderBy,+ groupBy,+ minTime,+ maxTime,+ );+ }, [+ selectedVolumeUID,+ pageSize,+ currentPage,+ queryFilters,+ groupBy,+ orderBy,+ minTime,+ maxTime,+ ]);+const formattedGroupedByVolumesData = useMemo(() =>formatDataForTable(groupedByRowData?.payload?.data?.records || [], groupBy),@@ -237,7 +281,7 @@ function K8sVolumesList({const { data, isFetching, isLoading, isError } = useGetK8sVolumesList(query as K8sVolumesListPayload,{- queryKey: ['volumeList', query],+ queryKey: volumesListQueryKey,enabled: !!query,},undefined,diff --git a/frontend/src/container/InfraMonitoringK8s/Volumes/utils.tsx b/frontend/src/container/InfraMonitoringK8s/Volumes/utils.tsxindex 7beabb1497..c4bf62bff3 100644--- a/frontend/src/container/InfraMonitoringK8s/Volumes/utils.tsx+++ b/frontend/src/container/InfraMonitoringK8s/Volumes/utils.tsx@@ -77,6 +77,74 @@ export const getK8sVolumesListQuery = (): K8sVolumesListPayload => ({orderBy: { columnName: 'usage', order: 'desc' },});+export const getVolumeListGroupedByRowDataQueryKey = (+ groupedByMeta: K8sVolumesData['meta'] | undefined,+ queryFilters: IBuilderQuery['filters'],+ orderBy: { columnName: string; order: 'asc' | 'desc' } | null,+ groupBy: IBuilderQuery['groupBy'],+ minTime: number,+ maxTime: number,+): (string | undefined)[] => {+ // When we have grouped by metadata defined+ // We need to leave out the min/max time+ // Otherwise it will cause a loop+ const groupedByMetaStr = JSON.stringify(groupedByMeta || undefined) ?? '';+ if (groupedByMetaStr) {+ return [+ 'volumeList',+ JSON.stringify(queryFilters),+ JSON.stringify(orderBy),+ JSON.stringify(groupBy),+ groupedByMetaStr,+ ];+ }+ return [+ 'volumeList',+ JSON.stringify(queryFilters),+ JSON.stringify(orderBy),+ JSON.stringify(groupBy),+ groupedByMetaStr,+ String(minTime),+ String(maxTime),+ ];+};++export const getVolumesListQueryKey = (+ selectedVolumeUID: string | null,+ pageSize: number,+ currentPage: number,+ queryFilters: IBuilderQuery['filters'],+ orderBy: { columnName: string; order: 'asc' | 'desc' } | null,+ groupBy: IBuilderQuery['groupBy'],+ minTime: number,+ maxTime: number,+): (string | undefined)[] => {+ // When selected volume is defined+ // We need to leave out the min/max time+ // Otherwise it will cause a loop+ if (selectedVolumeUID) {+ return [+ 'volumeList',+ String(pageSize),+ String(currentPage),+ JSON.stringify(queryFilters),+ JSON.stringify(orderBy),+ JSON.stringify(groupBy),+ ];+ }++ return [+ 'volumeList',+ String(pageSize),+ String(currentPage),+ JSON.stringify(queryFilters),+ JSON.stringify(orderBy),+ JSON.stringify(groupBy),+ String(minTime),+ String(maxTime),+ ];+};+const columnsConfig = [{title: <div className="column-header-left pvc-name-header">PVC Name</div>,diff --git a/frontend/src/container/InfraMonitoringK8s/__tests__/Volumes/K8sVolumesList.test.tsx b/frontend/src/container/InfraMonitoringK8s/__tests__/Volumes/K8sVolumesList.test.tsxindex d2d2e1f1a6..013784401c 100644--- a/frontend/src/container/InfraMonitoringK8s/__tests__/Volumes/K8sVolumesList.test.tsx+++ b/frontend/src/container/InfraMonitoringK8s/__tests__/Volumes/K8sVolumesList.test.tsx@@ -1,29 +1,93 @@-import setupCommonMocks from '../commonMocks';--setupCommonMocks();-import { QueryClient, QueryClientProvider } from 'react-query';// eslint-disable-next-line no-restricted-imports-import { MemoryRouter } from 'react-router-dom';-import { render, waitFor } from '@testing-library/react';+import { MemoryRouter } from 'react-router-dom-v5-compat';import { FeatureKeys } from 'constants/features';import K8sVolumesList from 'container/InfraMonitoringK8s/Volumes/K8sVolumesList';import { rest, server } from 'mocks-server/server';import { NuqsTestingAdapter } from 'nuqs/adapters/testing';import { IAppContext, IUser } from 'providers/App/types';+import { QueryBuilderProvider } from 'providers/QueryBuilder';+// eslint-disable-next-line no-restricted-imports+import { applyMiddleware, legacy_createStore as createStore } from 'redux';+import thunk from 'redux-thunk';+import reducers from 'store/reducers';+import { act, render, screen, userEvent, waitFor } from 'tests/test-utils';+import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime';import { LicenseResModel } from 'types/api/licensesV3/getActive';-const queryClient = new QueryClient({- defaultOptions: {- queries: {- retry: false,- cacheTime: 0,- },- },-});+import { INFRA_MONITORING_K8S_PARAMS_KEYS } from '../../constants';const SERVER_URL = 'http://localhost/api';+// jsdom does not implement IntersectionObserver — provide a no-op stub+const mockObserver = {+ observe: jest.fn(),+ unobserve: jest.fn(),+ disconnect: jest.fn(),+};+global.IntersectionObserver = jest+ .fn()+ .mockImplementation(() => mockObserver) as any;++const mockVolume = {+ persistentVolumeClaimName: 'test-pvc',+ volumeAvailable: 1000000,+ volumeCapacity: 5000000,+ volumeInodes: 100,+ volumeInodesFree: 50,+ volumeInodesUsed: 50,+ volumeUsage: 4000000,+ meta: {+ k8s_cluster_name: 'test-cluster',+ k8s_namespace_name: 'test-namespace',+ k8s_node_name: 'test-node',+ k8s_persistentvolumeclaim_name: 'test-pvc',+ k8s_pod_name: 'test-pod',+ k8s_pod_uid: 'test-pod-uid',+ k8s_statefulset_name: '',+ },+};++const mockVolumesResponse = {+ status: 'success',+ data: {+ type: '',+ records: [mockVolume],+ groups: null,+ total: 1,+ sentAnyHostMetricsData: false,+ isSendingK8SAgentMetrics: false,+ },+};++/** Renders K8sVolumesList with a real Redux store so dispatched actions affect state. */+function renderWithRealStore(+ initialEntries?: Record<string, any>,+): { testStore: ReturnType<typeof createStore> } {+ const testStore = createStore(reducers, applyMiddleware(thunk as any));+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });++ render(+ <NuqsTestingAdapter searchParams={initialEntries}>+ <QueryClientProvider client={queryClient}>+ <QueryBuilderProvider>+ <MemoryRouter>+ <K8sVolumesList+ isFiltersVisible={false}+ handleFilterVisibilityChange={jest.fn()}+ quickFiltersLastUpdated={-1}+ />+ </MemoryRouter>+ </QueryBuilderProvider>+ </QueryClientProvider>+ </NuqsTestingAdapter>,+ );++ return { testStore };+}+describe('K8sVolumesList - useGetAggregateKeys Category Regression', () => {let requestsMade: Array<{url: string;@@ -33,7 +97,6 @@ describe('K8sVolumesList - useGetAggregateKeys Category Regression', () => {beforeEach(() => {requestsMade = [];- queryClient.clear();server.use(rest.get(`${SERVER_URL}/v3/autocomplete/attribute_keys`, (req, res, ctx) => {@@ -79,19 +142,7 @@ describe('K8sVolumesList - useGetAggregateKeys Category Regression', () => {});it('should call aggregate keys API with k8s_volume_capacity', async () => {- render(- <NuqsTestingAdapter>- <QueryClientProvider client={queryClient}>- <MemoryRouter>- <K8sVolumesList- isFiltersVisible={false}- handleFilterVisibilityChange={jest.fn()}- quickFiltersLastUpdated={-1}- />- </MemoryRouter>- </QueryClientProvider>- </NuqsTestingAdapter>,- );+ renderWithRealStore();await waitFor(() => {expect(requestsMade.length).toBeGreaterThan(0);@@ -128,19 +179,7 @@ describe('K8sVolumesList - useGetAggregateKeys Category Regression', () => {activeLicense: (null as unknown) as LicenseResModel,} as IAppContext);- render(- <NuqsTestingAdapter>- <QueryClientProvider client={queryClient}>- <MemoryRouter>- <K8sVolumesList- isFiltersVisible={false}- handleFilterVisibilityChange={jest.fn()}- quickFiltersLastUpdated={-1}- />- </MemoryRouter>- </QueryClientProvider>- </NuqsTestingAdapter>,- );+ renderWithRealStore();await waitFor(() => {expect(requestsMade.length).toBeGreaterThan(0);@@ -159,3 +198,193 @@ describe('K8sVolumesList - useGetAggregateKeys Category Regression', () => {expect(aggregateAttribute).toBe('k8s.volume.capacity');});});++describe('K8sVolumesList', () => {+ beforeEach(() => {+ server.use(+ rest.post('http://localhost/api/v1/pvcs/list', (_req, res, ctx) =>+ res(ctx.status(200), ctx.json(mockVolumesResponse)),+ ),+ rest.get(+ 'http://localhost/api/v3/autocomplete/attribute_keys',+ (_req, res, ctx) =>+ res(ctx.status(200), ctx.json({ data: { attributeKeys: [] } })),+ ),+ );+ });++ it('renders volume rows from API response', async () => {+ renderWithRealStore();++ await waitFor(async () => {+ const elements = await screen.findAllByText('test-pvc');++ expect(elements.length).toBeGreaterThan(0);+ });+ });++ it('opens VolumeDetails when a volume row is clicked', async () => {+ const user = userEvent.setup();+ renderWithRealStore();++ const pvcCells = await screen.findAllByText('test-pvc');+ expect(pvcCells.length).toBeGreaterThan(0);++ const row = pvcCells[0].closest('tr');+ expect(row).not.toBeNull();+ await user.click(row!);++ await waitFor(async () => {+ const cells = await screen.findAllByText('test-pvc');+ expect(cells.length).toBeGreaterThan(1);+ });+ });++ it('closes VolumeDetails when the close button is clicked', async () => {+ const user = userEvent.setup();+ renderWithRealStore();++ const pvcCells = await screen.findAllByText('test-pvc');+ expect(pvcCells.length).toBeGreaterThan(0);++ const row = pvcCells[0].closest('tr');+ await user.click(row!);++ await waitFor(() => {+ expect(screen.getByRole('button', { name: 'Close' })).toBeInTheDocument();+ });++ await user.click(screen.getByRole('button', { name: 'Close' }));++ await waitFor(() => {+ expect(+ screen.queryByRole('button', { name: 'Close' }),+ ).not.toBeInTheDocument();+ });+ });++ it('does not re-fetch the volumes list when time range changes after selecting a volume', async () => {+ const user = userEvent.setup();+ let apiCallCount = 0;+ server.use(+ rest.post('http://localhost/api/v1/pvcs/list', async (_req, res, ctx) => {+ apiCallCount += 1;+ return res(ctx.status(200), ctx.json(mockVolumesResponse));+ }),+ );++ const { testStore } = renderWithRealStore();++ await waitFor(() => expect(apiCallCount).toBe(1));++ const pvcCells = await screen.findAllByText('test-pvc');+ const row = pvcCells[0].closest('tr');+ await user.click(row!);+ await waitFor(async () => {+ const cells = await screen.findAllByText('test-pvc');+ expect(cells.length).toBeGreaterThan(1);+ });++ // Wait for nuqs URL state to fully propagate to the component+ // The selectedVolumeUID is managed via nuqs (async URL state),+ // so we need to ensure the state has settled before dispatching time changes+ await act(async () => {+ await new Promise((resolve) => {+ setTimeout(resolve, 0);+ });+ });++ const countAfterClick = apiCallCount;++ // There's a specific component causing the min/max time to be updated+ // After the volume loads, it triggers the change again+ // And then the query to fetch data for the selected volume enters in a loop+ act(() => {+ testStore.dispatch({+ type: UPDATE_TIME_INTERVAL,+ payload: {+ minTime: Date.now() * 1000000 - 30 * 60 * 1000 * 1000000,+ maxTime: Date.now() * 1000000,+ selectedTime: '30m',+ },+ } as any);+ });++ // Allow any potential re-fetch to settle+ await new Promise((resolve) => {+ setTimeout(resolve, 500);+ });++ expect(apiCallCount).toBe(countAfterClick);+ });++ it('does not re-fetch groupedByRowData when time range changes after expanding a volume row with groupBy', async () => {+ const user = userEvent.setup();+ const groupByValue = [{ key: 'k8s_namespace_name' }];++ let groupedByRowDataCallCount = 0;+ server.use(+ rest.post('http://localhost/api/v1/pvcs/list', async (req, res, ctx) => {+ const body = await req.json();+ // Check for both underscore and dot notation keys since dotMetricsEnabled+ // may be true or false depending on test order+ const isGroupedByRowDataRequest = body.filters?.items?.some(+ (item: { key?: { key?: string }; value?: string }) =>+ (item.key?.key === 'k8s_namespace_name' ||+ item.key?.key === 'k8s.namespace.name') &&+ item.value === 'test-namespace',+ );+ if (isGroupedByRowDataRequest) {+ groupedByRowDataCallCount += 1;+ }+ return res(ctx.status(200), ctx.json(mockVolumesResponse));+ }),+ rest.get(+ 'http://localhost/api/v3/autocomplete/attribute_keys',+ (_req, res, ctx) =>+ res(+ ctx.status(200),+ ctx.json({+ data: {+ attributeKeys: [{ key: 'k8s_namespace_name', dataType: 'string' }],+ },+ }),+ ),+ ),+ );++ const { testStore } = renderWithRealStore({+ [INFRA_MONITORING_K8S_PARAMS_KEYS.GROUP_BY]: JSON.stringify(groupByValue),+ });++ await waitFor(async () => {+ const elements = await screen.findAllByText('test-namespace');+ return expect(elements.length).toBeGreaterThan(0);+ });++ const row = (await screen.findAllByText('test-namespace'))[0].closest('tr');+ expect(row).not.toBeNull();+ user.click(row as HTMLElement);+ await waitFor(() => expect(groupedByRowDataCallCount).toBe(1));++ const countAfterExpand = groupedByRowDataCallCount;++ act(() => {+ testStore.dispatch({+ type: UPDATE_TIME_INTERVAL,+ payload: {+ minTime: Date.now() * 1000000 - 30 * 60 * 1000 * 1000000,+ maxTime: Date.now() * 1000000,+ selectedTime: '30m',+ },+ } as any);+ });++ // Allow any potential re-fetch to settle+ await new Promise((resolve) => {+ setTimeout(resolve, 500);+ });++ expect(groupedByRowDataCallCount).toBe(countAfterExpand);+ });+});