Commit 8a8a1b8e by Daniel Lee Committed by GitHub

Fix: Prometheus regex ad-hoc filters w/ wildcards (#16234)

Fixes #14615.

Removes extra escaping for regex filter values when using ad hoc
filters for Prometheus. The extra escaping causes queries with
regex characters to be invalid.
parent bbdc1e6b
......@@ -215,7 +215,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
const { key, operator } = filter;
let { value } = filter;
if (operator === '=~' || operator === '!~') {
value = prometheusSpecialRegexEscape(value);
value = prometheusRegularEscape(value);
}
return addLabelToQuery(acc, key, value, operator);
}, expr);
......
......@@ -5,8 +5,8 @@ import {
alignRange,
extractRuleMappingFromGroups,
PrometheusDatasource,
prometheusSpecialRegexEscape,
prometheusRegularEscape,
prometheusSpecialRegexEscape,
} from '../datasource';
jest.mock('../metric_find_query');
......@@ -91,6 +91,25 @@ describe('PrometheusDatasource', () => {
const result = ctx.ds.createQuery(target, { interval: '15s' });
expect(result).toMatchObject({ expr: 'metric{job="foo",k1="v1",k2!="v2"} - metric{k1="v1",k2!="v2"}' });
});
it('should add escaping if needed to regex filter expressions', () => {
ctx.templateSrvMock.getAdhocFilters = () => [
{
key: 'k1',
operator: '=~',
value: 'v.*',
},
{
key: 'k2',
operator: '=~',
value: `v'.*`,
},
];
const result = ctx.ds.createQuery(target, { interval: '15s' });
expect(result).toMatchObject({
expr: `metric{job="foo",k1=~"v.*",k2=~"v\\\\'.*"} - metric{k1=~"v.*",k2=~"v\\\\'.*"}`,
});
});
});
describe('When performing performSuggestQuery', () => {
......
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