Loki: Fix unwrap parsing (#49732)

This commit is contained in:
Ivana Huckova 2022-05-27 14:00:51 +02:00 committed by GitHub
parent d5a327f43d
commit 745b101d41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 21 deletions

View File

@ -253,7 +253,7 @@
"@grafana/e2e-selectors": "workspace:*",
"@grafana/experimental": "^0.0.2-canary.30",
"@grafana/google-sdk": "0.0.3",
"@grafana/lezer-logql": "^0.0.11",
"@grafana/lezer-logql": "^0.0.12",
"@grafana/runtime": "workspace:*",
"@grafana/schema": "workspace:*",
"@grafana/slate-react": "0.22.10-grafana",

View File

@ -220,6 +220,50 @@ describe('buildVisualQueryFromString', () => {
);
});
it('parses query with with unwrap and error filter', () => {
expect(
buildVisualQueryFromString('sum_over_time({app="frontend"} | logfmt | unwrap duration | __error__="" [1m])')
).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['duration'] },
{ id: '__label_filter_no_errors', params: [] },
{ id: 'sum_over_time', params: ['1m'] },
],
})
);
});
it('parses query with with unwrap and label filter', () => {
expect(
buildVisualQueryFromString('sum_over_time({app="frontend"} | logfmt | unwrap duration | label="value" [1m])')
).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['duration'] },
{ id: '__label_filter', params: ['label', '=', 'value'] },
{ id: 'sum_over_time', params: ['1m'] },
],
})
);
});
it('returns error for query with unwrap and conversion operation', () => {
const context = buildVisualQueryFromString(
'sum_over_time({app="frontend"} | logfmt | unwrap duration(label) [5m])'

View File

@ -120,7 +120,7 @@ export function handleExpression(expr: string, node: SyntaxNode, context: Contex
}
case 'UnwrapExpr': {
const { operation, error } = getUnwrap(expr, node);
const { operation, error } = handleUnwrapExpr(expr, node, context);
if (operation) {
visQuery.operations.push(operation);
}
@ -297,25 +297,40 @@ function getLabelFormat(expr: string, node: SyntaxNode): QueryBuilderOperation {
};
}
function getUnwrap(expr: string, node: SyntaxNode): { operation?: QueryBuilderOperation; error?: string } {
// Check for nodes not supported in visual builder and return error
if (node.getChild('ConvOp')) {
function handleUnwrapExpr(
expr: string,
node: SyntaxNode,
context: Context
): { operation?: QueryBuilderOperation; error?: string } {
const unwrapExprChild = node.getChild('UnwrapExpr');
const labelFilterChild = node.getChild('LabelFilter');
const unwrapChild = node.getChild('Unwrap');
if (unwrapExprChild) {
handleExpression(expr, unwrapExprChild, context);
}
if (labelFilterChild) {
handleExpression(expr, labelFilterChild, context);
}
if (unwrapChild) {
if (unwrapChild?.nextSibling?.type.name === 'ConvOp') {
return {
error: 'Unwrap with conversion operator not supported in query builder',
};
}
return {
error: 'Unwrap with conversion operator not supported in query builder',
operation: {
id: 'unwrap',
params: [getString(expr, unwrapChild?.nextSibling)],
},
};
}
const id = 'unwrap';
const string = getString(expr, node.getChild('Identifier'));
return {
operation: {
id,
params: [string],
},
};
return {};
}
function handleRangeAggregation(expr: string, node: SyntaxNode, context: Context) {
const nameNode = node.getChild('RangeOp');
const funcName = getString(expr, nameNode);

View File

@ -4011,14 +4011,14 @@ __metadata:
languageName: node
linkType: hard
"@grafana/lezer-logql@npm:^0.0.11":
version: 0.0.11
resolution: "@grafana/lezer-logql@npm:0.0.11"
"@grafana/lezer-logql@npm:^0.0.12":
version: 0.0.12
resolution: "@grafana/lezer-logql@npm:0.0.12"
dependencies:
lezer: ^0.13.5
peerDependencies:
"@lezer/lr": ^0.15.8
checksum: 0427e59528ea5092e53a70b9b6be37b0ed29ca4c0ddf452cc192f8bd14d21a725ba1959b2e33e7567b42e4d0391cff4e4f36c249d7099a157d0f1e9093ba64e4
checksum: d28780b41a97b69a427390ef746e09a131e6a151be0894763e5cfe26fa16ae37fa5fe7409ea956f5e29974a5966162318a51bcd47422e9a3e609ca43340bbdcb
languageName: node
linkType: hard
@ -19715,7 +19715,7 @@ __metadata:
"@grafana/eslint-config": 3.0.0
"@grafana/experimental": ^0.0.2-canary.30
"@grafana/google-sdk": 0.0.3
"@grafana/lezer-logql": ^0.0.11
"@grafana/lezer-logql": ^0.0.12
"@grafana/runtime": "workspace:*"
"@grafana/schema": "workspace:*"
"@grafana/slate-react": 0.22.10-grafana