Loki: Remove hardcoded values for LokiOperationId (#53168)

This commit is contained in:
Matias Chomicki 2022-08-03 12:50:43 +02:00 committed by GitHub
parent 805203dba9
commit 7cf2b68e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 67 deletions

View File

@ -56,7 +56,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__line_contains', params: ['line'] }], operations: [{ id: LokiOperationId.LineContains, params: ['line'] }],
}) })
); );
}); });
@ -71,7 +71,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__line_contains', params: ['\\line'] }], operations: [{ id: LokiOperationId.LineContains, params: ['\\line'] }],
}) })
); );
}); });
@ -88,8 +88,8 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: '__line_filter_ip_matches', params: ['|=', '192.168.4.5/16'] }, { id: LokiOperationId.LineFilterIpMatches, params: ['|=', '192.168.4.5/16'] },
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
], ],
}) })
); );
@ -105,7 +105,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__label_filter', params: ['bar', '=', 'baz'] }], operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '=', 'baz'] }],
}) })
); );
}); });
@ -120,7 +120,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__label_filter', params: ['bar', '>=', '8'] }], operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '>=', '8'] }],
}) })
); );
}); });
@ -135,7 +135,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__label_filter_no_errors', params: [] }], operations: [{ id: LokiOperationId.LabelFilterNoErrors, params: [] }],
}) })
); );
}); });
@ -150,7 +150,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: '__label_filter', params: ['bar', '<', '8mb'] }], operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '<', '8mb'] }],
}) })
); );
}); });
@ -179,7 +179,7 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: LokiOperationId.LabelFilterIpMatches, params: ['address', '=', '192.168.4.5/16'] }, { id: LokiOperationId.LabelFilterIpMatches, params: ['address', '=', '192.168.4.5/16'] },
], ],
}) })
@ -196,7 +196,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: 'json', params: [] }], operations: [{ id: LokiOperationId.Json, params: [] }],
}) })
); );
}); });
@ -205,7 +205,7 @@ describe('buildVisualQueryFromString', () => {
const context = buildVisualQueryFromString('{app="frontend"} | json label="value" '); const context = buildVisualQueryFromString('{app="frontend"} | json label="value" ');
expect(context.query).toEqual({ expect(context.query).toEqual({
labels: [{ label: 'app', op: '=', value: 'frontend' }], labels: [{ label: 'app', op: '=', value: 'frontend' }],
operations: [{ id: 'json', params: ['label="value"'] }], operations: [{ id: LokiOperationId.Json, params: ['label="value"'] }],
}); });
}); });
@ -213,7 +213,7 @@ describe('buildVisualQueryFromString', () => {
const context = buildVisualQueryFromString('{app="frontend"} | json label="value", bar="baz", foo="bar" '); const context = buildVisualQueryFromString('{app="frontend"} | json label="value", bar="baz", foo="bar" ');
expect(context.query).toEqual({ expect(context.query).toEqual({
labels: [{ label: 'app', op: '=', value: 'frontend' }], labels: [{ label: 'app', op: '=', value: 'frontend' }],
operations: [{ id: 'json', params: ['label="value"', 'bar="baz"', 'foo="bar"'] }], operations: [{ id: LokiOperationId.Json, params: ['label="value"', 'bar="baz"', 'foo="bar"'] }],
}); });
}); });
@ -230,9 +230,9 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: 'unwrap', params: ['bytes_processed', ''] }, { id: LokiOperationId.Unwrap, params: ['bytes_processed', ''] },
{ id: 'sum_over_time', params: ['1m'] }, { id: LokiOperationId.SumOverTime, params: ['1m'] },
], ],
}) })
); );
@ -251,10 +251,10 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: 'unwrap', params: ['duration', ''] }, { id: LokiOperationId.Unwrap, params: ['duration', ''] },
{ id: '__label_filter_no_errors', params: [] }, { id: LokiOperationId.LabelFilterNoErrors, params: [] },
{ id: 'sum_over_time', params: ['1m'] }, { id: LokiOperationId.SumOverTime, params: ['1m'] },
], ],
}) })
); );
@ -273,10 +273,10 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: 'unwrap', params: ['duration', ''] }, { id: LokiOperationId.Unwrap, params: ['duration', ''] },
{ id: '__label_filter', params: ['label', '=', 'value'] }, { id: LokiOperationId.LabelFilter, params: ['label', '=', 'value'] },
{ id: 'sum_over_time', params: ['1m'] }, { id: LokiOperationId.SumOverTime, params: ['1m'] },
], ],
}) })
); );
@ -296,9 +296,9 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: 'unwrap', params: ['label', 'duration'] }, { id: LokiOperationId.Unwrap, params: ['label', 'duration'] },
{ id: 'sum_over_time', params: ['5m'] }, { id: LokiOperationId.SumOverTime, params: ['5m'] },
], ],
}) })
); );
@ -315,8 +315,8 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'json', params: [] }, { id: LokiOperationId.Json, params: [] },
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
], ],
}) })
); );
@ -333,9 +333,9 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'json', params: [] }, { id: LokiOperationId.Json, params: [] },
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: 'sum', params: [] }, { id: LokiOperationId.Sum, params: [] },
], ],
}) })
); );
@ -352,9 +352,9 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'json', params: [] }, { id: LokiOperationId.Json, params: [] },
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: '__sum_by', params: ['job', 'name'] }, { id: LokiOperationId.SumBy, params: ['job', 'name'] },
], ],
}) })
); );
@ -371,9 +371,9 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'json', params: [] }, { id: LokiOperationId.Json, params: [] },
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: '__sum_without', params: ['job', 'name'] }, { id: LokiOperationId.SumWithout, params: ['job', 'name'] },
], ],
}) })
); );
@ -390,11 +390,11 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: '__line_matches_regex', params: ['abc'] }, { id: LokiOperationId.LineMatchesRegex, params: ['abc'] },
{ id: 'json', params: [] }, { id: LokiOperationId.Json, params: [] },
{ id: '__label_filter', params: ['bar', '=', 'baz'] }, { id: LokiOperationId.LabelFilter, params: ['bar', '=', 'baz'] },
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: 'sum', params: [] }, { id: LokiOperationId.Sum, params: [] },
], ],
}) })
); );
@ -413,11 +413,11 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'logfmt', params: [] }, { id: LokiOperationId.Logfmt, params: [] },
{ id: '__label_filter_no_errors', params: [] }, { id: LokiOperationId.LabelFilterNoErrors, params: [] },
{ id: 'count_over_time', params: ['5m'] }, { id: LokiOperationId.CountOverTime, params: ['5m'] },
{ id: 'sum', params: [] }, { id: LokiOperationId.Sum, params: [] },
{ id: 'topk', params: [10] }, { id: LokiOperationId.TopK, params: [10] },
], ],
}) })
); );
@ -442,7 +442,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: 'rate', params: ['$__interval'] }], operations: [{ id: LokiOperationId.Rate, params: ['$__interval'] }],
}) })
); );
}); });
@ -457,7 +457,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: 'quantile_over_time', params: ['0.99', '1m'] }], operations: [{ id: LokiOperationId.QuantileOverTime, params: ['0.99', '1m'] }],
}) })
); );
}); });
@ -472,7 +472,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: 'line_format', params: ['abc'] }], operations: [{ id: LokiOperationId.LineFormat, params: ['abc'] }],
}) })
); );
}); });
@ -487,7 +487,7 @@ describe('buildVisualQueryFromString', () => {
label: 'app', label: 'app',
}, },
], ],
operations: [{ id: 'label_format', params: ['original', 'renameTo'] }], operations: [{ id: LokiOperationId.LabelFormat, params: ['original', 'renameTo'] }],
}) })
); );
}); });
@ -503,8 +503,8 @@ describe('buildVisualQueryFromString', () => {
}, },
], ],
operations: [ operations: [
{ id: 'label_format', params: ['original', 'renameTo'] }, { id: LokiOperationId.LabelFormat, params: ['original', 'renameTo'] },
{ id: 'label_format', params: ['baz', 'bar'] }, { id: LokiOperationId.LabelFormat, params: ['baz', 'bar'] },
], ],
}) })
); );
@ -514,13 +514,13 @@ describe('buildVisualQueryFromString', () => {
expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / rate({project="foo"}[5m])')).toEqual( expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / rate({project="foo"}[5m])')).toEqual(
noErrors({ noErrors({
labels: [{ op: '=', value: 'bar', label: 'project' }], labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }], operations: [{ id: LokiOperationId.Rate, params: ['5m'] }],
binaryQueries: [ binaryQueries: [
{ {
operator: '/', operator: '/',
query: { query: {
labels: [{ op: '=', value: 'foo', label: 'project' }], labels: [{ op: '=', value: 'foo', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }], operations: [{ id: LokiOperationId.Rate, params: ['5m'] }],
}, },
}, },
], ],
@ -533,8 +533,8 @@ describe('buildVisualQueryFromString', () => {
noErrors({ noErrors({
labels: [{ op: '=', value: 'bar', label: 'project' }], labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [ operations: [
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: '__divide_by', params: [2] }, { id: LokiOperationId.DivideBy, params: [2] },
], ],
}) })
); );
@ -547,21 +547,21 @@ describe('buildVisualQueryFromString', () => {
noErrors({ noErrors({
labels: [{ op: '=', value: 'bar', label: 'project' }], labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [ operations: [
{ id: 'rate', params: ['5m'] }, { id: LokiOperationId.Rate, params: ['5m'] },
{ id: '__multiply_by', params: [2] }, { id: LokiOperationId.MultiplyBy, params: [2] },
], ],
binaryQueries: [ binaryQueries: [
{ {
operator: '/', operator: '/',
query: { query: {
labels: [{ op: '=', value: 'foo', label: 'project' }], labels: [{ op: '=', value: 'foo', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }], operations: [{ id: LokiOperationId.Rate, params: ['5m'] }],
binaryQueries: [ binaryQueries: [
{ {
operator: '+', operator: '+',
query: { query: {
labels: [{ op: '=', value: 'test', label: 'app' }], labels: [{ op: '=', value: 'test', label: 'app' }],
operations: [{ id: 'rate', params: ['1m'] }], operations: [{ id: LokiOperationId.Rate, params: ['1m'] }],
}, },
}, },
], ],

View File

@ -254,7 +254,7 @@ function getLabelFilter(expr: string, node: SyntaxNode): { operation?: QueryBuil
}; };
} }
const id = '__label_filter'; const id = LokiOperationId.LabelFilter;
if (node.firstChild!.name === 'UnitFilter') { if (node.firstChild!.name === 'UnitFilter') {
const filter = node.firstChild!.firstChild; const filter = node.firstChild!.firstChild;
const label = filter!.firstChild; const label = filter!.firstChild;
@ -280,7 +280,7 @@ function getLabelFilter(expr: string, node: SyntaxNode): { operation?: QueryBuil
if (params.join('') === `__error__=`) { if (params.join('') === `__error__=`) {
return { return {
operation: { operation: {
id: '__label_filter_no_errors', id: LokiOperationId.LabelFilterNoErrors,
params: [], params: [],
}, },
}; };
@ -295,7 +295,7 @@ function getLabelFilter(expr: string, node: SyntaxNode): { operation?: QueryBuil
} }
function getLineFormat(expr: string, node: SyntaxNode): QueryBuilderOperation { function getLineFormat(expr: string, node: SyntaxNode): QueryBuilderOperation {
const id = 'line_format'; const id = LokiOperationId.LineFormat;
const string = handleQuotes(getString(expr, node.getChild('String'))); const string = handleQuotes(getString(expr, node.getChild('String')));
return { return {
@ -305,7 +305,7 @@ function getLineFormat(expr: string, node: SyntaxNode): QueryBuilderOperation {
} }
function getLabelFormat(expr: string, node: SyntaxNode): QueryBuilderOperation { function getLabelFormat(expr: string, node: SyntaxNode): QueryBuilderOperation {
const id = 'label_format'; const id = LokiOperationId.LabelFormat;
const renameTo = node.getChild('Identifier'); const renameTo = node.getChild('Identifier');
const op = renameTo!.nextSibling; const op = renameTo!.nextSibling;
const originalLabel = op!.nextSibling; const originalLabel = op!.nextSibling;
@ -339,7 +339,7 @@ function handleUnwrapExpr(
const identifier = convOp.nextSibling; const identifier = convOp.nextSibling;
return { return {
operation: { operation: {
id: 'unwrap', id: LokiOperationId.Unwrap,
params: [getString(expr, identifier), getString(expr, convOp)], params: [getString(expr, identifier), getString(expr, convOp)],
}, },
}; };
@ -347,7 +347,7 @@ function handleUnwrapExpr(
return { return {
operation: { operation: {
id: 'unwrap', id: LokiOperationId.Unwrap,
params: [getString(expr, unwrapChild?.nextSibling), ''], params: [getString(expr, unwrapChild?.nextSibling), ''],
}, },
}; };

View File

@ -67,6 +67,8 @@ export enum LokiOperationId {
LabelFilterNoErrors = '__label_filter_no_errors', LabelFilterNoErrors = '__label_filter_no_errors',
LabelFilterIpMatches = '__label_filter_ip_marches', LabelFilterIpMatches = '__label_filter_ip_marches',
Unwrap = 'unwrap', Unwrap = 'unwrap',
SumBy = '__sum_by',
SumWithout = '__sum_without',
// Binary ops // Binary ops
Addition = '__addition', Addition = '__addition',
Subtraction = '__subtraction', Subtraction = '__subtraction',