aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShapor Naghibzadeh <shapor@gmail.com>2020-04-26 01:04:25 -0700
committerShapor Naghibzadeh <shapor@google.com>2020-04-26 01:13:25 -0700
commita1a3633d89520c6e941908fb6e93a94dca6eeb2a (patch)
tree953c143de911d80f7db40a46eb2cb7c3efb6f16d
parent091bed01b084d8972a8d11a8a1325ce12646660a (diff)
downloadprometheus_node_collector-a1a3633d89520c6e941908fb6e93a94dca6eeb2a.tar.bz2
prometheus_node_collector-a1a3633d89520c6e941908fb6e93a94dca6eeb2a.tar.xz
prometheus_node_collector-a1a3633d89520c6e941908fb6e93a94dca6eeb2a.zip
Move regexp to global in meminfo_linux.go
Compile regexp outside of parsing function in meminfo_linux.go Signed-off-by: Shapor Naghibzadeh <shapor@google.com>
-rw-r--r--collector/meminfo_linux.go7
1 files changed, 5 insertions, 2 deletions
diff --git a/collector/meminfo_linux.go b/collector/meminfo_linux.go
index c5de432..3b9e2e9 100644
--- a/collector/meminfo_linux.go
+++ b/collector/meminfo_linux.go
@@ -25,6 +25,10 @@ import (
25 "strings" 25 "strings"
26) 26)
27 27
28var (
29 reParens = regexp.MustCompile(`\((.*)\)`)
30)
31
28func (c *meminfoCollector) getMemInfo() (map[string]float64, error) { 32func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
29 file, err := os.Open(procFilePath("meminfo")) 33 file, err := os.Open(procFilePath("meminfo"))
30 if err != nil { 34 if err != nil {
@@ -39,7 +43,6 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) {
39 var ( 43 var (
40 memInfo = map[string]float64{} 44 memInfo = map[string]float64{}
41 scanner = bufio.NewScanner(r) 45 scanner = bufio.NewScanner(r)
42 re = regexp.MustCompile(`\((.*)\)`)
43 ) 46 )
44 47
45 for scanner.Scan() { 48 for scanner.Scan() {
@@ -55,7 +58,7 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) {
55 } 58 }
56 key := parts[0][:len(parts[0])-1] // remove trailing : from key 59 key := parts[0][:len(parts[0])-1] // remove trailing : from key
57 // Active(anon) -> Active_anon 60 // Active(anon) -> Active_anon
58 key = re.ReplaceAllString(key, "_${1}") 61 key = reParens.ReplaceAllString(key, "_${1}")
59 switch len(parts) { 62 switch len(parts) {
60 case 2: // no unit 63 case 2: // no unit
61 case 3: // has unit, we presume kB 64 case 3: // has unit, we presume kB