Prechádzať zdrojové kódy

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.
David Kaltschmidt 7 rokov pred
rodič
commit
1efe34e6cf

+ 8 - 2
public/app/plugins/datasource/prometheus/datasource.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 {

+ 3 - 0
public/app/plugins/datasource/prometheus/specs/datasource.jest.ts

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