From 1a988df1439fd645af6c03791388a23c28123a8c Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Fri, 4 Apr 2025 13:50:54 +0200 Subject: [PATCH] Prometheus: Support new info function in code editor (#97850) * first attempt to have info function * unit test for query modeller * enable info function in devenv * fix unit test * add unit test for info function * throw error when trying to visualize the info function * remove visual query support * Update packages/grafana-prometheus/src/promql.ts Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * Update packages/grafana-prometheus/src/querybuilder/parsing.test.ts Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * update info function detail --------- Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> --- devenv/docker/blocks/prometheus/Dockerfile | 2 +- .../blocks/prometheus/docker-compose.yaml | 1 + .../components/monaco-query-field/promql.ts | 1 + packages/grafana-prometheus/src/promql.ts | 7 +++ .../src/querybuilder/parsing.test.ts | 51 +++++++++++++++++++ .../src/querybuilder/parsing.ts | 9 ++++ .../src/querybuilder/types.ts | 1 + 7 files changed, 71 insertions(+), 1 deletion(-) diff --git a/devenv/docker/blocks/prometheus/Dockerfile b/devenv/docker/blocks/prometheus/Dockerfile index 7fa6476b739..25d37e7e802 100644 --- a/devenv/docker/blocks/prometheus/Dockerfile +++ b/devenv/docker/blocks/prometheus/Dockerfile @@ -1,4 +1,4 @@ -FROM prom/prometheus:v3.1.0 +FROM prom/prometheus:v3.2.1 ADD prometheus.yml /etc/prometheus/ ADD recording.yml /etc/prometheus/ ADD alert.yml /etc/prometheus/ diff --git a/devenv/docker/blocks/prometheus/docker-compose.yaml b/devenv/docker/blocks/prometheus/docker-compose.yaml index 49b64c60fcf..f42734771f6 100644 --- a/devenv/docker/blocks/prometheus/docker-compose.yaml +++ b/devenv/docker/blocks/prometheus/docker-compose.yaml @@ -6,6 +6,7 @@ - "host.docker.internal:host-gateway" command: > --enable-feature=remote-write-receiver + --enable-feature=promql-experimental-functions --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries diff --git a/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts b/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts index 23916c0e9ff..d1ca8752aea 100644 --- a/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts +++ b/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts @@ -97,6 +97,7 @@ const functions = [ 'hour', 'idelta', 'increase', + 'info', 'irate', 'label_join', 'label_replace', diff --git a/packages/grafana-prometheus/src/promql.ts b/packages/grafana-prometheus/src/promql.ts index c973a11f78e..912a009eb64 100644 --- a/packages/grafana-prometheus/src/promql.ts +++ b/packages/grafana-prometheus/src/promql.ts @@ -327,6 +327,13 @@ export const FUNCTIONS = [ documentation: 'Calculates the increase in the time series in the range vector. Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for. The increase is extrapolated to cover the full time range as specified in the range vector selector, so that it is possible to get a non-integer result even if a counter increases only by integer increments.', }, + { + insertText: 'info', + label: 'info', + detail: 'info(v instant-vector, [data-label-selector instant-vector])', + documentation: + 'Returns latest details and metadata about a group of metrics, such as their labels and current values, without doing any calculations', + }, { insertText: 'irate', label: 'irate', diff --git a/packages/grafana-prometheus/src/querybuilder/parsing.test.ts b/packages/grafana-prometheus/src/querybuilder/parsing.test.ts index 0ed20246890..d17a2568f08 100644 --- a/packages/grafana-prometheus/src/querybuilder/parsing.test.ts +++ b/packages/grafana-prometheus/src/querybuilder/parsing.test.ts @@ -3,6 +3,57 @@ import { buildVisualQueryFromString } from './parsing'; import { PromOperationId, PromVisualQuery } from './types'; describe('buildVisualQueryFromString', () => { + describe('info function support', () => { + // Currently, the visual query editor throws an error when parsing the 'info' function + // because this function is only supported in code mode. + // TODO: When visual query editor support for the 'info' function is implemented, + // this test should be updated to expect successful parsing instead of an error. + it('should throw error when trying to parse info function', () => { + expect( + buildVisualQueryFromString( + 'sum by (cluster, sdk_language) ( info( rate(server_req_dur_sec_count{instance="the-instance"}[2m]), {sdk_language="go"} ) )' + ) + ).toEqual({ + query: { + labels: [ + { + label: 'instance', + op: '=', + value: 'the-instance', + }, + { + label: 'sdk_language', + op: '=', + value: 'go', + }, + ], + metric: 'server_req_dur_sec_count', + operations: [ + { + id: 'rate', + params: ['2m'], + }, + { + id: 'info', + params: [], + }, + { + id: '__sum_by', + params: ['cluster', 'sdk_language'], + }, + ], + }, + errors: [ + { + from: 33, + text: 'Query parsing is ambiguous.', + to: 121, + }, + ], + }); + }); + }); + describe('utf8 support', () => { it('supports uts-8 label names', () => { expect(buildVisualQueryFromString('{"glück:🍀.dot"="luck"} == 11')).toEqual({ diff --git a/packages/grafana-prometheus/src/querybuilder/parsing.ts b/packages/grafana-prometheus/src/querybuilder/parsing.ts index a282990da83..42713dfb986 100644 --- a/packages/grafana-prometheus/src/querybuilder/parsing.ts +++ b/packages/grafana-prometheus/src/querybuilder/parsing.ts @@ -256,6 +256,15 @@ function handleFunction(expr: string, node: SyntaxNode, context: Context) { const nameNode = node.getChild(FunctionIdentifier); const funcName = getString(expr, nameNode); + // Visual query builder doesn't support nested queries and so info function. + if (funcName === 'info') { + context.errors.push({ + text: 'Query parsing is ambiguous.', + from: node.from, + to: node.to, + }); + } + const body = node.getChild(FunctionCallBody); const params = []; let interval = ''; diff --git a/packages/grafana-prometheus/src/querybuilder/types.ts b/packages/grafana-prometheus/src/querybuilder/types.ts index 58774082743..a00e0cf3098 100644 --- a/packages/grafana-prometheus/src/querybuilder/types.ts +++ b/packages/grafana-prometheus/src/querybuilder/types.ts @@ -77,6 +77,7 @@ export enum PromOperationId { Hour = 'hour', Idelta = 'idelta', Increase = 'increase', + Info = 'info', Irate = 'irate', LabelJoin = 'label_join', LabelReplace = 'label_replace',