Commit 60e7b63c by Lukas Siatka Committed by GitHub

Loki/Prometheus: updates addLabelToQuery method to prevent it from adding labels…

Loki/Prometheus: updates addLabelToQuery method to prevent it from adding labels outside of selector when using Loki datasource (#25059)

* Chore: updates add label to query method in prometheus datasource to fix loki label insert

* Chore: adds addLabelToQuery test covering differences between adding label to loki vs non-loki queries

* Chore: adds an additional comment to addLabelToQuery

* Chore: renames isLokiDatasource to hasNoMetrics in addLabelToQuery
parent 9a59f387
...@@ -347,17 +347,16 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> { ...@@ -347,17 +347,16 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
let expression = query.expr ?? ''; let expression = query.expr ?? '';
switch (action.type) { switch (action.type) {
case 'ADD_FILTER': { case 'ADD_FILTER': {
expression = addLabelToQuery(expression, action.key, action.value); expression = addLabelToQuery(expression, action.key, action.value, undefined, true);
break; break;
} }
case 'ADD_FILTER_OUT': { case 'ADD_FILTER_OUT': {
expression = addLabelToQuery(expression, action.key, action.value, '!='); expression = addLabelToQuery(expression, action.key, action.value, '!=', true);
break; break;
} }
default: default:
break; break;
} }
return { ...query, expr: expression }; return { ...query, expr: expression };
} }
......
...@@ -63,6 +63,15 @@ describe('addLabelToQuery()', () => { ...@@ -63,6 +63,15 @@ describe('addLabelToQuery()', () => {
expect(addLabelToQuery('{x="y"} |="yy"', 'bar', 'baz')).toBe('{bar="baz",x="y"} |="yy"'); expect(addLabelToQuery('{x="y"} |="yy"', 'bar', 'baz')).toBe('{bar="baz",x="y"} |="yy"');
expect(addLabelToQuery('{x="y"} |="yy" !~"xx"', 'bar', 'baz')).toBe('{bar="baz",x="y"} |="yy" !~"xx"'); expect(addLabelToQuery('{x="y"} |="yy" !~"xx"', 'bar', 'baz')).toBe('{bar="baz",x="y"} |="yy" !~"xx"');
}); });
it('should add label to query properly with Loki datasource', () => {
expect(addLabelToQuery('{job="grafana"} |= "foo-bar"', 'filename', 'test.txt', undefined, true)).toBe(
'{filename="test.txt",job="grafana"} |= "foo-bar"'
);
expect(addLabelToQuery('{job="grafana"} |= "foo-bar"', 'filename', 'test.txt')).toBe(
'{filename="test.txt",job="grafana"} |= "foo{filename="test.txt"}-bar"'
);
});
}); });
describe('addLabelToSelector()', () => { describe('addLabelToSelector()', () => {
......
...@@ -18,8 +18,13 @@ const builtInWords = [ ...@@ -18,8 +18,13 @@ const builtInWords = [
const metricNameRegexp = /([A-Za-z:][\w:]*)\b(?![\(\]{=!",])/g; const metricNameRegexp = /([A-Za-z:][\w:]*)\b(?![\(\]{=!",])/g;
const selectorRegexp = /{([^{]*)}/g; const selectorRegexp = /{([^{]*)}/g;
// addLabelToQuery('foo', 'bar', 'baz') => 'foo{bar="baz"}' export function addLabelToQuery(
export function addLabelToQuery(query: string, key: string, value: string, operator?: string): string { query: string,
key: string,
value: string,
operator?: string,
hasNoMetrics?: boolean
): string {
if (!key || !value) { if (!key || !value) {
throw new Error('Need label to add to query.'); throw new Error('Need label to add to query.');
} }
...@@ -35,7 +40,17 @@ export function addLabelToQuery(query: string, key: string, value: string, opera ...@@ -35,7 +40,17 @@ export function addLabelToQuery(query: string, key: string, value: string, opera
const isColonBounded = word.endsWith(':'); const isColonBounded = word.endsWith(':');
previousWord = word; previousWord = word;
if (!insideSelector && !isColonBounded && !previousWordIsKeyWord && builtInWords.indexOf(word) === -1) {
// with Prometheus datasource, adds an empty selector to a bare metric name
// but doesn't add it with Loki datasource so there are no unnecessary labels
// e.g. when the filter contains a dash (-) character inside
if (
!hasNoMetrics &&
!insideSelector &&
!isColonBounded &&
!previousWordIsKeyWord &&
builtInWords.indexOf(word) === -1
) {
return `${word}{}`; return `${word}{}`;
} }
return word; return word;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment