Commit bfd05ba6 by David Committed by GitHub

Merge pull request #12617 from grafana/davkal/prometheus-value-formatting

Make prometheus value formatting more robust
parents e413f026 1f74b298
...@@ -222,7 +222,7 @@ class MetricsPanelCtrl extends PanelCtrl { ...@@ -222,7 +222,7 @@ class MetricsPanelCtrl extends PanelCtrl {
// and add built in variables interval and interval_ms // and add built in variables interval and interval_ms
var scopedVars = Object.assign({}, this.panel.scopedVars, { var scopedVars = Object.assign({}, this.panel.scopedVars, {
__interval: { text: this.interval, value: this.interval }, __interval: { text: this.interval, value: this.interval },
__interval_ms: { text: String(this.intervalMs), value: String(this.intervalMs) }, __interval_ms: { text: this.intervalMs, value: this.intervalMs },
}); });
var metricsQuery = { var metricsQuery = {
......
...@@ -17,11 +17,17 @@ export function alignRange(start, end, step) { ...@@ -17,11 +17,17 @@ export function alignRange(start, end, step) {
} }
export function prometheusRegularEscape(value) { export function prometheusRegularEscape(value) {
return value.replace(/'/g, "\\\\'"); if (typeof value === 'string') {
return value.replace(/'/g, "\\\\'");
}
return value;
} }
export function prometheusSpecialRegexEscape(value) { export function prometheusSpecialRegexEscape(value) {
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&')); if (typeof value === 'string') {
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
}
return value;
} }
export class PrometheusDatasource { export class PrometheusDatasource {
...@@ -196,7 +202,7 @@ export class PrometheusDatasource { ...@@ -196,7 +202,7 @@ export class PrometheusDatasource {
interval = adjustedInterval; interval = adjustedInterval;
scopedVars = Object.assign({}, options.scopedVars, { scopedVars = Object.assign({}, options.scopedVars, {
__interval: { text: interval + 's', value: interval + 's' }, __interval: { text: interval + 's', value: interval + 's' },
__interval_ms: { text: String(interval * 1000), value: String(interval * 1000) }, __interval_ms: { text: interval * 1000, value: interval * 1000 },
}); });
} }
query.step = interval; query.step = interval;
......
...@@ -166,6 +166,9 @@ describe('PrometheusDatasource', () => { ...@@ -166,6 +166,9 @@ describe('PrometheusDatasource', () => {
}); });
describe('Prometheus regular escaping', function() { describe('Prometheus regular escaping', function() {
it('should not escape non-string', function() {
expect(prometheusRegularEscape(12)).toEqual(12);
});
it('should not escape simple string', function() { it('should not escape simple string', function() {
expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression'); expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression');
}); });
......
...@@ -452,7 +452,7 @@ describe('PrometheusDatasource', function() { ...@@ -452,7 +452,7 @@ describe('PrometheusDatasource', function() {
interval: '10s', interval: '10s',
scopedVars: { scopedVars: {
__interval: { text: '10s', value: '10s' }, __interval: { text: '10s', value: '10s' },
__interval_ms: { text: String(10 * 1000), value: String(10 * 1000) }, __interval_ms: { text: 10 * 1000, value: 10 * 1000 },
}, },
}; };
var urlExpected = var urlExpected =
...@@ -463,8 +463,8 @@ describe('PrometheusDatasource', function() { ...@@ -463,8 +463,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('10s'); expect(query.scopedVars.__interval.text).to.be('10s');
expect(query.scopedVars.__interval.value).to.be('10s'); expect(query.scopedVars.__interval.value).to.be('10s');
expect(query.scopedVars.__interval_ms.text).to.be(String(10 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(10 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(10 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(10 * 1000);
}); });
it('should be min interval when it is greater than auto interval', function() { it('should be min interval when it is greater than auto interval', function() {
var query = { var query = {
...@@ -479,7 +479,7 @@ describe('PrometheusDatasource', function() { ...@@ -479,7 +479,7 @@ describe('PrometheusDatasource', function() {
interval: '5s', interval: '5s',
scopedVars: { scopedVars: {
__interval: { text: '5s', value: '5s' }, __interval: { text: '5s', value: '5s' },
__interval_ms: { text: String(5 * 1000), value: String(5 * 1000) }, __interval_ms: { text: 5 * 1000, value: 5 * 1000 },
}, },
}; };
var urlExpected = var urlExpected =
...@@ -490,8 +490,8 @@ describe('PrometheusDatasource', function() { ...@@ -490,8 +490,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('5s'); expect(query.scopedVars.__interval.text).to.be('5s');
expect(query.scopedVars.__interval.value).to.be('5s'); expect(query.scopedVars.__interval.value).to.be('5s');
expect(query.scopedVars.__interval_ms.text).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
}); });
it('should account for intervalFactor', function() { it('should account for intervalFactor', function() {
var query = { var query = {
...@@ -507,7 +507,7 @@ describe('PrometheusDatasource', function() { ...@@ -507,7 +507,7 @@ describe('PrometheusDatasource', function() {
interval: '10s', interval: '10s',
scopedVars: { scopedVars: {
__interval: { text: '10s', value: '10s' }, __interval: { text: '10s', value: '10s' },
__interval_ms: { text: String(10 * 1000), value: String(10 * 1000) }, __interval_ms: { text: 10 * 1000, value: 10 * 1000 },
}, },
}; };
var urlExpected = var urlExpected =
...@@ -518,8 +518,8 @@ describe('PrometheusDatasource', function() { ...@@ -518,8 +518,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('10s'); expect(query.scopedVars.__interval.text).to.be('10s');
expect(query.scopedVars.__interval.value).to.be('10s'); expect(query.scopedVars.__interval.value).to.be('10s');
expect(query.scopedVars.__interval_ms.text).to.be(String(10 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(10 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(10 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(10 * 1000);
}); });
it('should be interval * intervalFactor when greater than min interval', function() { it('should be interval * intervalFactor when greater than min interval', function() {
var query = { var query = {
...@@ -535,7 +535,7 @@ describe('PrometheusDatasource', function() { ...@@ -535,7 +535,7 @@ describe('PrometheusDatasource', function() {
interval: '5s', interval: '5s',
scopedVars: { scopedVars: {
__interval: { text: '5s', value: '5s' }, __interval: { text: '5s', value: '5s' },
__interval_ms: { text: String(5 * 1000), value: String(5 * 1000) }, __interval_ms: { text: 5 * 1000, value: 5 * 1000 },
}, },
}; };
var urlExpected = var urlExpected =
...@@ -546,8 +546,8 @@ describe('PrometheusDatasource', function() { ...@@ -546,8 +546,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('5s'); expect(query.scopedVars.__interval.text).to.be('5s');
expect(query.scopedVars.__interval.value).to.be('5s'); expect(query.scopedVars.__interval.value).to.be('5s');
expect(query.scopedVars.__interval_ms.text).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
}); });
it('should be min interval when greater than interval * intervalFactor', function() { it('should be min interval when greater than interval * intervalFactor', function() {
var query = { var query = {
...@@ -563,7 +563,7 @@ describe('PrometheusDatasource', function() { ...@@ -563,7 +563,7 @@ describe('PrometheusDatasource', function() {
interval: '5s', interval: '5s',
scopedVars: { scopedVars: {
__interval: { text: '5s', value: '5s' }, __interval: { text: '5s', value: '5s' },
__interval_ms: { text: String(5 * 1000), value: String(5 * 1000) }, __interval_ms: { text: 5 * 1000, value: 5 * 1000 },
}, },
}; };
var urlExpected = var urlExpected =
...@@ -574,8 +574,8 @@ describe('PrometheusDatasource', function() { ...@@ -574,8 +574,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('5s'); expect(query.scopedVars.__interval.text).to.be('5s');
expect(query.scopedVars.__interval.value).to.be('5s'); expect(query.scopedVars.__interval.value).to.be('5s');
expect(query.scopedVars.__interval_ms.text).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
}); });
it('should be determined by the 11000 data points limit, accounting for intervalFactor', function() { it('should be determined by the 11000 data points limit, accounting for intervalFactor', function() {
var query = { var query = {
...@@ -590,7 +590,7 @@ describe('PrometheusDatasource', function() { ...@@ -590,7 +590,7 @@ describe('PrometheusDatasource', function() {
interval: '5s', interval: '5s',
scopedVars: { scopedVars: {
__interval: { text: '5s', value: '5s' }, __interval: { text: '5s', value: '5s' },
__interval_ms: { text: String(5 * 1000), value: String(5 * 1000) }, __interval_ms: { text: 5 * 1000, value: 5 * 1000 },
}, },
}; };
var end = 7 * 24 * 60 * 60; var end = 7 * 24 * 60 * 60;
...@@ -609,8 +609,8 @@ describe('PrometheusDatasource', function() { ...@@ -609,8 +609,8 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval.text).to.be('5s'); expect(query.scopedVars.__interval.text).to.be('5s');
expect(query.scopedVars.__interval.value).to.be('5s'); expect(query.scopedVars.__interval.value).to.be('5s');
expect(query.scopedVars.__interval_ms.text).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000);
expect(query.scopedVars.__interval_ms.value).to.be(String(5 * 1000)); expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
}); });
}); });
}); });
......
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