Explore: Keep logQL filters when selecting labels in log row details (#20570)
(cherry picked from commit c6a9a83bf3)
This commit is contained in:
committed by
Marcus Efraimsson
parent
875431c7b0
commit
28399a1d85
@@ -13,7 +13,7 @@ import {
|
||||
FieldType,
|
||||
FieldConfig,
|
||||
} from '@grafana/data';
|
||||
import { addLabelToSelector } from 'app/plugins/datasource/prometheus/add_label_to_query';
|
||||
import { addLabelToSelector, keepSelectorFilters } from 'app/plugins/datasource/prometheus/add_label_to_query';
|
||||
import LanguageProvider from './language_provider';
|
||||
import { logStreamToDataFrame } from './result_transformer';
|
||||
import { formatQuery, parseQuery, getHighlighterExpressionsFromQuery } from './query_utils';
|
||||
@@ -276,13 +276,18 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
|
||||
modifyQuery(query: LokiQuery, action: any): LokiQuery {
|
||||
const parsed = parseQuery(query.expr || '');
|
||||
let { query: selector } = parsed;
|
||||
let selectorLabels, selectorFilters;
|
||||
switch (action.type) {
|
||||
case 'ADD_FILTER': {
|
||||
selector = addLabelToSelector(selector, action.key, action.value);
|
||||
selectorLabels = addLabelToSelector(selector, action.key, action.value);
|
||||
selectorFilters = keepSelectorFilters(selector);
|
||||
selector = `${selectorLabels} ${selectorFilters}`;
|
||||
break;
|
||||
}
|
||||
case 'ADD_FILTER_OUT': {
|
||||
selector = addLabelToSelector(selector, action.key, action.value, '!=');
|
||||
selectorLabels = addLabelToSelector(selector, action.key, action.value, '!=');
|
||||
selectorFilters = keepSelectorFilters(selector);
|
||||
selector = `${selectorLabels} ${selectorFilters}`;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -91,6 +91,12 @@ export function addLabelToSelector(selector: string, labelKey: string, labelValu
|
||||
return `{${formatted}}`;
|
||||
}
|
||||
|
||||
export function keepSelectorFilters(selector: string) {
|
||||
// Remove all label-key between {} and return filters. If first character is space, remove it.
|
||||
const filters = selector.replace(/\{(.*?)\}/g, '').replace(/^ /, '');
|
||||
return filters;
|
||||
}
|
||||
|
||||
function isPositionInsideChars(text: string, position: number, openChar: string, closeChar: string) {
|
||||
const nextSelectorStart = text.slice(position).indexOf(openChar);
|
||||
const nextSelectorEnd = text.slice(position).indexOf(closeChar);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { addLabelToQuery, addLabelToSelector } from '../add_label_to_query';
|
||||
import { addLabelToQuery, addLabelToSelector, keepSelectorFilters } from '../add_label_to_query';
|
||||
|
||||
describe('addLabelToQuery()', () => {
|
||||
it('should add label to simple query', () => {
|
||||
@@ -44,7 +44,7 @@ describe('addLabelToQuery()', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should not add duplicate labels to aquery', () => {
|
||||
it('should not add duplicate labels to a query', () => {
|
||||
expect(addLabelToQuery(addLabelToQuery('foo{x="yy"}', 'bar', 'baz', '!='), 'bar', 'baz', '!=')).toBe(
|
||||
'foo{bar!="baz",x="yy"}'
|
||||
);
|
||||
@@ -72,3 +72,15 @@ describe('addLabelToSelector()', () => {
|
||||
expect(addLabelToSelector('{}', 'baz', '42', '!=')).toBe('{baz!="42"}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('keepSelectorFilters()', () => {
|
||||
test('should return empty string if no filter is in selector', () => {
|
||||
expect(keepSelectorFilters('{foo="bar"}')).toBe('');
|
||||
});
|
||||
test('should return a filter if filter is in selector', () => {
|
||||
expect(keepSelectorFilters('{foo="bar"} |="baz"')).toBe('|="baz"');
|
||||
});
|
||||
test('should return multiple filters if multiple filters are in selector', () => {
|
||||
expect(keepSelectorFilters('{foo!="bar"} |="baz" |~"yy" !~"xx"')).toBe('|="baz" |~"yy" !~"xx"');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user