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(alerts-v2): preserve loaded thresholds on edit-page refresh (#12226)

Abhi kumar0d7aff9ce5fb
diff --git a/frontend/src/container/CreateAlertV2/context/__tests__/CreateAlertProvider.prefill.test.tsx b/frontend/src/container/CreateAlertV2/context/__tests__/CreateAlertProvider.prefill.test.tsx
index fa97241f4d..86786575f7 100644
--- a/frontend/src/container/CreateAlertV2/context/__tests__/CreateAlertProvider.prefill.test.tsx
+++ b/frontend/src/container/CreateAlertV2/context/__tests__/CreateAlertProvider.prefill.test.tsx
@@ -1,9 +1,11 @@
import { QueryClient, QueryClientProvider } from 'react-query';
-import { MemoryRouter } from 'react-router-dom';
-import { render, screen } from '@testing-library/react';
+import { MemoryRouter, useHistory } from 'react-router-dom';
+import { fireEvent, render, screen } from '@testing-library/react';
import { AlertTypes } from 'types/api/alerts/alertTypes';
+import { INITIAL_CREATE_ALERT_STATE } from '../constants';
import { CreateAlertProvider, useCreateAlertState } from '../index';
+import { AlertThresholdMatchType } from '../types';
// The provider only needs a query with a unit + empty builder for these assertions.
jest.mock('hooks/queryBuilder/useQueryBuilder', () => ({
@@ -108,3 +110,67 @@ describe('CreateAlertProvider — URL-declared prefill (issue #5291)', () => {
expect(screen.getByTestId('window-type')).toHaveTextContent('cumulative');
});
});
+
+// Reproduces the #12137 regression: on the alert overview page the query builder
+// rewrites location.search after the alert loads, which used to re-run the prefill
+// effect and RESET the loaded threshold back to 0.
+function SearchMutator(): JSX.Element {
+ const routerHistory = useHistory();
+ return (
+ <button
+ type="button"
+ data-testid="mutate-search"
+ onClick={(): void =>
+ routerHistory.replace(
+ '/alerts/overview?compositeQuery=normalized&ruleId=r1',
+ )
+ }
+ >
+ change search
+ </button>
+ );
+}
+
+describe('CreateAlertProvider — edit mode ignores URL prefill', () => {
+ it('keeps loaded thresholds when the query builder rewrites location.search', () => {
+ const initialAlertState = {
+ ...INITIAL_CREATE_ALERT_STATE,
+ threshold: {
+ ...INITIAL_CREATE_ALERT_STATE.threshold,
+ matchType: AlertThresholdMatchType.AT_LEAST_ONCE,
+ thresholds: [
+ {
+ ...INITIAL_CREATE_ALERT_STATE.threshold.thresholds[0],
+ thresholdValue: 245,
+ },
+ ],
+ },
+ };
+
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ render(
+ <MemoryRouter initialEntries={['/alerts/overview?ruleId=r1']}>
+ <QueryClientProvider client={queryClient}>
+ <CreateAlertProvider
+ initialAlertType={AlertTypes.METRICS_BASED_ALERT}
+ isEditMode
+ ruleId="r1"
+ initialAlertState={initialAlertState}
+ >
+ <Probe />
+ <SearchMutator />
+ </CreateAlertProvider>
+ </QueryClientProvider>
+ </MemoryRouter>,
+ );
+
+ expect(screen.getByTestId('threshold-value')).toHaveTextContent('245');
+
+ // Simulate the query builder normalizing the query into the URL post-load.
+ fireEvent.click(screen.getByTestId('mutate-search'));
+
+ expect(screen.getByTestId('threshold-value')).toHaveTextContent('245');
+ });
+});
diff --git a/frontend/src/container/CreateAlertV2/context/index.tsx b/frontend/src/container/CreateAlertV2/context/index.tsx
index 058b9105a3..d750d2a93a 100644
--- a/frontend/src/container/CreateAlertV2/context/index.tsx
+++ b/frontend/src/container/CreateAlertV2/context/index.tsx
@@ -168,6 +168,14 @@ export function CreateAlertProvider(
const yAxisUnitAppliedRef = useRef(false);
useEffect(() => {
+ // URL-declared prefill is a create-flow concern. In edit mode the alert
+ // state is owned by SET_INITIAL_STATE; running the RESET below would wipe
+ // the loaded thresholds each time the query builder rewrites location.search
+ // (which yields a fresh urlPrefill object and re-triggers this effect).
+ if (isEditMode) {
+ return;
+ }
+
setCreateAlertState({
slice: CreateAlertSlice.THRESHOLD,
action: {
@@ -235,7 +243,7 @@ export function CreateAlertProvider(
},
});
}
- }, [alertType, urlPrefill, ruleNameFromURL, yAxisUnitFromURL]);
+ }, [alertType, urlPrefill, ruleNameFromURL, yAxisUnitFromURL, isEditMode]);
useEffect(() => {
if (isEditMode && initialAlertState) {