From 1efe34e6cf5ffa0e604be526169ff10c3b5dadba Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Mon, 16 Jul 2018 12:40:55 +0200 Subject: [PATCH] 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. --- public/app/plugins/datasource/prometheus/datasource.ts | 10 ++++++++-- .../datasource/prometheus/specs/datasource.jest.ts | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 9ccda65a145..35b04066552 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/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 { diff --git a/public/app/plugins/datasource/prometheus/specs/datasource.jest.ts b/public/app/plugins/datasource/prometheus/specs/datasource.jest.ts index 219b990e5dd..15798a33cd2 100644 --- a/public/app/plugins/datasource/prometheus/specs/datasource.jest.ts +++ b/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'); });