Skip to content

Commit

Permalink
Loki: Add support for range aggregations with by grouping (#56184) (#…
Browse files Browse the repository at this point in the history
…56369)

* Loki: Fix unwrapped range operations with grouping

* Update, fix tests

* Fix modeller

(cherry picked from commit 4469572)

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
  • Loading branch information
grafanabot and ivanahuckova committed Oct 5, 2022
1 parent 4d3408f commit 5518641
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class LokiQueryModeller extends LokiAndPromQueryModellerBase {
{ id: LokiOperationId.LabelFilterNoErrors, params: [] },
{ id: LokiOperationId.Unwrap, params: ['latency'] },
{ id: LokiOperationId.LabelFilterNoErrors, params: [] },
{ id: LokiOperationId.QuantileOverTime, params: [0.5, '$__interval'] },
{ id: LokiOperationId.QuantileOverTime, params: ['$__interval', 0.5] },
{ id: LokiOperationId.Sum, params: [] },
],
},
Expand Down
124 changes: 124 additions & 0 deletions public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { createRangeOperation, createRangeOperationWithGrouping } from './operationUtils';
import { LokiVisualQueryOperationCategory } from './types';

describe('createRangeOperation', () => {
it('should create basic range operation without possible grouping', () => {
expect(createRangeOperation('test_range_operation')).toMatchObject({
id: 'test_range_operation',
name: 'Test range operation',
params: [{ name: 'Range', type: 'string' }],
defaultParams: ['$__interval'],
alternativesKey: 'range function',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});
});

it('should create basic range operation with possible grouping', () => {
expect(createRangeOperation('test_range_operation', true)).toMatchObject({
id: 'test_range_operation',
name: 'Test range operation',
params: [
{ name: 'Range', type: 'string' },
{
name: 'By label',
type: 'string',
restParam: true,
optional: true,
},
],
defaultParams: ['$__interval'],
alternativesKey: 'range function',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});
});

it('should create range operation for quantile_over_time', () => {
expect(createRangeOperation('quantile_over_time', true)).toMatchObject({
id: 'quantile_over_time',
name: 'Quantile over time',
params: [
{ name: 'Range', type: 'string' },
{ name: 'Quantile', type: 'number' },
{ name: 'By label', type: 'string', restParam: true, optional: true },
],
defaultParams: ['$__interval', '0.95'],
alternativesKey: 'range function',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});
});
});

describe('createRangeOperationWithGrouping', () => {
it('returns correct operation definitions with overrides and params', () => {
const operations = createRangeOperationWithGrouping('quantile_over_time');
expect(operations).toHaveLength(3);
expect(operations[0]).toMatchObject({
id: 'quantile_over_time',
name: 'Quantile over time',
params: [
{ name: 'Range', type: 'string' },
{ name: 'Quantile', type: 'number' },
{ name: 'By label', type: 'string', restParam: true, optional: true },
],
defaultParams: ['$__interval', '0.95'],
alternativesKey: 'range function',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});

expect(operations[1]).toMatchObject({
id: '__quantile_over_time_by',
name: 'Quantile over time by',
params: [
{ name: 'Range', type: 'string' },
{ name: 'Quantile', type: 'number' },
{ name: 'Label', type: 'string', restParam: true, optional: true },
],
defaultParams: ['$__interval', '0.95', ''],
alternativesKey: 'range function with grouping',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});

expect(operations[2]).toMatchObject({
id: '__quantile_over_time_without',
name: 'Quantile over time without',
params: [
{ name: 'Range', type: 'string' },
{ name: 'Quantile', type: 'number' },
{ name: 'Label', type: 'string', restParam: true, optional: true },
],
defaultParams: ['$__interval', '0.95', ''],
alternativesKey: 'range function with grouping',
category: LokiVisualQueryOperationCategory.RangeFunctions,
});
});

it('returns correct query string using range operation definitions for quantile_over_time with by grouping', () => {
const operations = createRangeOperationWithGrouping('quantile_over_time');
const query = operations[1].renderer(
{ id: '__quantile_over_time_by', params: ['[5m]', '0.95', 'source', 'place'] },
operations[1],
'{job="grafana"}'
);
expect(query).toBe('quantile_over_time(0.95, {job="grafana"} [[5m]]) by (source, place)');
});

it('returns correct query string using range operation definitions for quantile_over_time with without grouping', () => {
const operations = createRangeOperationWithGrouping('quantile_over_time');
const query = operations[2].renderer(
{ id: '__quantile_over_time_without', params: ['[$__interval]', '0.91', 'source', 'place'] },
operations[2],
'{job="grafana"}'
);
expect(query).toBe('quantile_over_time(0.91, {job="grafana"} [[$__interval]]) without (source, place)');
});

it('returns correct query string using range operation definitions for avg_over_time with without grouping', () => {
const operations = createRangeOperationWithGrouping('avg_over_time');
const query = operations[2].renderer(
{ id: '__avg_over_time_without', params: ['[$__interval]', 'source'] },
operations[2],
'{job="grafana"}'
);
expect(query).toBe('avg_over_time({job="grafana"} [[$__interval]]) without (source)');
});
});

0 comments on commit 5518641

Please sign in to comment.