forked from alicebob/asprom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.go
82 lines (73 loc) · 1.67 KB
/
metric.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
package main
import (
"log"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type metric struct {
typ prometheus.ValueType
aeroName string
desc string
}
// cmetrics is promkey -> prom metric
type cmetrics map[string]cmetric
type cmetric struct {
desc *prometheus.Desc
typ prometheus.ValueType
}
// infoCollect parses RequestInfo() results and handles the metrics
func infoCollect(
ch chan<- prometheus.Metric,
metrics cmetrics,
info string,
labelValues ...string,
) {
stats := parseInfo(info)
for key, m := range metrics {
v, ok := stats[key]
if !ok {
// key presence depends on (namespace) configuration
continue
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Printf("%q invalid value %q: %s", key, v, err)
continue
}
ch <- prometheus.MustNewConstMetric(m.desc, m.typ, f, labelValues...)
}
}
func parseInfo(s string) map[string]string {
r := map[string]string{}
for _, l := range strings.Split(s, ";") {
for _, v := range strings.Split(l, ":") {
kv := strings.SplitN(v, "=", 2)
if len(kv) > 1 {
r[kv[0]] = kv[1]
}
}
}
return r
}
// gauge is a helper to add an aerospike metric
func gauge(name string, desc string) metric {
return metric{
typ: prometheus.GaugeValue,
aeroName: name,
desc: desc,
}
}
// counter is a helper to add an aerospike metric
func counter(name string, desc string) metric {
return metric{
typ: prometheus.CounterValue,
aeroName: name,
desc: desc,
}
}
// promkey makes the prom metric name out of an aerospike stat name
func promkey(sys, key string) string {
k := strings.Replace(key, "-", "_", -1)
return namespace + "_" + sys + "_" + k
}