Skip to content

Commit

Permalink
use util.SanitizeMetricKey
Browse files Browse the repository at this point in the history
  • Loading branch information
astj committed Oct 5, 2016
1 parent 4f2aed8 commit f48ed39
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion metrics/darwin/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (g *InterfaceGenerator) collectIntarfacesValues() (metrics.Values, error) {
for lineScanner.Scan() {
line := lineScanner.Text()
fields := strings.Fields(line)
name := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(regexp.MustCompile(`\*`).ReplaceAllString(fields[0], ""), "_")
name := util.SanitizeMetricKey(regexp.MustCompile(`\*`).ReplaceAllString(fields[0], ""))
if match, _ := regexp.MatchString(`^lo\d+$`, name); match {
continue
}
Expand Down
6 changes: 2 additions & 4 deletions metrics/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ type FilesystemGenerator struct {
UseMountPoint bool
}

var sanitizerReg = regexp.MustCompile(`[^A-Za-z0-9_-]`)

// Generate the metrics of filesystems
func (g *FilesystemGenerator) Generate() (Values, error) {
filesystems, err := util.CollectDfValues()
Expand All @@ -32,9 +30,9 @@ func (g *FilesystemGenerator) Generate() (Values, error) {
if device := strings.TrimPrefix(name, "/dev/"); name != device {
var metricName string
if g.UseMountPoint {
metricName = sanitizerReg.ReplaceAllString(dfs.Mounted, "_")
metricName = util.SanitizeMetricKey(dfs.Mounted)
} else {
metricName = sanitizerReg.ReplaceAllString(device, "_")
metricName = util.SanitizeMetricKey(device)
}
// kilo bytes -> bytes
ret["filesystem."+metricName+".size"] = float64(dfs.Used+dfs.Available) * 1024
Expand Down
8 changes: 3 additions & 5 deletions metrics/linux/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func parseDiskStats(out []byte, mapping map[string]string) (metrics.Values, erro
diskLogger.Warningf("Failed to parse disk metrics: %s", text)
continue
}
device := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(cols[2], "_")
device := util.SanitizeMetricKey(cols[2])
values := cols[3:]

if len(values) != len(diskMetricsNames) {
Expand Down Expand Up @@ -150,8 +150,6 @@ func parseDiskStats(out []byte, mapping map[string]string) (metrics.Values, erro
return results, nil
}

var mountpointSanitizerReg = regexp.MustCompile(`[^A-Za-z0-9_-]`)

// Generate the metrics of filesystems
func getDeviceNameMapping() (map[string]string, error) {
filesystems, err := util.CollectDfValues()
Expand All @@ -162,8 +160,8 @@ func getDeviceNameMapping() (map[string]string, error) {
for _, dfs := range filesystems {
name := dfs.Name
if device := strings.TrimPrefix(name, "/dev/"); name != device {
mountpointLabel := mountpointSanitizerReg.ReplaceAllString(dfs.Mounted, "_")
deviceName := mountpointSanitizerReg.ReplaceAllString(device, "_")
mountpointLabel := util.SanitizeMetricKey(dfs.Mounted)
deviceName := util.SanitizeMetricKey(device)
ret[deviceName] = mountpointLabel
}
}
Expand Down
5 changes: 2 additions & 3 deletions metrics/linux/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/metrics"
"github.com/mackerelio/mackerel-agent/util"
)

/*
Expand Down Expand Up @@ -80,15 +81,13 @@ func (g *InterfaceGenerator) collectInterfacesValues() (metrics.Values, error) {
return parseNetdev(out)
}

var sanitizerReg = regexp.MustCompile(`[^A-Za-z0-9_-]`)

func parseNetdev(out []byte) (metrics.Values, error) {
lineScanner := bufio.NewScanner(bytes.NewReader(out))
results := make(map[string]float64)
for lineScanner.Scan() {
line := lineScanner.Text()
if kv := strings.SplitN(line, ":", 2); len(kv) == 2 {
name := sanitizerReg.ReplaceAllString(strings.TrimSpace(kv[0]), "_")
name := util.SanitizeMetricKey(strings.TrimSpace(kv[0]))
if name == "lo" {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion metrics/windows/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (g *FilesystemGenerator) Generate() (metrics.Values, error) {
ret := make(map[string]float64)
for name, values := range filesystems {
if matches := regexp.MustCompile(`^(.*):`).FindStringSubmatch(name); matches != nil {
device := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(matches[1], "_")
device := util.SanitizeMetricKey(matches[1])

ret["filesystem."+device+".size"] = values.KbSize * 1024
ret["filesystem."+device+".used"] = values.KbUsed * 1024
Expand Down

0 comments on commit f48ed39

Please sign in to comment.