Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ad hoc filter replace bug #170

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ describe('PrometheusDatasource', () => {
expr: `metric{job="foo", k1=~"v.*", k2=~"v\\\\'.*"} - metric{k1=~"v.*", k2=~"v\\\\'.*"}`,
});
});

it('AdhocFilters should not add filters', () => {
getAdhocFiltersMock.mockReturnValue([
{
key: 'k1',
operator: '=',
value: 'v1',
}
]);
replaceMock.mockImplementation((str, params) => {
const topk = params['topk'] as ScopedVar;
return str.replace('$topk', topk.value);
});
const caseTarget: PromQuery = { expr: 'topk_max($topk, vmalert_iteration_duration_seconds_sum)', refId: 'A' };
const result = ds.createQuery(caseTarget, { interval: '15s', scopedVars: { 'topk': { text: 'topk', value: '5' } } as ScopedVars} as DataQueryRequest<PromQuery>, 0, 0);
expect(result).toMatchObject({ expr: 'topk_max(5, vmalert_iteration_duration_seconds_sum{k1="v1"})'});
});
});

describe('alignRange', () => {
Expand Down Expand Up @@ -489,6 +506,26 @@ describe('PrometheusDatasource', () => {
const result = ds.applyTemplateVariables(query, {});
expect(result).toMatchObject({ expr: 'test{job="bar", k1="v1", k2!="v2"}' });
});

it('AdhocFilters should not add filters', () => {
getAdhocFiltersMock.mockReturnValue([
{
key: 'k1',
operator: '=',
value: 'v1',
}
]);
replaceMock.mockImplementation((str, params) => {
const topk = params['topk'] as ScopedVar;
dmitryk-dk marked this conversation as resolved.
Show resolved Hide resolved
return str?.replace('$topk', topk.value);
});
const query: PromQuery = {
expr: 'topk_max($topk, vmalert_iteration_duration_seconds_sum)',
refId: 'A'
};
const result = ds.applyTemplateVariables(query, { 'topk': { text: 'topk', value: '5' } } as ScopedVars);
expect(result).toMatchObject({ expr: 'topk_max(5, vmalert_iteration_duration_seconds_sum{k1="v1"})'});
});
});

describe('metricFindQuery', () => {
Expand Down
11 changes: 5 additions & 6 deletions src/datasource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,15 @@ export class PrometheusDatasource

let expr = target.expr;

// Apply adhoc filters
expr = this.enhanceExprWithAdHocFilters(expr);

// Apply WITH templates
const dashboardUID = options.dashboardUID || options.app || ""
expr = mergeTemplateWithQuery(expr, this.withTemplates.find(t => t.uid === dashboardUID))

// Only replace vars in expression after having (possibly) updated interval vars
query.expr = this.templateSrv.replace(expr, scopedVars, this.interpolateQueryExpr);
expr = this.templateSrv.replace(expr, scopedVars, this.interpolateQueryExpr);

// Apply adhoc filters
query.expr = this.enhanceExprWithAdHocFilters(expr);
// Align query interval with step to allow query caching and to ensure
// that about-same-time query results look the same.
const adjusted = alignRange(start, end, query.step, this.timeSrv.timeRange().to.utcOffset() * 60);
Expand Down Expand Up @@ -1016,12 +1015,12 @@ export class PrometheusDatasource
delete variables.__interval_ms;

//Add ad hoc filters
const expr = this.enhanceExprWithAdHocFilters(target.expr);
const expr = this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr);

return {
...target,
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
expr: this.templateSrv.replace(expr, variables, this.interpolateQueryExpr),
dmitryk-dk marked this conversation as resolved.
Show resolved Hide resolved
expr: this.enhanceExprWithAdHocFilters(expr),
interval: this.templateSrv.replace(target.interval, variables),
};
}
Expand Down
Loading