From c8e3120d1fd0296d0fc6c9bb0997a53446fbdc8c Mon Sep 17 00:00:00 2001 From: Kamil Dziedzic Date: Thu, 30 Aug 2018 16:49:24 +0200 Subject: [PATCH] PMM-2576: Fix prometheus metric names for SHOW GLOBAL VARIABLES. (#307) * Use regexp to replace invalid characters. Signed-off-by: Kamil Dziedzic --- collector/global_variables.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/collector/global_variables.go b/collector/global_variables.go index e1d52b9..2a9fa47 100644 --- a/collector/global_variables.go +++ b/collector/global_variables.go @@ -53,7 +53,7 @@ func (ScrapeGlobalVariables) Scrape(db *sql.DB, ch chan<- prometheus.Metric) err if err := globalVariablesRows.Scan(&key, &val); err != nil { return err } - key = strings.ToLower(key) + key = validPrometheusName(key) if floatVal, ok := parseStatus(val); ok { ch <- prometheus.MustNewConstMetric( newDesc(globalVariables, key, "Generic gauge metric from SHOW GLOBAL VARIABLES."), @@ -113,3 +113,10 @@ func parseWsrepProviderOptions(opts string) float64 { return val } + +func validPrometheusName(s string) string { + nameRe := regexp.MustCompile("([^a-zA-Z0-9_])") + s = nameRe.ReplaceAllString(s, "_") + s = strings.ToLower(s) + return s +}