mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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,
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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), ''],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user