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
perf(grid-card): set cache time zero when auto-refresh is enabled (#10225)
Vinicius Lourenço1430e5fa991a
diff --git a/frontend/src/constants/queryCacheTime.ts b/frontend/src/constants/queryCacheTime.tsnew file mode 100644index 0000000000..8378d8031d--- /dev/null+++ b/frontend/src/constants/queryCacheTime.ts@@ -0,0 +1,3 @@+export const DASHBOARD_CACHE_TIME = 30_000;+// keep it low or zero, otherwise, when enabled auto-refresh, this causes OOM due to accumulated queries in cache+export const DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED = 0;diff --git a/frontend/src/constants/reactQueryKeys.ts b/frontend/src/constants/reactQueryKeys.tsindex e3bc56b0dc..89b3a60cf5 100644--- a/frontend/src/constants/reactQueryKeys.ts+++ b/frontend/src/constants/reactQueryKeys.ts@@ -96,4 +96,7 @@ export const REACT_QUERY_KEY = {// Span Percentiles Query KeysGET_SPAN_PERCENTILES: 'GET_SPAN_PERCENTILES',++ // Dashboard Grid Card Query Keys+ DASHBOARD_GRID_CARD_QUERY_RANGE: 'DASHBOARD_GRID_CARD_QUERY_RANGE',} as const;diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DynamicVariableInput.tsx b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DynamicVariableInput.tsxindex 37118165f6..2b6b5b4a64 100644--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DynamicVariableInput.tsx+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/DynamicVariableInput.tsx@@ -13,6 +13,10 @@ import { FieldValueResponse } from 'types/api/dynamicVariables/getFieldValues';import { GlobalReducer } from 'types/reducer/globalTime';import { isRetryableError as checkIfRetryableError } from 'utils/errorUtils';+import {+ DASHBOARD_CACHE_TIME,+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,+} from '../../../constants/queryCacheTime';import SelectVariableInput from './SelectVariableInput';import { useDashboardVariableSelectHelper } from './useDashboardVariableSelectHelper';import {@@ -101,9 +105,10 @@ function DynamicVariableInput({return dynamicVars || 'no_dynamic_variables';}, [existingVariables]);- const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(- (state) => state.globalTime,- );+ const { maxTime, minTime, isAutoRefreshDisabled } = useSelector<+ AppState,+ GlobalReducer+ >((state) => state.globalTime);const {variableFetchCycleId,@@ -232,6 +237,9 @@ function DynamicVariableInput({!!variableData.dynamicVariablesSource &&!!variableData.dynamicVariablesAttribute &&(isVariableFetching || (isVariableSettled && hasVariableFetchedOnce)),+ cacheTime: isAutoRefreshDisabled+ ? DASHBOARD_CACHE_TIME+ : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,queryFn: ({ signal }) =>getFieldValues(variableData.dynamicVariablesSource?.toLowerCase() === 'all telemetry'diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/QueryVariableInput.tsx b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/QueryVariableInput.tsxindex d6a5a09e2f..bef3e21825 100644--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/QueryVariableInput.tsx+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/QueryVariableInput.tsx@@ -11,6 +11,10 @@ import { AppState } from 'store/reducers';import { VariableResponseProps } from 'types/api/dashboard/variables/query';import { GlobalReducer } from 'types/reducer/globalTime';+import {+ DASHBOARD_CACHE_TIME,+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,+} from '../../../constants/queryCacheTime';import { variablePropsToPayloadVariables } from '../utils';import SelectVariableInput from './SelectVariableInput';import { useDashboardVariableSelectHelper } from './useDashboardVariableSelectHelper';@@ -33,9 +37,10 @@ function QueryVariableInput({);const [errorMessage, setErrorMessage] = useState<null | string>(null);- const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(- (state) => state.globalTime,- );+ const { maxTime, minTime, isAutoRefreshDisabled } = useSelector<+ AppState,+ GlobalReducer+ >((state) => state.globalTime);const {variableFetchCycleId,@@ -197,6 +202,9 @@ function QueryVariableInput({signal,),refetchOnWindowFocus: false,+ cacheTime: isAutoRefreshDisabled+ ? DASHBOARD_CACHE_TIME+ : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,onSuccess: (response) => {getOptions(response.payload);settleVariableFetch(variableData.name, 'complete');diff --git a/frontend/src/container/GridCardLayout/GridCard/index.tsx b/frontend/src/container/GridCardLayout/GridCard/index.tsxindex 8a6efdcc0e..0765f32082 100644--- a/frontend/src/container/GridCardLayout/GridCard/index.tsx+++ b/frontend/src/container/GridCardLayout/GridCard/index.tsx@@ -25,6 +25,11 @@ import { GlobalReducer } from 'types/reducer/globalTime';import { getGraphType } from 'utils/getGraphType';import { getSortedSeriesData } from 'utils/getSortedSeriesData';+import {+ DASHBOARD_CACHE_TIME,+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,+} from '../../../constants/queryCacheTime';+import { REACT_QUERY_KEY } from '../../../constants/reactQueryKeys';import EmptyWidget from '../EmptyWidget';import { MenuItemKeys } from '../WidgetHeader/contants';import { GridCardGraphProps } from './types';@@ -68,10 +73,12 @@ function GridCardGraph({setDashboardQueryRangeCalled,} = useDashboard();- const { minTime, maxTime, selectedTime: globalSelectedInterval } = useSelector<- AppState,- GlobalReducer- >((state) => state.globalTime);+ const {+ minTime,+ maxTime,+ selectedTime: globalSelectedInterval,+ isAutoRefreshDisabled,+ } = useSelector<AppState, GlobalReducer>((state) => state.globalTime);const handleBackNavigation = (): void => {const searchParams = new URLSearchParams(window.location.search);@@ -210,8 +217,10 @@ function GridCardGraph({version || DEFAULT_ENTITY_VERSION,{queryKey: [+ REACT_QUERY_KEY.DASHBOARD_GRID_CARD_QUERY_RANGE,maxTime,minTime,+ isAutoRefreshDisabled,globalSelectedInterval,widget?.query,widget?.panelTypes,@@ -241,6 +250,9 @@ function GridCardGraph({return failureCount < 2;},keepPreviousData: true,+ cacheTime: isAutoRefreshDisabled+ ? DASHBOARD_CACHE_TIME+ : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,enabled: queryEnabledCondition,refetchOnMount: false,onError: (error) => {diff --git a/frontend/src/providers/Dashboard/Dashboard.tsx b/frontend/src/providers/Dashboard/Dashboard.tsxindex fb896ab7b9..662970bdbf 100644--- a/frontend/src/providers/Dashboard/Dashboard.tsx+++ b/frontend/src/providers/Dashboard/Dashboard.tsx@@ -46,6 +46,10 @@ import APIError from 'types/api/error';import { GlobalReducer } from 'types/reducer/globalTime';import { v4 as generateUUID } from 'uuid';+import {+ DASHBOARD_CACHE_TIME,+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,+} from '../../constants/queryCacheTime';import { useDashboardVariablesSelector } from '../../hooks/dashboard/useDashboardVariables';import {setDashboardVariablesStore,@@ -272,7 +276,12 @@ export function DashboardProvider({return data;};const dashboardResponse = useQuery(- [REACT_QUERY_KEY.DASHBOARD_BY_ID, isDashboardPage?.params, dashboardId],+ [+ REACT_QUERY_KEY.DASHBOARD_BY_ID,+ isDashboardPage?.params,+ dashboardId,+ globalTime.isAutoRefreshDisabled,+ ],{enabled: (!!isDashboardPage || !!isDashboardWidgetPage) && isLoggedIn,queryFn: async () => {@@ -289,6 +298,9 @@ export function DashboardProvider({}},refetchOnWindowFocus: false,+ cacheTime: globalTime.isAutoRefreshDisabled+ ? DASHBOARD_CACHE_TIME+ : DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,onError: (error) => {showErrorModal(error as APIError);},diff --git a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsxindex caab18411f..17bb5f0745 100644--- a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx+++ b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx@@ -1,7 +1,10 @@import { QueryClient, QueryClientProvider } from 'react-query';+// eslint-disable-next-line no-restricted-imports+import { useSelector } from 'react-redux';import { MemoryRouter } from 'react-router-dom';import { render, screen, waitFor } from '@testing-library/react';import getDashboard from 'api/v1/dashboards/id/get';+import { DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED } from 'constants/queryCacheTime';import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';import ROUTES from 'constants/routes';import { DashboardProvider, useDashboard } from 'providers/Dashboard/Dashboard';@@ -47,6 +50,7 @@ jest.mock('react-redux', () => ({selectedTime: 'GLOBAL_TIME',minTime: '2023-01-01T00:00:00Z',maxTime: '2023-01-01T01:00:00Z',+ isAutoRefreshDisabled: true,})),useDispatch: jest.fn(() => jest.fn()),}));@@ -322,13 +326,68 @@ describe('Dashboard Provider - Query Key with Route Params', () => {REACT_QUERY_KEY.DASHBOARD_BY_ID,{ dashboardId: dashboardId1 },dashboardId1,+ true, // globalTime.isAutoRefreshDisabled]);expect(cacheKeys[1]).toEqual([REACT_QUERY_KEY.DASHBOARD_BY_ID,{ dashboardId: dashboardId2 },dashboardId2,+ true, // globalTime.isAutoRefreshDisabled]);});++ it('should not store dashboard in cache when autoRefresh is enabled (isAutoRefreshDisabled=false)', async () => {+ jest.mocked(useSelector).mockImplementation(() => ({+ selectedTime: 'GLOBAL_TIME',+ minTime: '2023-01-01T00:00:00Z',+ maxTime: '2023-01-01T01:00:00Z',+ isAutoRefreshDisabled: false,+ }));++ const queryClient = createTestQueryClient();+ const dashboardId = 'auto-refresh-dashboard';++ mockUseRouteMatch.mockReturnValue({+ path: ROUTES.DASHBOARD,+ url: `/dashboard/${dashboardId}`,+ isExact: true,+ params: { dashboardId },+ });++ render(+ <QueryClientProvider client={queryClient}>+ <MemoryRouter initialEntries={[`/dashboard/${dashboardId}`]}>+ <DashboardProvider>+ <TestComponent />+ </DashboardProvider>+ </MemoryRouter>+ </QueryClientProvider>,+ );++ await waitFor(() => {+ expect(mockGetDashboard).toHaveBeenCalledWith({ id: dashboardId });+ });++ const dashboardQuery = queryClient+ .getQueryCache()+ .getAll()+ .find(+ (query) =>+ query.queryKey[0] === REACT_QUERY_KEY.DASHBOARD_BY_ID &&+ query.queryKey[3] === false,+ );+ expect(dashboardQuery).toBeDefined();+ expect((dashboardQuery as { cacheTime: number }).cacheTime).toBe(+ DASHBOARD_CACHE_TIME_ON_REFRESH_ENABLED,+ );++ jest.mocked(useSelector).mockImplementation(() => ({+ selectedTime: 'GLOBAL_TIME',+ minTime: '2023-01-01T00:00:00Z',+ maxTime: '2023-01-01T01:00:00Z',+ isAutoRefreshDisabled: true,+ }));+ });});});