Skip to content

Commit 435a092

Browse files
committed
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 <[email protected]>
1 parent 46acc01 commit 435a092

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Diff for: collector/engine_innodb.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ const (
3232
engineInnodbStatusQuery = `SHOW ENGINE INNODB STATUS`
3333
)
3434

35+
var (
36+
// 0 queries inside InnoDB, 0 queries in queue
37+
// 0 read views open inside InnoDB
38+
rQueries = regexp.MustCompile(`(\d+) queries inside InnoDB, (\d+) queries in queue`)
39+
rViews = regexp.MustCompile(`(\d+) read views open inside InnoDB`)
40+
)
41+
3542
// ScrapeEngineInnodbStatus scrapes from `SHOW ENGINE INNODB STATUS`.
3643
type ScrapeEngineInnodbStatus struct{}
3744

@@ -67,11 +74,6 @@ func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance,
6774
}
6875
}
6976

70-
// 0 queries inside InnoDB, 0 queries in queue
71-
// 0 read views open inside InnoDB
72-
rQueries, _ := regexp.Compile(`(\d+) queries inside InnoDB, (\d+) queries in queue`)
73-
rViews, _ := regexp.Compile(`(\d+) read views open inside InnoDB`)
74-
7577
for _, line := range strings.Split(statusCol, "\n") {
7678
if data := rQueries.FindStringSubmatch(line); data != nil {
7779
value, _ := strconv.ParseFloat(data[1], 64)

Diff for: collector/global_variables.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const (
3434
)
3535

3636
var (
37+
promNameRe = regexp.MustCompile("([^a-zA-Z0-9_])")
38+
wsrespGcacheSizeRe = regexp.MustCompile(`gcache.size = (\d+)([MG]?);`)
39+
3740
// Map known global variables to help strings. Unknown will be mapped to generic gauges.
3841
globalVariablesHelp = map[string]string{
3942
// 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
148151

149152
var key string
150153
var val sql.RawBytes
151-
var textItems = map[string]string{
154+
textItems := map[string]string{
152155
"innodb_version": "",
153156
"version": "",
154157
"version_comment": "",
@@ -226,13 +229,12 @@ func (ScrapeGlobalVariables) Scrape(ctx context.Context, instance *instance, ch
226229

227230
// parseWsrepProviderOptions parse wsrep_provider_options to get gcache.size in bytes.
228231
func parseWsrepProviderOptions(opts string) float64 {
229-
var val float64
230-
r, _ := regexp.Compile(`gcache.size = (\d+)([MG]?);`)
231-
data := r.FindStringSubmatch(opts)
232+
data := wsrespGcacheSizeRe.FindStringSubmatch(opts)
232233
if data == nil {
233234
return 0
234235
}
235236

237+
var val float64
236238
val, _ = strconv.ParseFloat(data[1], 64)
237239
switch data[2] {
238240
case "M":
@@ -245,8 +247,7 @@ func parseWsrepProviderOptions(opts string) float64 {
245247
}
246248

247249
func validPrometheusName(s string) string {
248-
nameRe := regexp.MustCompile("([^a-zA-Z0-9_])")
249-
s = nameRe.ReplaceAllString(s, "_")
250+
s = promNameRe.ReplaceAllString(s, "_")
250251
s = strings.ToLower(s)
251252
return s
252253
}

0 commit comments

Comments
 (0)