From fdeeeec99a4c46b8ff449d9e05234f7641821c2b Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Fri, 28 Mar 2025 15:26:52 +0100 Subject: [PATCH 1/3] Review regex declarations in innodb and global variables collectors Move regex vars to package level to avoid re-compilation on each scrape. Signed-off-by: Cristian Greco --- collector/engine_innodb.go | 12 +++++++----- collector/global_variables.go | 13 +++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/collector/engine_innodb.go b/collector/engine_innodb.go index 55f06438d..911d37ebf 100644 --- a/collector/engine_innodb.go +++ b/collector/engine_innodb.go @@ -32,6 +32,13 @@ const ( engineInnodbStatusQuery = `SHOW ENGINE INNODB STATUS` ) +var ( + // 0 queries inside InnoDB, 0 queries in queue + // 0 read views open inside InnoDB + rQueries = regexp.MustCompile(`(\d+) queries inside InnoDB, (\d+) queries in queue`) + rViews = regexp.MustCompile(`(\d+) read views open inside InnoDB`) +) + // ScrapeEngineInnodbStatus scrapes from `SHOW ENGINE INNODB STATUS`. type ScrapeEngineInnodbStatus struct{} @@ -67,11 +74,6 @@ func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance, } } - // 0 queries inside InnoDB, 0 queries in queue - // 0 read views open inside InnoDB - rQueries, _ := regexp.Compile(`(\d+) queries inside InnoDB, (\d+) queries in queue`) - rViews, _ := regexp.Compile(`(\d+) read views open inside InnoDB`) - for _, line := range strings.Split(statusCol, "\n") { if data := rQueries.FindStringSubmatch(line); data != nil { value, _ := strconv.ParseFloat(data[1], 64) diff --git a/collector/global_variables.go b/collector/global_variables.go index 926ffbb31..4a5b5d5c4 100644 --- a/collector/global_variables.go +++ b/collector/global_variables.go @@ -34,6 +34,9 @@ const ( ) var ( + promNameRe = regexp.MustCompile("([^a-zA-Z0-9_])") + wsrespGcacheSizeRe = regexp.MustCompile(`gcache.size = (\d+)([MG]?);`) + // Map known global variables to help strings. Unknown will be mapped to generic gauges. globalVariablesHelp = map[string]string{ // https://github.com/facebook/mysql-5.6/wiki/New-MySQL-RocksDB-Server-Variables @@ -148,7 +151,7 @@ func (ScrapeGlobalVariables) Scrape(ctx context.Context, instance *instance, ch var key string var val sql.RawBytes - var textItems = map[string]string{ + textItems := map[string]string{ "innodb_version": "", "version": "", "version_comment": "", @@ -226,13 +229,12 @@ func (ScrapeGlobalVariables) Scrape(ctx context.Context, instance *instance, ch // parseWsrepProviderOptions parse wsrep_provider_options to get gcache.size in bytes. func parseWsrepProviderOptions(opts string) float64 { - var val float64 - r, _ := regexp.Compile(`gcache.size = (\d+)([MG]?);`) - data := r.FindStringSubmatch(opts) + data := wsrespGcacheSizeRe.FindStringSubmatch(opts) if data == nil { return 0 } + var val float64 val, _ = strconv.ParseFloat(data[1], 64) switch data[2] { case "M": @@ -245,8 +247,7 @@ func parseWsrepProviderOptions(opts string) float64 { } func validPrometheusName(s string) string { - nameRe := regexp.MustCompile("([^a-zA-Z0-9_])") - s = nameRe.ReplaceAllString(s, "_") + s = promNameRe.ReplaceAllString(s, "_") s = strings.ToLower(s) return s } From b5bf68d586f3047d250721b5e2b4c0c1527cb118 Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Thu, 26 Jun 2025 18:31:49 +0200 Subject: [PATCH 2/3] Update collector/engine_innodb.go Co-authored-by: Ben Kochie Signed-off-by: Cristian Greco --- collector/engine_innodb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector/engine_innodb.go b/collector/engine_innodb.go index 911d37ebf..6729df43d 100644 --- a/collector/engine_innodb.go +++ b/collector/engine_innodb.go @@ -35,8 +35,8 @@ const ( var ( // 0 queries inside InnoDB, 0 queries in queue // 0 read views open inside InnoDB - rQueries = regexp.MustCompile(`(\d+) queries inside InnoDB, (\d+) queries in queue`) - rViews = regexp.MustCompile(`(\d+) read views open inside InnoDB`) + queriesRe = regexp.MustCompile(`(\d+) queries inside InnoDB, (\d+) queries in queue`) + viewsRe = regexp.MustCompile(`(\d+) read views open inside InnoDB`) ) // ScrapeEngineInnodbStatus scrapes from `SHOW ENGINE INNODB STATUS`. From 3a7fcf4a9cb57bb0399abee6002d7673983f875a Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Thu, 26 Jun 2025 18:33:10 +0200 Subject: [PATCH 3/3] fix after rename Signed-off-by: Cristian Greco --- collector/engine_innodb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector/engine_innodb.go b/collector/engine_innodb.go index 6729df43d..a18b8e6f9 100644 --- a/collector/engine_innodb.go +++ b/collector/engine_innodb.go @@ -75,7 +75,7 @@ func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance, } for _, line := range strings.Split(statusCol, "\n") { - if data := rQueries.FindStringSubmatch(line); data != nil { + if data := queriesRe.FindStringSubmatch(line); data != nil { value, _ := strconv.ParseFloat(data[1], 64) ch <- prometheus.MustNewConstMetric( newDesc(innodb, "queries_inside_innodb", "Queries inside InnoDB."), @@ -88,7 +88,7 @@ func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance, prometheus.GaugeValue, value, ) - } else if data := rViews.FindStringSubmatch(line); data != nil { + } else if data := viewsRe.FindStringSubmatch(line); data != nil { value, _ := strconv.ParseFloat(data[1], 64) ch <- prometheus.MustNewConstMetric( newDesc(innodb, "read_views_open_inside_innodb", "Read views open inside InnoDB."),