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
feat(frontend): register search() as a query builder function (#12513)
Tushar Vats16849967c55b
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsxindex 7669d2c4a2..d1080b8185 100644--- a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx+++ b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx@@ -59,6 +59,7 @@ import {dedupeOptionsByLabel,getFieldContextPrefix,getRecentOptions,+ isSupportedFunction,renderRecentDeleteButton,} from './utils';@@ -1275,11 +1276,13 @@ function QuerySearch({}if (queryContext.isInFunction) {- options = Object.values(QUERY_BUILDER_FUNCTIONS).map((option) => ({- label: option,- apply: `${option}()`,- type: 'function',- }));+ options = Object.values(QUERY_BUILDER_FUNCTIONS)+ .filter((option) => isSupportedFunction(option, dataSource))+ .map((option) => ({+ label: option,+ apply: `${option}()`,+ type: 'function',+ }));// Add space after selection for functionsconst optionsWithSpace = addSpaceToOptions(options);diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/__tests__/utils.test.ts b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/__tests__/utils.test.tsindex 82b91fd47b..9ded9f4074 100644--- a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/__tests__/utils.test.ts+++ b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/__tests__/utils.test.ts@@ -1,8 +1,12 @@+import { QUERY_BUILDER_FUNCTIONS } from 'constants/antlrQueryConstants';+import { DataSource } from 'types/common/queryBuilder';+import {combineInitialAndUserExpression,dedupeOptionsByLabel,getFieldContextPrefix,getUserExpressionFromCombined,+ isSupportedFunction,} from '../utils';describe('entityLogsExpression', () => {@@ -118,3 +122,19 @@ describe('dedupeOptionsByLabel', () => {expect(dedupeOptionsByLabel([])).toStrictEqual([]);});});++describe('isSupportedFunction', () => {+ const { HASANY, SEARCH } = QUERY_BUILDER_FUNCTIONS;++ it('allows the has family on every signal', () => {+ [DataSource.LOGS, DataSource.TRACES, DataSource.METRICS].forEach((signal) => {+ expect(isSupportedFunction(HASANY, signal)).toBe(true);+ });+ });++ it('allows search on logs only', () => {+ expect(isSupportedFunction(SEARCH, DataSource.LOGS)).toBe(true);+ expect(isSupportedFunction(SEARCH, DataSource.TRACES)).toBe(false);+ expect(isSupportedFunction(SEARCH, DataSource.METRICS)).toBe(false);+ });+});diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/utils.ts b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/utils.tsindex 676e66e0e6..7ca88dbf78 100644--- a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/utils.ts+++ b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/utils.ts@@ -1,6 +1,7 @@import { closeCompletion, startCompletion } from '@codemirror/autocomplete';import type { Completion } from '@codemirror/autocomplete';import type { EditorView } from '@uiw/react-codemirror';+import { QUERY_BUILDER_FUNCTIONS } from 'constants/antlrQueryConstants';import dayjs from 'dayjs';import { normalizeFilterExpression } from 'lib/recentQueries/normalize';import * as recentQueriesStore from 'lib/recentQueries/recentQueriesStore';@@ -15,6 +16,15 @@ import {RECENTS_SECTION,} from './constants';+// search() lives in the logs condition builder only; traces and metrics reject it+// as an unsupported operator. Every other function is implemented for all signals.+export function isSupportedFunction(+ functionName: string,+ signal: SignalType,+): boolean {+ return functionName !== QUERY_BUILDER_FUNCTIONS.SEARCH || signal === 'logs';+}+export interface FieldContextPrefixMatch {context: string;remainder: string;diff --git a/frontend/src/constants/antlrQueryConstants.ts b/frontend/src/constants/antlrQueryConstants.tsindex e833cb7186..0854451e6d 100644--- a/frontend/src/constants/antlrQueryConstants.ts+++ b/frontend/src/constants/antlrQueryConstants.ts@@ -41,6 +41,7 @@ export const QUERY_BUILDER_FUNCTIONS = {HASANY: 'hasAny',HASALL: 'hasAll',HASTOKEN: 'hasToken',+ SEARCH: 'search',};export function negateOperator(operatorOrFunction: string): string {diff --git a/frontend/src/lib/recentQueries/normalize.test.ts b/frontend/src/lib/recentQueries/normalize.test.tsindex 5dbac9729f..f1370fbcea 100644--- a/frontend/src/lib/recentQueries/normalize.test.ts+++ b/frontend/src/lib/recentQueries/normalize.test.ts@@ -34,7 +34,7 @@ describe('normalizeFilterExpression', () => {);});- it('lowercases HAS / HASANY / HASALL / HASTOKEN function names', () => {+ it('lowercases HAS / HASANY / HASALL / HASTOKEN / SEARCH function names', () => {expect(normalizeFilterExpression('HAS(tags, "x")')).toBe(normalizeFilterExpression('has(tags, "x")'),);@@ -47,6 +47,9 @@ describe('normalizeFilterExpression', () => {expect(normalizeFilterExpression('HASTOKEN(msg, "err")')).toBe(normalizeFilterExpression('hasToken(msg, "err")'),);+ expect(normalizeFilterExpression('SEARCH("err")')).toBe(+ normalizeFilterExpression('search("err")'),+ );});it('lowercases TRUE / FALSE boolean literals', () => {diff --git a/frontend/src/utils/__tests__/queryContextUtils.test.ts b/frontend/src/utils/__tests__/queryContextUtils.test.tsindex da6d22e9e8..8238bd080b 100644--- a/frontend/src/utils/__tests__/queryContextUtils.test.ts+++ b/frontend/src/utils/__tests__/queryContextUtils.test.ts@@ -380,6 +380,19 @@ describe('extractQueryPairs', () => {consoleSpy.mockRestore();});+ it('does not turn a search() term into a pair', () => {+ // The bare form lexes as a KEY; left in it becomes a phantom filter item.+ expect(extractQueryPairs("search('err')")).toStrictEqual([]);+ expect(extractQueryPairs('search(err)')).toStrictEqual([]);+ expect(extractQueryPairs('search(')).toStrictEqual([]);++ expect(+ extractQueryPairs("search(err) AND service.name = 'api'").map(+ (pair) => pair.key,+ ),+ ).toStrictEqual(['service.name']);+ });+it('should treat lowercase exists as non-value operator', () => {const input = 'body exists service.name contains "test"';const result = extractQueryPairs(input);@@ -821,3 +834,17 @@ describe('getQueryContextAtCursor - partial operator', () => {expect(ctx.operatorToken).toBe('k');});});++describe('getQueryContextAtCursor - function context', () => {+ // Each function keyword gets its own lexer token, and every one has to be+ // registered as a function token for the editor to offer the function list.+ it.each(['has', 'hasAny', 'hasAll', 'hasToken', 'search'])(+ 'resolves %s to function context',+ (functionName) => {+ const ctx = getQueryContextAtCursor(functionName, functionName.length);++ expect(ctx.isInFunction).toBe(true);+ expect(ctx.isInKey).toBe(false);+ },+ );+});diff --git a/frontend/src/utils/queryContextUtils.ts b/frontend/src/utils/queryContextUtils.tsindex cd44666d85..b0ca384fbf 100644--- a/frontend/src/utils/queryContextUtils.ts+++ b/frontend/src/utils/queryContextUtils.ts@@ -1279,6 +1279,41 @@ export function getQueryContextAtCursor(}}+// The grammar skips whitespace outright, so hidden-channel tokens do not reach the+// stream today -- but the rest of this file guards against them, so keep the+// assumption in one place rather than spread across callers.+function nextVisibleIndex(tokens: IToken[], start: number): number {+ let index = start;+ while (index < tokens.length && tokens[index].channel !== 0) {+ index += 1;+ }+ return index;+}++// Returns the token index just past the parenthesised argument list at+// argumentStart, or argumentStart when none follows (a call the user is still+// typing). An unclosed list consumes the remainder.+function indexPastArguments(tokens: IToken[], argumentStart: number): number {+ let index = nextVisibleIndex(tokens, argumentStart);+ if (index >= tokens.length || tokens[index].type !== FilterQueryLexer.LPAREN) {+ return argumentStart;+ }++ let depth = 0;+ for (; index < tokens.length; index++) {+ if (tokens[index].type === FilterQueryLexer.LPAREN) {+ depth += 1;+ } else if (tokens[index].type === FilterQueryLexer.RPAREN) {+ depth -= 1;+ if (depth === 0) {+ return index + 1;+ }+ }+ }++ return index;+}+/*** Extracts all key-operator-value triplets from a query string* This is useful for getting value suggestions based on the current key and operator@@ -1324,6 +1359,14 @@ export function extractQueryPairs(query: string): IQueryPair[] {continue;}+ // A search() term is free text, not a key: the bare form search(x) lexes+ // as one, and left in it surfaces as a phantom filter item -- the log+ // detail drawer rebuilds its filters from these pairs.+ if (token.type === FilterQueryLexer.SEARCH) {+ iterator = indexPastArguments(allTokens, iterator);+ continue;+ }+// If token is a KEY, start a new pairif (token.type === FilterQueryLexer.KEY &&diff --git a/frontend/src/utils/tokenUtils.ts b/frontend/src/utils/tokenUtils.tsindex a70d185b1e..822fb3714a 100644--- a/frontend/src/utils/tokenUtils.ts+++ b/frontend/src/utils/tokenUtils.ts@@ -77,6 +77,7 @@ export function isFunctionToken(tokenType: number): boolean {FilterQueryLexer.HASANY,FilterQueryLexer.HASALL,FilterQueryLexer.HASTOKEN,+ FilterQueryLexer.SEARCH,].includes(tokenType);}