Loki: Add label filters after label_format if present (#86124)

* Loki ad-hoc filters: add filters after label_format

* Chore: add comment explaining rationale

* Formatting

* Formatting
This commit is contained in:
Matias Chomicki
2024-04-19 17:31:38 +02:00
committed by GitHub
parent 8f38ef70ce
commit aa73140564
3 changed files with 23 additions and 0 deletions
@@ -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
);
});
});
});
@@ -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"}'}
@@ -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);