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.
pull/12617/head
David Kaltschmidt 8 years ago
parent 09c3569caa
commit 1efe34e6cf
  1. 10
      public/app/plugins/datasource/prometheus/datasource.ts
  2. 3
      public/app/plugins/datasource/prometheus/specs/datasource.jest.ts

@ -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');
});

Loading…
Cancel
Save