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
This commit is contained in:
Brendan O'Handley 2024-04-15 14:26:51 -05:00 committed by GitHub
parent 2aedd9dacf
commit 7c156274cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 89 additions and 0 deletions

View File

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

View File

@ -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);

View File

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

View File

@ -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(

View File

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