From 5ba9ccb5a7420f1804dda53771f412d13c84ff54 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:20:19 +0100 Subject: [PATCH] Loki: Add label_replace option to query builder (#47247) * Add label_replace operation * Update --- .../querybuilder/LokiQueryModeller.test.ts | 9 ++++++++ .../loki/querybuilder/operations.ts | 21 +++++++++++++++++++ .../datasource/loki/querybuilder/types.ts | 1 + 3 files changed, 31 insertions(+) diff --git a/public/app/plugins/datasource/loki/querybuilder/LokiQueryModeller.test.ts b/public/app/plugins/datasource/loki/querybuilder/LokiQueryModeller.test.ts index 79b83388ee9..8690854ad57 100644 --- a/public/app/plugins/datasource/loki/querybuilder/LokiQueryModeller.test.ts +++ b/public/app/plugins/datasource/loki/querybuilder/LokiQueryModeller.test.ts @@ -121,6 +121,15 @@ describe('LokiQueryModeller', () => { ).toBe('{app="grafana"} | line_format "{{.status_code}}"'); }); + it('Can render with label_format operation', () => { + expect( + modeller.renderQuery({ + labels: [{ label: 'app', op: '=', value: 'grafana' }], + operations: [{ id: LokiOperationId.LabelFormat, params: ['new', 'old'] }], + }) + ).toBe('{app="grafana"} | label_format old=`new`'); + }); + it('Can render simply binary operation with scalar', () => { expect( modeller.renderQuery({ diff --git a/public/app/plugins/datasource/loki/querybuilder/operations.ts b/public/app/plugins/datasource/loki/querybuilder/operations.ts index 24abf7d89e0..c0b9019e1ca 100644 --- a/public/app/plugins/datasource/loki/querybuilder/operations.ts +++ b/public/app/plugins/datasource/loki/querybuilder/operations.ts @@ -84,6 +84,27 @@ export function getOperationDefintions(): QueryBuilderOperationDef[] { [Read the docs](https://grafana.com/docs/loki/latest/logql/log_queries/#line-format-expression) for more. `, }, + { + id: LokiOperationId.LabelFormat, + name: 'Label format', + params: [ + { name: 'Label', type: 'string' }, + { name: 'Rename', type: 'string' }, + ], + defaultParams: ['', ''], + alternativesKey: 'format', + category: LokiVisualQueryOperationCategory.Formats, + orderRank: LokiOperationOrder.LineFormats, + renderer: (model, def, innerExpr) => `${innerExpr} | label_format ${model.params[1]}=\`${model.params[0]}\``, + addOperationHandler: addLokiOperation, + explainHandler: () => + `This will change name of label to desired new label. In the example below, label "error_level" will be renamed to "level". + + Example: error_level=\`level\` + + [Read the docs](https://grafana.com/docs/loki/latest/logql/log_queries/#labels-format-expression) for more. + `, + }, { id: LokiOperationId.LineContains, diff --git a/public/app/plugins/datasource/loki/querybuilder/types.ts b/public/app/plugins/datasource/loki/querybuilder/types.ts index 38945329cab..ef506eb4ae1 100644 --- a/public/app/plugins/datasource/loki/querybuilder/types.ts +++ b/public/app/plugins/datasource/loki/querybuilder/types.ts @@ -31,6 +31,7 @@ export enum LokiOperationId { Json = 'json', Logfmt = 'logfmt', LineFormat = 'line_format', + LabelFormat = 'label_format', Rate = 'rate', CountOverTime = 'count_over_time', SumOverTime = 'sum_over_time',