Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loki: Implement decolorize logql operation #68972

Merged
merged 2 commits into from May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -258,7 +258,7 @@
"@grafana/faro-core": "1.0.2",
"@grafana/faro-web-sdk": "1.0.2",
"@grafana/google-sdk": "0.1.1",
"@grafana/lezer-logql": "0.1.3",
"@grafana/lezer-logql": "0.1.5",
"@grafana/monaco-logql": "^0.0.7",
"@grafana/runtime": "workspace:*",
"@grafana/scenes": "^0.7.0",
Expand Down
Expand Up @@ -118,6 +118,12 @@ const afterSelectorCompletions = [
type: 'PIPE_OPERATION',
documentation: 'Operator docs',
},
{
insertText: '| decolorize',
label: 'decolorize',
type: 'PIPE_OPERATION',
documentation: 'Operator docs',
},
];

function buildAfterSelectorCompletions(
Expand Down
Expand Up @@ -281,6 +281,13 @@ export async function getAfterSelectorCompletions(
documentation: explainOperator(LokiOperationId.Unwrap),
});

completions.push({
type: 'PIPE_OPERATION',
label: 'decolorize',
insertText: `${prefix}decolorize`,
documentation: explainOperator(LokiOperationId.Decolorize),
});

// Let's show label options only if query has parser
if (hasQueryParser) {
extractedLabelKeys.forEach((key) => {
Expand Down
12 changes: 12 additions & 0 deletions public/app/plugins/datasource/loki/querybuilder/operations.ts
Expand Up @@ -474,6 +474,18 @@ Example: \`\`error_level=\`level\` \`\`
}`;
},
},
{
id: LokiOperationId.Decolorize,
name: 'Decolorize',
params: [],
defaultParams: [],
alternativesKey: 'format',
category: LokiVisualQueryOperationCategory.Formats,
orderRank: LokiOperationOrder.PipeOperations,
renderer: (op, def, innerExpr) => `${innerExpr} | decolorize`,
addOperationHandler: addLokiOperation,
explainHandler: () => `This will remove ANSI color codes from log lines.`,
},
...binaryScalarOperations,
{
id: LokiOperationId.NestedQuery,
Expand Down
34 changes: 34 additions & 0 deletions public/app/plugins/datasource/loki/querybuilder/parsing.test.ts
Expand Up @@ -304,6 +304,40 @@ describe('buildVisualQueryFromString', () => {
);
});

it('parses query with with only decolorize', () => {
expect(buildVisualQueryFromString('{app="frontend"} | decolorize')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Decolorize, params: [] }],
})
);
});

it('parses query with with decolorize and other operations', () => {
expect(buildVisualQueryFromString('{app="frontend"} | logfmt | decolorize | __error__="')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [
{ id: LokiOperationId.Logfmt, params: [] },
{ id: LokiOperationId.Decolorize, params: [] },
{ id: LokiOperationId.LabelFilterNoErrors, params: [] },
],
})
);
});

it('parses metrics query with function', () => {
expect(buildVisualQueryFromString('rate({app="frontend"} | json [5m])')).toEqual(
noErrors({
Expand Down
15 changes: 15 additions & 0 deletions public/app/plugins/datasource/loki/querybuilder/parsing.ts
Expand Up @@ -7,6 +7,7 @@ import {
Bool,
By,
ConvOp,
Decolorize,
Filter,
FilterOp,
Grouping,
Expand Down Expand Up @@ -176,6 +177,11 @@ export function handleExpression(expr: string, node: SyntaxNode, context: Contex
break;
}

case Decolorize: {
visQuery.operations.push(getDecolorize());
break;
}

case RangeAggregationExpr: {
visQuery.operations.push(handleRangeAggregation(expr, node, context));
break;
Expand Down Expand Up @@ -368,6 +374,15 @@ function getLabelFormat(expr: string, node: SyntaxNode): QueryBuilderOperation {
};
}

function getDecolorize(): QueryBuilderOperation {
const id = LokiOperationId.Decolorize;

return {
id,
params: [],
};
}

function handleUnwrapExpr(
expr: string,
node: SyntaxNode,
Expand Down
1 change: 1 addition & 0 deletions public/app/plugins/datasource/loki/querybuilder/types.ts
Expand Up @@ -40,6 +40,7 @@ export enum LokiOperationId {
Unpack = 'unpack',
LineFormat = 'line_format',
LabelFormat = 'label_format',
Decolorize = 'decolorize',
Rate = 'rate',
RateCounter = 'rate_counter',
CountOverTime = 'count_over_time',
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Expand Up @@ -3408,12 +3408,12 @@ __metadata:
languageName: node
linkType: hard

"@grafana/lezer-logql@npm:0.1.3":
version: 0.1.3
resolution: "@grafana/lezer-logql@npm:0.1.3"
"@grafana/lezer-logql@npm:0.1.5":
version: 0.1.5
resolution: "@grafana/lezer-logql@npm:0.1.5"
peerDependencies:
"@lezer/lr": ^1.0.0
checksum: 160e4039ce7dd0cd304d37608c0764c24d61030a84636161161bb7cd5cd2d6f064cd3156e8814ed4cb5b8ddebeb4dbae77120f9a3eaea6afe889d99f6dd23543
checksum: ac0a842c06add812b583e35f0cd4a6a0272adc1a34b681ceb94fbde3e63ed93db72ef4ee9f6531a781c380530b98b2a178736160baa251262a240d11f3afb211
languageName: node
linkType: hard

Expand Down Expand Up @@ -18055,7 +18055,7 @@ __metadata:
"@grafana/faro-core": 1.0.2
"@grafana/faro-web-sdk": 1.0.2
"@grafana/google-sdk": 0.1.1
"@grafana/lezer-logql": 0.1.3
"@grafana/lezer-logql": 0.1.5
"@grafana/monaco-logql": ^0.0.7
"@grafana/runtime": "workspace:*"
"@grafana/scenes": ^0.7.0
Expand Down