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