Loki: Add unwrap with conversion function to builder (#52639)

* Loki: Add unwrap with conversion operator to builder

* Update explain section

* Update test
This commit is contained in:
Ivana Huckova
2022-07-25 12:51:28 +02:00
committed by GitHub
parent 9d6994c565
commit 53b8e528fc
4 changed files with 97 additions and 29 deletions

View File

@@ -323,16 +323,30 @@ export function getOperationDefinitions(): QueryBuilderOperationDef[] {
{
id: LokiOperationId.Unwrap,
name: 'Unwrap',
params: [{ name: 'Identifier', type: 'string', hideName: true, minWidth: 16, placeholder: 'Label key' }],
defaultParams: [''],
params: [
{ name: 'Identifier', type: 'string', hideName: true, minWidth: 16, placeholder: 'Label key' },
{
name: 'Conversion function',
hideName: true,
type: 'string',
options: ['duration', 'duration_seconds', 'bytes'],
optional: true,
},
],
defaultParams: ['', ''],
alternativesKey: 'format',
category: LokiVisualQueryOperationCategory.Formats,
orderRank: LokiOperationOrder.Unwrap,
renderer: (op, def, innerExpr) => `${innerExpr} | unwrap ${op.params[0]}`,
renderer: (op, def, innerExpr) =>
`${innerExpr} | unwrap ${op.params[1] ? `${op.params[1]}(${op.params[0]})` : op.params[0]}`,
addOperationHandler: addLokiOperation,
explainHandler: (op) => {
let label = String(op.params[0]).length > 0 ? op.params[0] : '<label>';
return `Use the extracted label \`${label}\` as sample values instead of log lines for the subsequent range aggregation.`;
return `Use the extracted label \`${label}\` as sample values instead of log lines for the subsequent range aggregation.${
op.params[1]
? ` Conversion function \`${op.params[1]}\` wrapping \`${label}\` will attempt to convert this label from a specific format (e.g. 3k, 500ms).`
: ''
}`;
},
},
...binaryScalarOperations,

View File

@@ -217,7 +217,7 @@ describe('buildVisualQueryFromString', () => {
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['bytes_processed'] },
{ id: 'unwrap', params: ['bytes_processed', ''] },
{ id: 'sum_over_time', params: ['1m'] },
],
})
@@ -238,7 +238,7 @@ describe('buildVisualQueryFromString', () => {
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['duration'] },
{ id: 'unwrap', params: ['duration', ''] },
{ id: '__label_filter_no_errors', params: [] },
{ id: 'sum_over_time', params: ['1m'] },
],
@@ -260,7 +260,7 @@ describe('buildVisualQueryFromString', () => {
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['duration'] },
{ id: 'unwrap', params: ['duration', ''] },
{ id: '__label_filter', params: ['label', '=', 'value'] },
{ id: 'sum_over_time', params: ['1m'] },
],
@@ -268,18 +268,26 @@ describe('buildVisualQueryFromString', () => {
);
});
it('returns error for query with unwrap and conversion operation', () => {
it('parses query with unwrap and conversion function', () => {
const context = buildVisualQueryFromString(
'sum_over_time({app="frontend"} | logfmt | unwrap duration(label) [5m])'
);
expect(context.errors).toEqual([
{
text: 'Unwrap with conversion operator not supported in query builder: | unwrap duration(label)',
from: 40,
to: 64,
parentType: 'LogRangeExpr',
},
]);
expect(context).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [
{ id: 'logfmt', params: [] },
{ id: 'unwrap', params: ['label', 'duration'] },
{ id: 'sum_over_time', params: ['5m'] },
],
})
);
});
it('parses metrics query with function', () => {

View File

@@ -323,16 +323,21 @@ function handleUnwrapExpr(
}
if (unwrapChild) {
if (unwrapChild?.nextSibling?.type.name === 'ConvOp') {
if (unwrapChild.nextSibling?.type.name === 'ConvOp') {
const convOp = unwrapChild.nextSibling;
const identifier = convOp.nextSibling;
return {
error: 'Unwrap with conversion operator not supported in query builder',
operation: {
id: 'unwrap',
params: [getString(expr, identifier), getString(expr, convOp)],
},
};
}
return {
operation: {
id: 'unwrap',
params: [getString(expr, unwrapChild?.nextSibling)],
params: [getString(expr, unwrapChild?.nextSibling), ''],
},
};
}