diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 665f8d89c8c..2066bece06a 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -1180,6 +1180,22 @@ describe('LokiDatasource', () => { defaultAdHocFilters ); }); + it('should add the filter after other label filters', () => { + assertAdHocFilters( + '{bar="baz"} | logfmt | test="value" | line_format "test"', + '{bar="baz"} | logfmt | test="value" | job=`grafana` | line_format "test"', + ds, + defaultAdHocFilters + ); + }); + it('should add the filter after label_format', () => { + assertAdHocFilters( + '{bar="baz"} | logfmt | test="value" | label_format process="{{.process}}"', + '{bar="baz"} | logfmt | test="value" | label_format process="{{.process}}" | job=`grafana`', + ds, + defaultAdHocFilters + ); + }); }); }); diff --git a/public/app/plugins/datasource/loki/modifyQuery.test.ts b/public/app/plugins/datasource/loki/modifyQuery.test.ts index 90f6e8d876b..2a1598dcc20 100644 --- a/public/app/plugins/datasource/loki/modifyQuery.test.ts +++ b/public/app/plugins/datasource/loki/modifyQuery.test.ts @@ -43,6 +43,8 @@ describe('addLabelToQuery()', () => { ${'{foo="bar"} | logfmt | x="y"'} | ${'query with parser and label filter'} | ${'bar'} | ${'='} | ${'baz'} | ${'{foo="bar"} | logfmt | x="y" | bar=`baz`'} ${'rate({foo="bar"} | logfmt [5m])'} | ${'metric query with parser'} | ${'bar'} | ${'='} | ${'baz'} | ${'rate({foo="bar"} | logfmt | bar=`baz` [5m])'} ${'sum by(host) (rate({foo="bar"} | logfmt | x="y" | line_format "{{.status}}" [5m]))'} | ${'metric query with parser'} | ${'bar'} | ${'='} | ${'baz'} | ${'sum by(host) (rate({foo="bar"} | logfmt | x="y" | bar=`baz` | line_format "{{.status}}" [5m]))'} + ${'sum by(host) (rate({foo="bar"} | logfmt | x="y" | label_format process="{{.process}}" [5m]))'} | ${'metric query with parser and label format'} | ${'bar'} | ${'='} | ${'baz'} | ${'sum by(host) (rate({foo="bar"} | logfmt | x="y" | label_format process="{{.process}}" | bar=`baz` [5m]))'} + ${'{foo="bar"} | logfmt | x="y" | label_format process="{{.process}}"'} | ${'query with parser and label format'} | ${'bar'} | ${'='} | ${'baz'} | ${'{foo="bar"} | logfmt | x="y" | label_format process="{{.process}}" | bar=`baz`'} ${'{foo="bar"} | logfmt | line_format "{{.status}}"'} | ${'do not add filter to line_format expressions in query with parser'} | ${'bar'} | ${'='} | ${'baz'} | ${'{foo="bar"} | logfmt | bar=`baz` | line_format "{{.status}}"'} ${'{foo="bar"} | logfmt | line_format "{{status}}"'} | ${'do not add filter to line_format expressions in query with parser'} | ${'bar'} | ${'='} | ${'baz'} | ${'{foo="bar"} | logfmt | bar=`baz` | line_format "{{status}}"'} ${'{}'} | ${'query without stream selector'} | ${'bar'} | ${'='} | ${'baz'} | ${'{bar="baz"}'} diff --git a/public/app/plugins/datasource/loki/modifyQuery.ts b/public/app/plugins/datasource/loki/modifyQuery.ts index 130c29c0a68..2f8e2c8c72b 100644 --- a/public/app/plugins/datasource/loki/modifyQuery.ts +++ b/public/app/plugins/datasource/loki/modifyQuery.ts @@ -21,6 +21,7 @@ import { JsonExpressionParser, LogfmtExpressionParser, Expr, + LabelFormatExpr, } from '@grafana/lezer-logql'; import { unescapeLabelValue } from './languageUtils'; @@ -162,6 +163,8 @@ export function addLabelToQuery( const parserPositions = getParserPositions(query); const labelFilterPositions = getLabelFilterPositions(query); const hasStreamSelectorMatchers = getMatcherInStreamPositions(query); + // For non-indexed labels we want to add them after label_format to, for example, allow ad-hoc filters to use formatted labels + const labelFormatPositions = getNodePositionsFromQuery(query, [LabelFormatExpr]); const everyStreamSelectorHasMatcher = streamSelectorPositions.every((streamSelectorPosition) => hasStreamSelectorMatchers.some( (matcherPosition) => @@ -175,6 +178,7 @@ export function addLabelToQuery( ...streamSelectorPositions, ...labelFilterPositions, ...parserPositions, + ...labelFormatPositions, ]); return addFilterAsLabelFilter(query, lastPositionsPerExpression, filter); @@ -191,6 +195,7 @@ export function addLabelToQuery( const lastPositionsPerExpression = getLastPositionPerExpression(query, [ ...parserPositions, ...labelFilterPositions, + ...labelFormatPositions, ]); return addFilterAsLabelFilter(query, lastPositionsPerExpression, filter);