mirror of
https://github.com/grafana/grafana.git
synced 2025-01-02 04:07:15 -06:00
Prometheus: Fix applying ad-hoc filters to the expression that has a template variable (#75250)
* Interpolate first and then apply ad-hoc filters * More tests
This commit is contained in:
parent
13ea22ac1e
commit
d076f733e9
@ -835,6 +835,55 @@ describe('PrometheusDatasource', () => {
|
||||
const result = ds.applyTemplateVariables(query, {});
|
||||
expect(result).toMatchObject({ expr: 'test{job="bar", k1="v1", k2!="v2"}' });
|
||||
});
|
||||
|
||||
it('should add ad-hoc filters only to expr', () => {
|
||||
replaceMock.mockImplementation((a: string) => a?.replace('$A', '99') ?? a);
|
||||
getAdhocFiltersMock.mockReturnValue([
|
||||
{
|
||||
key: 'k1',
|
||||
operator: '=',
|
||||
value: 'v1',
|
||||
},
|
||||
{
|
||||
key: 'k2',
|
||||
operator: '!=',
|
||||
value: 'v2',
|
||||
},
|
||||
]);
|
||||
|
||||
const query = {
|
||||
expr: 'test{job="bar"} > $A',
|
||||
refId: 'A',
|
||||
};
|
||||
|
||||
const result = ds.applyTemplateVariables(query, {});
|
||||
expect(result).toMatchObject({ expr: 'test{job="bar", k1="v1", k2!="v2"} > 99' });
|
||||
});
|
||||
|
||||
it('should add ad-hoc filters only to expr and expression has template variable as label value??', () => {
|
||||
const searchPattern = /\$A/g;
|
||||
replaceMock.mockImplementation((a: string) => a?.replace(searchPattern, '99') ?? a);
|
||||
getAdhocFiltersMock.mockReturnValue([
|
||||
{
|
||||
key: 'k1',
|
||||
operator: '=',
|
||||
value: 'v1',
|
||||
},
|
||||
{
|
||||
key: 'k2',
|
||||
operator: '!=',
|
||||
value: 'v2',
|
||||
},
|
||||
]);
|
||||
|
||||
const query = {
|
||||
expr: 'test{job="$A"} > $A',
|
||||
refId: 'A',
|
||||
};
|
||||
|
||||
const result = ds.applyTemplateVariables(query, {});
|
||||
expect(result).toMatchObject({ expr: 'test{job="99", k1="v1", k2!="v2"} > 99' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('metricFindQuery', () => {
|
||||
|
@ -1280,14 +1280,17 @@ export class PrometheusDatasource
|
||||
delete variables.__interval;
|
||||
delete variables.__interval_ms;
|
||||
|
||||
//Add ad hoc filters
|
||||
const expr = this.enhanceExprWithAdHocFilters(target.expr);
|
||||
// interpolate expression
|
||||
const expr = this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr);
|
||||
|
||||
// Add ad hoc filters
|
||||
const exprWithAdHocFilters = this.enhanceExprWithAdHocFilters(expr);
|
||||
|
||||
return {
|
||||
...target,
|
||||
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
|
||||
expr: this.templateSrv.replace(expr, variables, this.interpolateQueryExpr),
|
||||
expr: exprWithAdHocFilters,
|
||||
interval: this.templateSrv.replace(target.interval, variables),
|
||||
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user