From 435a092aa047ec349601f7d53b7a3b31a9ab40ac Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Fri, 28 Mar 2025 15:26:52 +0100 Subject: [PATCH] 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 }