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
fix: added sanity logic for explorer old urls (#8804)
SagarRajput-7019083983a14
diff --git a/frontend/src/container/LogsExplorerViews/index.tsx b/frontend/src/container/LogsExplorerViews/index.tsxindex ecfc80f1fe..33214c9962 100644--- a/frontend/src/container/LogsExplorerViews/index.tsx+++ b/frontend/src/container/LogsExplorerViews/index.tsx@@ -256,7 +256,6 @@ function LogsExplorerViewsContainer({} = useGetExplorerQueryRange(listChartQuery,PANEL_TYPES.TIME_SERIES,- // ENTITY_VERSION_V4,ENTITY_VERSION_V5,{enabled:@@ -279,7 +278,6 @@ function LogsExplorerViewsContainer({} = useGetExplorerQueryRange(requestData,panelType,- // ENTITY_VERSION_V4,ENTITY_VERSION_V5,{keepPreviousData: true,diff --git a/frontend/src/providers/QueryBuilder.tsx b/frontend/src/providers/QueryBuilder.tsxindex b5fb5ef10d..c07bfecd26 100644--- a/frontend/src/providers/QueryBuilder.tsx+++ b/frontend/src/providers/QueryBuilder.tsx@@ -61,6 +61,7 @@ import {QueryBuilderData,} from 'types/common/queryBuilder';import { GlobalReducer } from 'types/reducer/globalTime';+import { sanitizeOrderByForExplorer } from 'utils/sanitizeOrderBy';import { v4 as uuid } from 'uuid';export const QueryBuilderContext = createContext<QueryBuilderContextType>({@@ -102,6 +103,12 @@ export function QueryBuilderProvider({const currentPathnameRef = useRef<string | null>(location.pathname);+ // This is used to determine if the query was called from the handleRunQuery function - which means manual trigger from Stage and Run button+ const [+ calledFromHandleRunQuery,+ setCalledFromHandleRunQuery,+ ] = useState<boolean>(false);+const { maxTime, minTime } = useSelector<AppState, GlobalReducer>((state) => state.globalTime,);@@ -184,6 +191,17 @@ export function QueryBuilderProvider({} as BaseAutocompleteData,};+ // Explorer pages: sanitize stale orderBy before first query+ const isExplorer =+ location.pathname === ROUTES.LOGS_EXPLORER ||+ location.pathname === ROUTES.TRACES_EXPLORER;+ if (isExplorer) {+ const sanitizedOrderBy = sanitizeOrderByForExplorer(currentElement);+ return calledFromHandleRunQuery+ ? currentElement+ : { ...currentElement, orderBy: sanitizedOrderBy };+ }+return currentElement;});@@ -215,7 +233,7 @@ export function QueryBuilderProvider({return nextQuery;},- [initialDataSource],+ [initialDataSource, location.pathname, calledFromHandleRunQuery],);const initQueryBuilderData = useCallback(@@ -871,6 +889,12 @@ export function QueryBuilderProvider({const handleRunQuery = useCallback((shallUpdateStepInterval?: boolean, newQBQuery?: boolean) => {+ const isExplorer =+ location.pathname === ROUTES.LOGS_EXPLORER ||+ location.pathname === ROUTES.TRACES_EXPLORER;+ if (isExplorer) {+ setCalledFromHandleRunQuery(true);+ }let currentQueryData = currentQuery;if (newQBQuery) {@@ -915,7 +939,14 @@ export function QueryBuilderProvider({queryType,});},- [currentQuery, queryType, maxTime, minTime, redirectWithQueryBuilderData],+ [+ location.pathname,+ currentQuery,+ queryType,+ maxTime,+ minTime,+ redirectWithQueryBuilderData,+ ],);useEffect(() => {@@ -925,6 +956,7 @@ export function QueryBuilderProvider({setStagedQuery(null);// reset the last used query to 0 when navigating away from the pagesetLastUsedQuery(0);+ setCalledFromHandleRunQuery(false);}}, [location.pathname]);diff --git a/frontend/src/utils/__tests__/sanitizeOrderBy.test.ts b/frontend/src/utils/__tests__/sanitizeOrderBy.test.tsnew file mode 100644index 0000000000..633d9a7efc--- /dev/null+++ b/frontend/src/utils/__tests__/sanitizeOrderBy.test.ts@@ -0,0 +1,130 @@+/* eslint-disable sonarjs/no-duplicate-string */+import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse';+import {+ IBuilderQuery,+ OrderByPayload,+} from 'types/api/queryBuilder/queryBuilderData';+import { DataSource } from 'types/common/queryBuilder';+import { getParsedAggregationOptionsForOrderBy } from 'utils/aggregationConverter';+import { sanitizeOrderByForExplorer } from 'utils/sanitizeOrderBy';++jest.mock('utils/aggregationConverter', () => ({+ getParsedAggregationOptionsForOrderBy: jest.fn(),+}));++const buildQuery = (overrides: Partial<IBuilderQuery> = {}): IBuilderQuery => ({+ queryName: 'A',+ dataSource: DataSource.TRACES,+ aggregateOperator: '',+ aggregateAttribute: undefined,+ aggregations: [],+ timeAggregation: '',+ spaceAggregation: '',+ temporality: '',+ functions: [],+ filter: { expression: '' } as any,+ filters: { items: [], op: 'AND' } as any,+ groupBy: [],+ expression: '',+ disabled: false,+ having: [] as any,+ limit: null,+ stepInterval: 60 as any,+ orderBy: [],+ legend: '',+ ...overrides,+});++describe('sanitizeOrderByForExplorer', () => {+ beforeEach(() => {+ jest.clearAllMocks();+ });++ it('keeps only orderBy items that are present in groupBy keys or aggregation keys (including alias)', () => {+ (getParsedAggregationOptionsForOrderBy as jest.Mock).mockReturnValue([+ {+ key: 'count()',+ dataType: DataTypes.Float64,+ isColumn: false,+ type: '',+ isJSON: false,+ },+ {+ key: 'avg(duration)',+ dataType: DataTypes.Float64,+ isColumn: false,+ type: '',+ isJSON: false,+ },+ {+ key: 'latency',+ dataType: DataTypes.Float64,+ isColumn: false,+ type: '',+ isJSON: false,+ },+ ]);++ const orderBy: OrderByPayload[] = [+ { columnName: 'service.name', order: 'asc' },+ { columnName: 'count()', order: 'desc' },+ { columnName: 'avg(duration)', order: 'asc' },+ { columnName: 'latency', order: 'asc' }, // alias+ { columnName: 'not-allowed', order: 'desc' }, // invalid orderBy+ { columnName: 'timestamp', order: 'desc' }, // invalid orderBy+ ];++ const query = buildQuery({+ groupBy: [+ {+ key: 'service.name',+ dataType: DataTypes.String,+ isColumn: true,+ type: 'resource',+ isJSON: false,+ },+ ] as any,+ orderBy,+ });++ const result = sanitizeOrderByForExplorer(query);++ expect(result).toEqual([+ { columnName: 'service.name', order: 'asc' },+ { columnName: 'count()', order: 'desc' },+ { columnName: 'avg(duration)', order: 'asc' },+ { columnName: 'latency', order: 'asc' },+ ]);+ });++ it('returns empty when none of the orderBy items are allowed', () => {+ (getParsedAggregationOptionsForOrderBy as jest.Mock).mockReturnValue([+ {+ key: 'count()',+ dataType: DataTypes.Float64,+ isColumn: false,+ type: '',+ isJSON: false,+ },+ ]);++ const query = buildQuery({+ groupBy: [],+ orderBy: [+ { columnName: 'foo', order: 'asc' },+ { columnName: 'bar', order: 'desc' },+ ],+ });++ const result = sanitizeOrderByForExplorer(query);+ expect(result).toEqual([]);+ });++ it('handles missing orderBy by returning an empty array', () => {+ (getParsedAggregationOptionsForOrderBy as jest.Mock).mockReturnValue([]);++ const query = buildQuery({ orderBy: [] });+ const result = sanitizeOrderByForExplorer(query);+ expect(result).toEqual([]);+ });+});diff --git a/frontend/src/utils/sanitizeOrderBy.ts b/frontend/src/utils/sanitizeOrderBy.tsnew file mode 100644index 0000000000..098996e7d0--- /dev/null+++ b/frontend/src/utils/sanitizeOrderBy.ts@@ -0,0 +1,32 @@+import * as Sentry from '@sentry/react';+import {+ IBuilderQuery,+ OrderByPayload,+} from 'types/api/queryBuilder/queryBuilderData';++import { getParsedAggregationOptionsForOrderBy } from './aggregationConverter';++export function sanitizeOrderByForExplorer(+ query: IBuilderQuery,+): OrderByPayload[] {+ const allowed = new Set<string>();+ (query.groupBy || []).forEach((g) => g?.key && allowed.add(g.key));+ getParsedAggregationOptionsForOrderBy(query).forEach((agg) => {+ // agg.key is the expression or alias (e.g., count(), avg(quantity), 'alias')+ if ((agg as any)?.key) allowed.add((agg as any).key as string);+ });++ const current = query.orderBy || [];++ const hasInvalidOrderBy = current.some((o) => !allowed.has(o.columnName));++ if (hasInvalidOrderBy) {+ Sentry.captureEvent({+ message: `Invalid orderBy: current: ${JSON.stringify(+ current,+ )} - allowed: ${JSON.stringify(Array.from(allowed))}`,+ level: 'warning',+ });+ }+ return current.filter((o) => allowed.has(o.columnName));+}