-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
158 lines (122 loc) · 3.14 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
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
package pag
import (
"errors"
"io"
"sort"
"strconv"
"strings"
)
var (
ErrEmptyName = errors.New("empty name")
ErrEmptyLabel = errors.New("empty label")
ErrLabelValueNotString = errors.New("label value is not string")
ErrInvalidLabel = errors.New("invalid label")
)
type MetricType string
func (s MetricType) String() string { return string(s) }
const (
Counter MetricType = "counter"
Histogram MetricType = "histogram"
)
type MetricConfig struct {
Help string `json:"help" yaml:"help"`
Type MetricType `json:"type" yaml:"type"`
Buckets []string `json:"buckets" yaml:"buckets"`
}
func (s MetricConfig) LabelValues() map[string]map[string]bool {
if s.Type == Histogram && len(s.Buckets) > 0 {
vs := make(map[string]bool, len(s.Buckets))
for _, b := range s.Buckets {
vs[b] = true
}
return map[string]map[string]bool{"le": vs}
}
return nil
}
func EncodeLabels(labels map[string]string) string {
if len(labels) == 0 {
return ""
}
vs := make([]string, 0, len(labels))
for k, v := range labels {
vs = append(vs, k+"="+`"`+v+`"`)
}
sort.Strings(vs)
return "{" + strings.Join(vs, ",") + "}"
}
func ParseMetric(s string) (name string, labels map[string]string, err error) {
s = strings.TrimSpace(s)
if len(s) == 0 {
return "", nil, ErrEmptyName
}
i := strings.IndexByte(s, '{')
j := strings.LastIndexByte(s, '}')
if (i == -1) != (j == -1) {
return "", nil, ErrInvalidLabel
}
if i == -1 && j == -1 {
return s, nil, nil
}
if i == 0 {
return "", nil, ErrInvalidLabel
}
if i >= j {
return "", nil, ErrInvalidLabel
}
if j != len(s)-1 {
return "", nil, ErrInvalidLabel
}
name = s[:i]
for _, kv := range strings.Split(s[i+1:j], ",") {
p := strings.SplitN(kv, "=", 2)
if len(p) != 2 {
return "", nil, ErrInvalidLabel
}
label := strings.TrimSpace(p[0])
value := strings.TrimSpace(p[1])
if len(label) == 0 || len(value) == 0 {
return "", nil, ErrEmptyLabel
}
if len(value) <= 2 || value[0] != '"' || value[len(value)-1] != '"' {
return "", nil, ErrLabelValueNotString
}
if labels == nil {
labels = make(map[string]string)
}
labels[label] = value[1 : len(p[1])-1]
}
return name, labels, nil
}
func PrintMetric(w io.Writer, prefix string, name string, config MetricConfig, values map[string]map[string]float64) (count int) {
names := []string{name}
if config.Type == Histogram {
names = []string{name + "_bucket", name + "_sum", name + "_count"}
}
for _, m := range names {
for l, v := range values[m] {
if count == 0 {
w.Write([]byte("# HELP " + prefix + name + " " + config.Help + "\n"))
w.Write([]byte("# TYPE " + prefix + name + " " + config.Type.String() + "\n"))
}
w.Write([]byte(prefix + m))
w.Write([]byte(l))
w.Write([]byte(" "))
w.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64)))
w.Write([]byte("\n"))
count++
}
}
if count > 0 {
w.Write([]byte("\n\n"))
}
return count
}
var histSuffixes = [...]string{"_count", "_sum", "_bucket"}
func StripHistSuffix(s string) string {
for _, suffix := range histSuffixes {
if strings.HasSuffix(s, suffix) {
return s[:len(s)-len(suffix)]
}
}
return s
}