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(exceptions): keep the filter pills when running the query (#13204)

Aditya Singhfe24e1e1316d
diff --git a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/QueryBuilderSearchV2.tsx b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/QueryBuilderSearchV2.tsx
index 93a58e633f..63b181b9d5 100644
--- a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/QueryBuilderSearchV2.tsx
+++ b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/QueryBuilderSearchV2.tsx
@@ -28,6 +28,7 @@ import { useGetAttributeSuggestions } from 'hooks/queryBuilder/useGetAttributeSu
import { validationMapper } from 'hooks/queryBuilder/useIsValidTag';
import { operatorTypeMapper } from 'hooks/queryBuilder/useOperatorType';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
+import { useRunQueryWithFilterItems } from 'hooks/queryBuilder/useRunQueryWithFilterItems';
import useDebounceValue from 'hooks/useDebounce';
import {
cloneDeep,
@@ -157,7 +158,8 @@ function QueryBuilderSearchV2(
const { registerShortcut, deregisterShortcut } = useKeyboardHotkeys();
- const { handleRunQuery, currentQuery } = useQueryBuilder();
+ const { currentQuery } = useQueryBuilder();
+ const runQuery = useRunQueryWithFilterItems();
const selectRef = useRef<BaseSelectRef>(null);
@@ -462,11 +464,11 @@ function QueryBuilderSearchV2(
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
- handleRunQuery();
+ runQuery();
setIsOpen(false);
}
},
- [handleRunQuery, searchValue],
+ [runQuery, searchValue],
);
const handleOnBlur = useCallback((): void => {
diff --git a/frontend/src/hooks/queryBuilder/useRunQueryWithFilterItems.ts b/frontend/src/hooks/queryBuilder/useRunQueryWithFilterItems.ts
new file mode 100644
index 0000000000..bb328632ce
--- /dev/null
+++ b/frontend/src/hooks/queryBuilder/useRunQueryWithFilterItems.ts
@@ -0,0 +1,15 @@
+import { useCallback } from 'react';
+
+import { useQueryBuilder } from './useQueryBuilder';
+
+// The shared handleRunQuery clears filters.items, where QueryBuilderSearchV2 keeps its filters.
+// TODO: remove with QueryBuilderSearchV2 and move its callers back to handleRunQuery.
+// QuerySearch writes filter.expression, which handleRunQuery keeps.
+export const useRunQueryWithFilterItems = (): (() => void) => {
+ const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder();
+
+ return useCallback(
+ () => redirectWithQueryBuilderData(currentQuery),
+ [currentQuery, redirectWithQueryBuilderData],
+ );
+};
diff --git a/frontend/src/pages/AllErrors/__tests__/AllErrors.test.tsx b/frontend/src/pages/AllErrors/__tests__/AllErrors.test.tsx
new file mode 100644
index 0000000000..663f4b77a9
--- /dev/null
+++ b/frontend/src/pages/AllErrors/__tests__/AllErrors.test.tsx
@@ -0,0 +1,111 @@
+// eslint-disable-next-line no-restricted-imports
+import { useSelector } from 'react-redux';
+import { ENVIRONMENT } from 'constants/env';
+import {
+ extractCompositeQueryObject,
+ INIT_URL_WITH_COMMON_QUERY,
+ MOCK_ERROR_LIST,
+ TAG_FROM_QUERY,
+} from 'container/AllError/tests/constants';
+import { server } from 'mocks-server/server';
+import { safeNavigateMock } from '__tests__/safeNavigateMock';
+import { rest } from 'msw';
+import store from 'store';
+import { render, screen, userEvent, waitFor, within } from 'tests/test-utils';
+import { Query } from 'types/api/queryBuilder/queryBuilderData';
+
+import AllErrors from '../index';
+
+// The stubbed time picker never finishes loading the global time, which gates the list.
+jest.mock('react-redux', () => ({
+ ...jest.requireActual('react-redux'),
+ useSelector: jest.fn(),
+}));
+
+jest.mock('container/TopNav/DateTimeSelectionV2', () => ({
+ __esModule: true,
+ default: function MockDateTimeSelector(): JSX.Element {
+ return <div data-testid="datetime-selector" />;
+ },
+}));
+
+const listErrorsURL = `${ENVIRONMENT.baseURL}/api/v1/listErrors`;
+const countErrorsURL = `${ENVIRONMENT.baseURL}/api/v1/countErrors`;
+
+const listErrorsSpy = jest.fn();
+
+const lastNavigatedFilterItems = (): unknown => {
+ const { calls } = safeNavigateMock.mock;
+ const to = calls[calls.length - 1]?.[0];
+ const query = extractCompositeQueryObject(
+ typeof to === 'string' ? to : '',
+ ) as Query | null;
+
+ return query?.builder.queryData[0]?.filters?.items;
+};
+
+const KEPT_FILTER_ITEMS = [
+ expect.objectContaining({
+ key: expect.objectContaining({ key: 'deployment.environment' }),
+ value: 'mq-kafka',
+ }),
+];
+
+describe('Exceptions page - run query', () => {
+ beforeEach(() => {
+ const state = store.getState();
+ (useSelector as jest.Mock).mockImplementation((selector) =>
+ selector({
+ ...state,
+ globalTime: { ...state.globalTime, loading: false },
+ }),
+ );
+ safeNavigateMock.mockClear();
+ listErrorsSpy.mockClear();
+ server.use(
+ rest.post(listErrorsURL, async (req, res, ctx) => {
+ listErrorsSpy(await req.json());
+ return res(ctx.status(200), ctx.json(MOCK_ERROR_LIST));
+ }),
+ rest.post(countErrorsURL, (_req, res, ctx) =>
+ res(ctx.status(200), ctx.json(MOCK_ERROR_LIST.length)),
+ ),
+ );
+ });
+
+ const renderWithFilter = async (): Promise<void> => {
+ render(<AllErrors />, undefined, {
+ initialRoute: INIT_URL_WITH_COMMON_QUERY,
+ });
+ await waitFor(() =>
+ expect(listErrorsSpy).toHaveBeenCalledWith(
+ expect.objectContaining({ tags: TAG_FROM_QUERY }),
+ ),
+ );
+ };
+
+ it('keeps the filter items in the query when Run Query is clicked', async () => {
+ const user = userEvent.setup({ delay: null });
+ await renderWithFilter();
+
+ await user.click(screen.getByRole('button', { name: /run query/i }));
+
+ await waitFor(() =>
+ expect(lastNavigatedFilterItems()).toStrictEqual(KEPT_FILTER_ITEMS),
+ );
+ });
+
+ it('keeps the filter items in the query on Ctrl+Enter in the search bar', async () => {
+ const user = userEvent.setup({ delay: null });
+ await renderWithFilter();
+
+ await user.click(
+ within(screen.getByTestId('qb-search-select')).getByRole('combobox'),
+ );
+ await user.keyboard('{Control>}{Enter}{/Control}');
+
+ await waitFor(() =>
+ expect(lastNavigatedFilterItems()).toStrictEqual(KEPT_FILTER_ITEMS),
+ );
+ });
+});
diff --git a/frontend/src/pages/AllErrors/index.tsx b/frontend/src/pages/AllErrors/index.tsx
index fa3ed80086..42dcab94ff 100644
--- a/frontend/src/pages/AllErrors/index.tsx
+++ b/frontend/src/pages/AllErrors/index.tsx
@@ -14,7 +14,7 @@ import { LOCALSTORAGE } from 'constants/localStorage';
import RightToolbarActions from 'container/QueryBuilder/components/ToolbarActions/RightToolbarActions';
import ResourceAttributesFilterV2 from 'container/ResourceAttributeFilterV2/ResourceAttributesFilterV2';
import Toolbar from 'container/Toolbar/Toolbar';
-import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
+import { useRunQueryWithFilterItems } from 'hooks/queryBuilder/useRunQueryWithFilterItems';
import history from 'lib/history';
import { isNull } from 'lodash-es';
@@ -25,7 +25,7 @@ import './AllErrors.styles.scss';
function AllErrors(): JSX.Element {
const { pathname } = useLocation();
- const { handleRunQuery } = useQueryBuilder();
+ const handleRunQuery = useRunQueryWithFilterItems();
const queryClient = useQueryClient();
const isLoadingQueries = useAllErrorsQueryState((s) => s.isFetching);