From 0da4168836a73a8a10bd8cf8b2e40d61b6179498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 1 Mar 2016 21:35:55 +0100 Subject: [PATCH] fix(prometheus): fixed templating issue with prometheus for when using variable with non regex operator, #2918 --- public/app/features/templating/templateSrv.js | 24 +++++++++++-------- .../datasource/prometheus/datasource.ts | 11 ++++++++- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index 0df887ca483..534fd927140 100644 --- a/public/app/features/templating/templateSrv.js +++ b/public/app/features/templating/templateSrv.js @@ -42,7 +42,11 @@ function (angular, _) { return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, "\\$1"); } - this.formatValue = function(value, format) { + this.formatValue = function(value, format, variable) { + if (typeof format === 'function') { + return format(value, variable, this.formatValue); + } + switch(format) { case "regex": { if (typeof value === 'string') { @@ -121,21 +125,21 @@ function (angular, _) { this._regex.lastIndex = 0; return target.replace(this._regex, function(match, g1, g2) { - if (scopedVars) { - value = scopedVars[g1 || g2]; - if (value) { - return self.formatValue(value.value); - } - } - variable = self._index[g1 || g2]; if (!variable) { return match; } + if (scopedVars) { + value = scopedVars[g1 || g2]; + if (value) { + return self.formatValue(value.value, format, variable); + } + } + systemValue = self._grafanaVariables[variable.current.value]; if (systemValue) { - return self.formatValue(systemValue); + return self.formatValue(systemValue, format, variable); } value = variable.current.value; @@ -143,7 +147,7 @@ function (angular, _) { value = self.getAllValue(variable); } - var res = self.formatValue(value, format); + var res = self.formatValue(value, format, variable); return res; }); }; diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 55ce628d53d..69dae38e358 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -39,6 +39,15 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS return backendSrv.datasourceRequest(options); }; + function interpolateQueryExpr(value, variable, defaultFormatFn) { + // if no multi or include all do not regexEscape + if (!variable.multi && !variable.includeAll) { + return value; + } + + return defaultFormatFn(value, 'regex', variable); + }; + // Called once per panel (graph) this.query = function(options) { var start = getPrometheusTime(options.range.from, false); @@ -52,7 +61,7 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS } var query: any = {}; - query.expr = templateSrv.replace(target.expr, options.scopedVars, 'regex'); + query.expr = templateSrv.replace(target.expr, options.scopedVars, interpolateQueryExpr); var interval = target.interval || options.interval; var intervalFactor = target.intervalFactor || 1;