From 7c156274cddd0b2174c7189b885f515304e2a7c2 Mon Sep 17 00:00:00 2001 From: Brendan O'Handley Date: Mon, 15 Apr 2024 14:26:51 -0500 Subject: [PATCH] Prometheus: Add native histogram functions (#86002) * add native histogram functions to code editor * add native histogram function types for query builder * add functions to query builder * add test to show parsing breaks from code to builder * add histogram_avg to code editor * add histogram_avg to builder and make parity between package and core * add functions to hard coded promql file for highlighting * remove native histogram test so that it can be added in #85942 * remove functions from core prometheus js to prevent merge conflict in #86080 * use xit for test instead of removing it --- .../components/monaco-query-field/promql.ts | 6 +++ packages/grafana-prometheus/src/promql.ts | 38 +++++++++++++++++++ .../src/querybuilder/operations.ts | 17 +++++++++ .../src/querybuilder/parsing.test.ts | 22 +++++++++++ .../src/querybuilder/types.ts | 6 +++ 5 files changed, 89 insertions(+) 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 af7ab116c95..c7697fbfb3c 100644 --- a/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts +++ b/packages/grafana-prometheus/src/components/monaco-query-field/promql.ts @@ -84,6 +84,12 @@ const functions = [ 'exp', 'floor', 'histogram_quantile', + 'histogram_avg', + 'histogram_count', + 'histogram_sum', + 'histogram_fraction', + 'histogram_stddev', + 'histogram_stdvar', 'holt_winters', 'hour', 'idelta', diff --git a/packages/grafana-prometheus/src/promql.ts b/packages/grafana-prometheus/src/promql.ts index c37f52796ba..a9e89ee5f67 100644 --- a/packages/grafana-prometheus/src/promql.ts +++ b/packages/grafana-prometheus/src/promql.ts @@ -534,6 +534,44 @@ export const FUNCTIONS = [ detail: 'present_over_time(range-vector)', documentation: 'The value 1 for any series in the specified interval.', }, + { + insertText: 'histogram_avg', + label: 'histogram_avg', + detail: 'histogram_avg(v instant-vector)', + documentation: + 'Returns the arithmetic average of observed values stored in a native histogram. Samples that are not native histograms are ignored and do not show up in the returned vector.', + }, + { + insertText: 'histogram_count', + label: 'histogram_count', + detail: 'histogram_count(v instant-vector)', + documentation: 'Returns the count of observations stored in a native histogram.', + }, + { + insertText: 'histogram_sum', + label: 'histogram_sum', + detail: 'histogram_sum(v instant-vector)', + documentation: 'Returns the sum of observations stored in a native histogram.', + }, + { + insertText: 'histogram_fraction', + label: 'histogram_fraction', + detail: 'histogram_fraction(lower scalar, upper scalar, v instant-vector)', + documentation: 'Returns the estimated fraction of observations between the provided lower and upper values.', + }, + { + insertText: 'histogram_stddev', + label: 'histogram_stddev', + detail: 'histogram_stddev(v instant-vector)', + documentation: + 'Returns the estimated standard deviation of observations in a native histogram, based on the geometric mean of the buckets where the observations lie.', + }, + { + insertText: 'histogram_stdvar', + label: 'histogram_stdvar', + detail: 'histogram_stdvar(v instant-vector)', + documentation: 'Returns the estimated standard variance of observations in a native histogram.', + }, ]; export const PROM_KEYWORDS = FUNCTIONS.map((keyword) => keyword.label); diff --git a/packages/grafana-prometheus/src/querybuilder/operations.ts b/packages/grafana-prometheus/src/querybuilder/operations.ts index 3b445efa37e..e4a3dffacbd 100644 --- a/packages/grafana-prometheus/src/querybuilder/operations.ts +++ b/packages/grafana-prometheus/src/querybuilder/operations.ts @@ -28,6 +28,23 @@ export function getOperationDefinitions(): QueryBuilderOperationDef[] { renderer: functionRendererLeft, addOperationHandler: defaultAddOperationHandler, }, + createFunction({ id: PromOperationId.HistogramAvg }), + createFunction({ id: PromOperationId.HistogramCount }), + createFunction({ id: PromOperationId.HistogramSum }), + { + id: PromOperationId.HistogramFraction, + name: 'Histogram fraction', + params: [ + { name: 'Lower scalar', type: 'number' }, + { name: 'Upper scalar', type: 'number' }, + ], + defaultParams: [0.0, 0.2], + category: PromVisualQueryOperationCategory.Functions, + renderer: functionRendererLeft, + addOperationHandler: defaultAddOperationHandler, + }, + createFunction({ id: PromOperationId.HistogramStddev }), + createFunction({ id: PromOperationId.HistogramStdvar }), { id: PromOperationId.LabelReplace, name: 'Label replace', diff --git a/packages/grafana-prometheus/src/querybuilder/parsing.test.ts b/packages/grafana-prometheus/src/querybuilder/parsing.test.ts index 08fa72b7b8a..fbd094bb5ff 100644 --- a/packages/grafana-prometheus/src/querybuilder/parsing.test.ts +++ b/packages/grafana-prometheus/src/querybuilder/parsing.test.ts @@ -290,6 +290,28 @@ describe('buildVisualQueryFromString', () => { }); }); + // enable in #85942 when updated lezer parser is merged + xit('parses a native histogram function correctly', () => { + expect( + buildVisualQueryFromString('histogram_count(rate(counters_logins{app="backend"}[$__rate_interval]))') + ).toEqual({ + errors: [], + query: { + metric: 'counters_logins', + labels: [{ label: 'app', op: '=', value: 'backend' }], + operations: [ + { + id: 'rate', + params: ['$__rate_interval'], + }, + { + id: 'histogram_quantile', + }, + ], + }, + }); + }); + it('parses function with multiple arguments', () => { expect( buildVisualQueryFromString( diff --git a/packages/grafana-prometheus/src/querybuilder/types.ts b/packages/grafana-prometheus/src/querybuilder/types.ts index 24750f21a96..773b22373a4 100644 --- a/packages/grafana-prometheus/src/querybuilder/types.ts +++ b/packages/grafana-prometheus/src/querybuilder/types.ts @@ -63,6 +63,12 @@ export enum PromOperationId { Floor = 'floor', Group = 'group', HistogramQuantile = 'histogram_quantile', + HistogramAvg = 'histogram_avg', + HistogramCount = 'histogram_count', + HistogramSum = 'histogram_sum', + HistogramFraction = 'histogram_fraction', + HistogramStddev = 'histogram_stddev', + HistogramStdvar = 'histogram_stdvar', HoltWinters = 'holt_winters', Hour = 'hour', Idelta = 'idelta',