Commit 98f0305e by Carl Bergquist Committed by GitHub

Merge pull request #9983 from mtanda/prometheus_nested

prometheus nested query support
parents 1e10fcad 98bb8bf7
...@@ -157,6 +157,7 @@ export class PrometheusDatasource { ...@@ -157,6 +157,7 @@ export class PrometheusDatasource {
// Only replace vars in expression after having (possibly) updated interval vars // Only replace vars in expression after having (possibly) updated interval vars
query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr); query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr);
query.expr = this.replaceNestedQuery(query.expr, options);
query.requestId = options.panelId + target.refId; query.requestId = options.panelId + target.refId;
return query; return query;
} }
...@@ -271,6 +272,13 @@ export class PrometheusDatasource { ...@@ -271,6 +272,13 @@ export class PrometheusDatasource {
}); });
} }
replaceNestedQuery(query, options) {
return query.replace(/\#([A-Z])/g, (match, g1) => {
let replaceTarget = options.targets.find((t) => { return t.refId === g1; });
return replaceTarget ? replaceTarget.expr : match;
});
}
transformMetricData(md, options, start, end, step) { transformMetricData(md, options, start, end, step) {
var dps = [], var dps = [],
metricLabel = null; metricLabel = null;
......
...@@ -590,4 +590,19 @@ describe('PrometheusDatasource', function() { ...@@ -590,4 +590,19 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000); expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
}); });
}); });
describe('The nested query', function() {
it('should generate correct query', function() {
let query = 'sum(rate(#A[1m]))';
let options = {
targets: [
{
refId: 'A',
expr: 'http_requests_total'
}
]
};
let result = ctx.ds.replaceNestedQuery(query, options);
expect(result).to.be('sum(rate(http_requests_total[1m]))');
});
});
}); });
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