Skip to content

Review regex declarations in innodb and global variables collectors #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions collector/engine_innodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions collector/global_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": "",
Expand Down Expand Up @@ -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":
Expand All @@ -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
}
Expand Down