Commit 1efe34e6 by David Kaltschmidt

Make prometheus value formatting more robust

- prometheus datasources passes its own interpolator function to the
  template server
- that function relies on incoming values being strings
- some template variables may be non-strings, e.g., `__interval_ms`,
  which throws an error

This PR makes this more robust.
parent 09c3569c
......@@ -17,11 +17,17 @@ export function alignRange(start, end, step) {
}
export function prometheusRegularEscape(value) {
return value.replace(/'/g, "\\\\'");
if (typeof value === 'string') {
return value.replace(/'/g, "\\\\'");
}
return 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 {
......
......@@ -166,6 +166,9 @@ describe('PrometheusDatasource', () => {
});
describe('Prometheus regular escaping', function() {
it('should not escape non-string', function() {
expect(prometheusRegularEscape(12)).toEqual(12);
});
it('should not escape simple string', function() {
expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression');
});
......
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