Skip to content

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.

fix(apm): stop the service drilldown crashing on an empty operations list (#12578)

Abhi kumar098448330d9a
diff --git a/frontend/src/container/GridCardLayout/__tests__/useResolveQuery.test.tsx b/frontend/src/container/GridCardLayout/__tests__/useResolveQuery.test.tsx
new file mode 100644
index 0000000000..564a0cbcf7
--- /dev/null
+++ b/frontend/src/container/GridCardLayout/__tests__/useResolveQuery.test.tsx
@@ -0,0 +1,91 @@
+import { renderHook } from '@testing-library/react';
+import { PANEL_TYPES } from 'constants/queryBuilder';
+import MockQueryClientProvider from 'providers/test/MockQueryClientProvider';
+import { Query } from 'types/api/queryBuilder/queryBuilderData';
+
+import useUpdatedQuery from '../useResolveQuery';
+
+const mockGetSubstituteVars = jest.fn();
+const mockDynamicVariables: unknown[] = [];
+
+jest.mock('api/dashboard/substitute_vars', () => ({
+ getSubstituteVars: (...args: unknown[]): unknown =>
+ mockGetSubstituteVars(...args),
+}));
+
+jest.mock('api/v5/v5', () => ({
+ prepareQueryRangePayloadV5: (): { queryPayload: unknown } => ({
+ queryPayload: { start: 0, end: 1 },
+ }),
+}));
+
+jest.mock(
+ 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi',
+ () => ({
+ mapQueryDataFromApi: (): Query => ({ resolved: true }) as unknown as Query,
+ }),
+);
+
+jest.mock('hooks/dashboard/useDashboardVariablesByType', () => ({
+ useDashboardVariablesByType: (): unknown[] => mockDynamicVariables,
+}));
+
+jest.mock('react-redux', () => ({
+ ...jest.requireActual('react-redux'),
+ useSelector: (): unknown => ({
+ selectedTime: 'GLOBAL_TIME',
+ }),
+}));
+
+const QUERY = { builder: { queryData: [] } } as unknown as Query;
+
+const WIDGET_CONFIG = {
+ query: QUERY,
+ panelTypes: PANEL_TYPES.TIME_SERIES,
+ timePreferance: 'GLOBAL_TIME' as const,
+};
+
+describe('useResolveQuery', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ mockDynamicVariables.length = 0;
+ });
+
+ it('skips the substitute_vars round-trip when there are no variables', async () => {
+ const { result } = renderHook(() => useUpdatedQuery(), {
+ wrapper: MockQueryClientProvider,
+ });
+
+ const resolved = await result.current.getUpdatedQuery({
+ widgetConfig: WIDGET_CONFIG,
+ });
+
+ expect(mockGetSubstituteVars).not.toHaveBeenCalled();
+ expect(resolved).toBe(QUERY);
+ });
+
+ it('resolves through substitute_vars when the dashboard has variables', async () => {
+ mockGetSubstituteVars.mockResolvedValue({
+ httpStatusCode: 200,
+ data: { compositeQuery: {} },
+ });
+
+ const { result } = renderHook(() => useUpdatedQuery(), {
+ wrapper: MockQueryClientProvider,
+ });
+
+ const resolved = await result.current.getUpdatedQuery({
+ widgetConfig: WIDGET_CONFIG,
+ dashboardData: {
+ data: {
+ variables: {
+ env: { name: 'env', selectedValue: 'prod' },
+ },
+ },
+ },
+ });
+
+ expect(mockGetSubstituteVars).toHaveBeenCalledTimes(1);
+ expect(resolved).toStrictEqual({ resolved: true });
+ });
+});
diff --git a/frontend/src/container/GridCardLayout/useResolveQuery.ts b/frontend/src/container/GridCardLayout/useResolveQuery.ts
index c8f72be228..ee1cd338b4 100644
--- a/frontend/src/container/GridCardLayout/useResolveQuery.ts
+++ b/frontend/src/container/GridCardLayout/useResolveQuery.ts
@@ -2,6 +2,7 @@ import { useCallback } from 'react';
import { useMutation } from 'react-query';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
+import { isEmpty } from 'lodash-es';
import { getSubstituteVars } from 'api/dashboard/substitute_vars';
import { prepareQueryRangePayloadV5 } from 'api/v5/v5';
import { PANEL_TYPES } from 'constants/queryBuilder';
@@ -46,13 +47,21 @@ function useUpdatedQuery(): UseUpdatedQueryResult {
widgetConfig,
dashboardData,
}: UseUpdatedQueryOptions): Promise<Query> => {
+ const variables = getDashboardVariables(dashboardData?.data?.variables);
+
+ // `/substitute_vars` only rewrites `$variable` references, so on surfaces with no
+ // dashboard behind them (APM, Celery, API monitoring) the round-trip is a no-op.
+ if (isEmpty(variables) && isEmpty(dashboardDynamicVariables)) {
+ return widgetConfig.query;
+ }
+
// Prepare query payload with resolved variables
const { queryPayload } = prepareQueryRangePayloadV5({
query: widgetConfig.query,
graphType: getGraphType(widgetConfig.panelTypes),
selectedTime: widgetConfig.timePreferance,
globalSelectedInterval,
- variables: getDashboardVariables(dashboardData?.data?.variables),
+ variables,
originalGraphType: widgetConfig.panelTypes,
dynamicVariables: dashboardDynamicVariables,
});
diff --git a/frontend/src/container/MetricsApplication/Tabs/Overview.tsx b/frontend/src/container/MetricsApplication/Tabs/Overview.tsx
index b6622cb933..0982c917cf 100644
--- a/frontend/src/container/MetricsApplication/Tabs/Overview.tsx
+++ b/frontend/src/container/MetricsApplication/Tabs/Overview.tsx
@@ -124,6 +124,9 @@ function Application(): JSX.Element {
start: minTime,
end: maxTime,
}),
+ // the time range is part of the key, so without this every window change blanks the
+ // operations list and the widgets below are rebuilt with an empty `operation in []`
+ keepPreviousData: true,
});
const selectedTraceTags: string = JSON.stringify(
diff --git a/frontend/src/container/QueryTable/Drilldown/__tests__/useBaseAggregateOptions.test.tsx b/frontend/src/container/QueryTable/Drilldown/__tests__/useBaseAggregateOptions.test.tsx
new file mode 100644
index 0000000000..93341a3b79
--- /dev/null
+++ b/frontend/src/container/QueryTable/Drilldown/__tests__/useBaseAggregateOptions.test.tsx
@@ -0,0 +1,88 @@
+import { renderHook, waitFor } from '@testing-library/react';
+import { Query } from 'types/api/queryBuilder/queryBuilderData';
+
+import useBaseAggregateOptions from '../useBaseAggregateOptions';
+
+const mockGetUpdatedQuery = jest.fn();
+const mockNotificationsError = jest.fn();
+
+jest.mock('container/GridCardLayout/useResolveQuery', () => ({
+ __esModule: true,
+ default: (): unknown => ({
+ getUpdatedQuery: mockGetUpdatedQuery,
+ isLoading: false,
+ }),
+}));
+
+jest.mock('hooks/useNotifications', () => ({
+ useNotifications: (): unknown => ({
+ notifications: { error: mockNotificationsError },
+ }),
+}));
+
+jest.mock('providers/Dashboard/store/useDashboardStore', () => ({
+ useDashboardStore: (): unknown => ({ dashboardData: undefined }),
+}));
+
+jest.mock('hooks/dashboard/useContextVariables', () => ({
+ __esModule: true,
+ default: (): unknown => ({ processedVariables: {} }),
+}));
+
+jest.mock('hooks/useSafeNavigate', () => ({
+ useSafeNavigate: (): unknown => ({ safeNavigate: jest.fn() }),
+}));
+
+jest.mock('react-router-dom', () => ({
+ ...jest.requireActual('react-router-dom'),
+ useLocation: (): { pathname: string } => ({ pathname: '/services/socky-api' }),
+}));
+
+const QUERY = {
+ builder: {
+ queryData: [{ queryName: 'A', dataSource: 'traces', aggregations: [] }],
+ },
+} as unknown as Query;
+
+const AGGREGATE_DATA = { queryName: 'A', filters: [] };
+
+const renderOptions = (): ReturnType<typeof renderHook> =>
+ renderHook(() =>
+ useBaseAggregateOptions({
+ query: QUERY,
+ onClose: jest.fn(),
+ subMenu: '',
+ setSubMenu: jest.fn(),
+ aggregateData: AGGREGATE_DATA,
+ fieldVariables: {},
+ }),
+ );
+
+describe('useBaseAggregateOptions', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it('notifies and keeps the unresolved query when variable resolution fails', async () => {
+ mockGetUpdatedQuery.mockRejectedValue(
+ new Error('syntax errors in expression'),
+ );
+
+ renderOptions();
+
+ await waitFor(() =>
+ expect(mockNotificationsError).toHaveBeenCalledWith({
+ message: 'Unable to resolve variables',
+ }),
+ );
+ });
+
+ it('does not notify when variable resolution succeeds', async () => {
+ mockGetUpdatedQuery.mockResolvedValue(QUERY);
+
+ renderOptions();
+
+ await waitFor(() => expect(mockGetUpdatedQuery).toHaveBeenCalled());
+ expect(mockNotificationsError).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx b/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
index 68f2b89447..1de1311bd5 100644
--- a/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
+++ b/frontend/src/container/QueryTable/Drilldown/useBaseAggregateOptions.tsx
@@ -6,6 +6,7 @@ import { PANEL_TYPES } from 'constants/queryBuilder';
import useUpdatedQuery from 'container/GridCardLayout/useResolveQuery';
import { processContextLinks } from 'container/NewWidget/RightContainer/ContextLinks/utils';
import useContextVariables from 'hooks/dashboard/useContextVariables';
+import { useNotifications } from 'hooks/useNotifications';
import ContextMenu from 'periscope/components/ContextMenu';
import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { ContextLinksData } from 'types/api/dashboard/getAll';
@@ -50,23 +51,25 @@ const useBaseAggregateOptions = ({
const { getUpdatedQuery, isLoading: isResolveQueryLoading } =
useUpdatedQuery();
const { dashboardData } = useDashboardStore();
+ const { notifications } = useNotifications();
useEffect(() => {
if (!aggregateData) {
return;
}
- const resolveQuery = async (): Promise<void> => {
- const updatedQuery = await getUpdatedQuery({
- widgetConfig: {
- query,
- panelTypes: panelType || PANEL_TYPES.TIME_SERIES,
- timePreferance: 'GLOBAL_TIME',
- },
- dashboardData,
+ getUpdatedQuery({
+ widgetConfig: {
+ query,
+ panelTypes: panelType || PANEL_TYPES.TIME_SERIES,
+ timePreferance: 'GLOBAL_TIME',
+ },
+ dashboardData,
+ })
+ .then(setResolvedQuery)
+ .catch(() => {
+ setResolvedQuery(query);
+ notifications.error({ message: 'Unable to resolve variables' });
});
- setResolvedQuery(updatedQuery);
- };
- resolveQuery();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [query, aggregateData, panelType]);