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',