/
parsekvp.go
160 lines (121 loc) · 3.75 KB
/
parsekvp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"github.com/pkg/errors"
"math/big"
"regexp"
"strconv"
"strings"
)
var kvpRegex = regexp.MustCompile(`\s*(?P<key>[a-zA-Z0-9.#\-_]+)=(?P<value>"(\w|\s)*"|[!-~]*)\s*`)
// Regex to find/replace with nothing
var normalizeRegex = regexp.MustCompile(`^sample#`)
var numWithUnitRegex = regexp.MustCompile(`^(\d+(\.\d+)?)([a-zA-Z]+)$`)
// parseKvp This function returns a map of all the key-value pairs within a log
// message. This returns an `error` object if the string isn't a valid kvp
// format.
func parseKvp(msg string) (map[string]interface{}, error) {
matches, err := extractKvp(msg)
if err != nil {
return nil, err
}
payload := make(map[string]interface{})
for _, m := range matches {
k := m[0]
v := m[1]
if v == "" {
payload[k] = nil
continue
} else if v[0] == '"' && v[len(v)-1] == '"' {
v = v[1 : len(v)-1]
}
samples := parseMetricValue(v)
for _, s := range samples {
name := normalizeMetricName(k, s.Unit)
payload[name] = s.Value
}
}
return payload, nil
}
// extractKvp Returns a slice of tuple strings where t[0] is the key and
// t[1] is the value. The slice will be nil if there's a parsing error
func extractKvp(msg string) ([][]string, error) {
tuples := [][]string{}
logger.Debugf("matching `%s`", msg)
for i := 0; i < len(msg); {
match := kvpRegex.FindStringSubmatchIndex(msg[i:])
if match == nil {
return nil, errors.New("Parsing error: invalid kvp format")
}
if match[0] != 0 {
return nil, errors.New("Parsing error: invalid kvp format")
}
logger.Debugf("Match? %v, `%s`", match, msg[i+match[0]:i+match[1]])
tuples = append(tuples, []string{msg[i+match[2] : i+match[3]], msg[i+match[4] : i+match[5]]})
logger.Debugf("%+v", tuples)
i += match[1]
}
return tuples, nil
}
func normalizeMetricName(rawMetric string, unit *string) string {
nn := normalizeRegex.ReplaceAllString(rawMetric, "")
// NOTE: For whatever reason, some of the keys for the Postgres's metrics use
// hyphens instead of underscores; this simply converts it...
nn = strings.Replace(nn, "-", "_", -1)
if nn == "memory_pgpgin" || nn == "memory_pgpgout" || unit == nil {
// Don't suffix, since the only unit is pages
return nn
}
return nn + "_" + *unit
}
type metricSample struct {
Value interface{}
Unit *string
}
// parseMetricValue Returns a parsed value. If `unit` is nil, that means that
// the parsed value is simply a string.
func parseMetricValue(value string) []*metricSample {
samples := make([]*metricSample, 0)
// Checks for unit-less numerical value
if n, err := parseNumber(value); err == nil {
f := big.NewFloat(n)
if f.IsInt() {
i, _ := f.Int64()
samples = append(samples, &metricSample{Value: int(i)})
} else {
samples = append(samples, &metricSample{Value: n})
}
return samples
}
matches := numWithUnitRegex.FindAllStringSubmatch(value, -1)
// Checks if value doesn't match a numerical value w/ unit
if len(matches) == 0 {
samples = append(samples, &metricSample{Value: value})
return samples
}
pv, _ := parseNumber(matches[0][1])
pUnit := matches[0][3]
samples = append(samples, &metricSample{Value: pv, Unit: &pUnit})
nv, nUnit := normalizeStorageSize(pv, pUnit)
if nUnit != pUnit {
samples = append(samples, &metricSample{Value: nv, Unit: &nUnit})
}
return samples
}
func normalizeStorageSize(num float64, unit string) (float64, string) {
switch strings.ToLower(unit) {
case "b", "bytes":
return num / 1024 / 1024, "MB"
case "kb", "kilobytes":
return num / 1024, "MB"
case "mb", "megabytes":
return num, "MB"
case "gb", "gigabytes":
return num * 1024, "MB"
default:
// Unsure what the unit of measure is, just return it as is
return num, unit
}
}
func parseNumber(value string) (float64, error) {
return strconv.ParseFloat(value, 64)
}