Loki: Add label_replace option to query builder (#47247)

* Add label_replace operation

* Update
pull/47283/head
Ivana Huckova 3 years ago committed by GitHub
parent 962eba39ac
commit 5ba9ccb5a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      public/app/plugins/datasource/loki/querybuilder/LokiQueryModeller.test.ts
  2. 21
      public/app/plugins/datasource/loki/querybuilder/operations.ts
  3. 1
      public/app/plugins/datasource/loki/querybuilder/types.ts

@ -121,6 +121,15 @@ describe('LokiQueryModeller', () => {
).toBe('{app="grafana"} | line_format "{{.status_code}}"'); ).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', () => { it('Can render simply binary operation with scalar', () => {
expect( expect(
modeller.renderQuery({ modeller.renderQuery({

@ -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. [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, id: LokiOperationId.LineContains,

@ -31,6 +31,7 @@ export enum LokiOperationId {
Json = 'json', Json = 'json',
Logfmt = 'logfmt', Logfmt = 'logfmt',
LineFormat = 'line_format', LineFormat = 'line_format',
LabelFormat = 'label_format',
Rate = 'rate', Rate = 'rate',
CountOverTime = 'count_over_time', CountOverTime = 'count_over_time',
SumOverTime = 'sum_over_time', SumOverTime = 'sum_over_time',

Loading…
Cancel
Save