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: aggregation options for metric in alert condition do not get updated (#9485)

Amlan Kumar Nandyb7ece08d3e2a
diff --git a/frontend/src/hooks/queryBuilder/__tests__/useQueryBuilderOperations.test.ts b/frontend/src/hooks/queryBuilder/__tests__/useQueryBuilderOperations.test.ts
index c150d3bbb7..0e7318ce70 100644
--- a/frontend/src/hooks/queryBuilder/__tests__/useQueryBuilderOperations.test.ts
+++ b/frontend/src/hooks/queryBuilder/__tests__/useQueryBuilderOperations.test.ts
@@ -245,5 +245,81 @@ describe('useQueryBuilderOperations - Empty Aggregate Attribute Type', () => {
}),
);
});
+
+ it('should reset operators when going from gauge -> empty -> gauge', () => {
+ // Start with a gauge metric
+ const gaugeQuery: IBuilderQuery = {
+ ...defaultMockQuery,
+ aggregateAttribute: {
+ key: 'original_gauge',
+ dataType: DataTypes.Float64,
+ type: ATTRIBUTE_TYPES.GAUGE,
+ } as BaseAutocompleteData,
+ aggregations: [
+ {
+ timeAggregation: MetricAggregateOperator.COUNT_DISTINCT,
+ metricName: 'original_gauge',
+ temporality: '',
+ spaceAggregation: '',
+ },
+ ],
+ };
+ const { result, rerender } = renderHook(
+ ({ query }) =>
+ useQueryOperations({
+ query,
+ index: 0,
+ entityVersion: ENTITY_VERSION_V5,
+ }),
+ {
+ initialProps: { query: gaugeQuery },
+ },
+ );
+
+ // Re-render with empty attribute
+ const emptyAttribute: BaseAutocompleteData = {
+ key: '',
+ dataType: DataTypes.Float64,
+ type: '',
+ };
+ const emptyQuery: IBuilderQuery = {
+ ...defaultMockQuery,
+ aggregateAttribute: emptyAttribute,
+ aggregations: [
+ {
+ timeAggregation: MetricAggregateOperator.COUNT,
+ metricName: '',
+ temporality: '',
+ spaceAggregation: MetricAggregateOperator.SUM,
+ },
+ ],
+ };
+ rerender({ query: emptyQuery });
+
+ // Change to a new gauge metric
+ const newGaugeAttribute: BaseAutocompleteData = {
+ key: 'new_gauge',
+ dataType: DataTypes.Float64,
+ type: ATTRIBUTE_TYPES.GAUGE,
+ };
+ act(() => {
+ result.current.handleChangeAggregatorAttribute(newGaugeAttribute);
+ });
+
+ expect(mockHandleSetQueryData).toHaveBeenLastCalledWith(
+ 0,
+ expect.objectContaining({
+ aggregateAttribute: newGaugeAttribute,
+ aggregations: [
+ {
+ timeAggregation: MetricAggregateOperator.AVG,
+ metricName: 'new_gauge',
+ temporality: '',
+ spaceAggregation: '',
+ },
+ ],
+ }),
+ );
+ });
});
});
diff --git a/frontend/src/hooks/queryBuilder/useQueryBuilderOperations.ts b/frontend/src/hooks/queryBuilder/useQueryBuilderOperations.ts
index faaab1b1b0..3b9ede45e4 100644
--- a/frontend/src/hooks/queryBuilder/useQueryBuilderOperations.ts
+++ b/frontend/src/hooks/queryBuilder/useQueryBuilderOperations.ts
@@ -89,6 +89,8 @@ export const useQueryOperations: UseQueryOperations = ({
name: metricName,
type: metricType,
});
+ } else {
+ setPreviousMetricInfo(null);
}
}
}, [query]);
@@ -295,7 +297,6 @@ export const useQueryOperations: UseQueryOperations = ({
if (!isEditMode) {
// Get current metric info
- const currentMetricName = newQuery.aggregateAttribute?.key || '';
const currentMetricType = newQuery.aggregateAttribute?.type || '';
const prevMetricType = previousMetricInfo?.type
@@ -378,14 +379,6 @@ export const useQueryOperations: UseQueryOperations = ({
];
}
}
-
- // Update the tracked metric info for next comparison only if we have valid data
- if (currentMetricName && currentMetricType) {
- setPreviousMetricInfo({
- name: currentMetricName,
- type: currentMetricType,
- });
- }
}
}