-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetric.go
285 lines (245 loc) · 6.83 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package dogstatsd
import (
"strconv"
"strings"
"github.com/coredns/coredns/plugin"
dto "github.com/prometheus/client_model/go"
)
type key struct {
kind kind
name string
tags tags
index int
}
type kind int
const (
counter kind = 'c'
gauge kind = 'g'
histogram kind = 'h'
)
type metric struct {
kind kind
name string
value float64
rate float64
tags tags
// index of the histogram bucket that the metric was generated from.
index int
count uint64
version uint64
}
func makeMetrics(f *dto.MetricFamily, m *dto.Metric, rand func(min, max float64) float64) []metric {
name := makeName(*f.Name)
tags := makeTags(m)
switch *f.Type {
case dto.MetricType_COUNTER:
return []metric{{
kind: counter,
name: name,
value: *m.Counter.Value,
tags: tags,
}}
case dto.MetricType_GAUGE:
return []metric{{
kind: gauge,
name: name,
value: *m.Gauge.Value,
tags: tags,
}}
case dto.MetricType_HISTOGRAM:
buckets := m.Histogram.Bucket
metrics := make([]metric, 0, len(buckets))
acc := uint64(0)
min := 0.0
for index, bucket := range buckets {
cct := *bucket.CumulativeCount
max := *bucket.UpperBound
metrics = append(metrics, metric{
kind: histogram,
name: name,
value: rand(min, max),
tags: tags,
index: index,
count: cct - acc,
version: cct,
})
acc = cct
min = max
}
return metrics
default:
// case dto.MetricType_SUMMARY:
// case dto.MetricType_UNTYPED:
//
// For now summary and untyped metrics are not used in coredns, so we
// will skip generating them.
return nil
}
}
func makeName(s string) string {
const prefix = plugin.Namespace + "_"
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}
func appendMetric(b []byte, m metric) []byte {
b = appendName(b, m.name)
b = append(b, ':')
b = strconv.AppendFloat(b, m.value, 'g', -1, 64)
b = append(b, '|')
b = append(b, byte(m.kind))
if m.rate != 0 && m.rate != 1 {
b = append(b, '|', '@')
b = strconv.AppendFloat(b, m.rate, 'g', -1, 64)
}
if len(m.tags) != 0 {
b = append(b, '|', '#')
b = append(b, m.tags...)
}
return append(b, '\n')
}
func appendName(b []byte, s string) []byte {
// Dogstatsd metric names must start with a letter. Here we are not checking
// for this condition because in the context of coredns all metric names are
// prefixed with "coredns_" and therefore state with a letter.
for _, c := range s {
switch {
case isUnderscore(c):
// Dogstatsd systems use periods as namespace delimiers, whereas
// prometheus uses underscores. This may have the side effect of
// replacing underscores with periods in the metric name part of
// the prometheus metric, but it doesn't seem out of place in the
// context of a dogstatsd metric. We could have tried to identify
// the namespace and subsystem and only put periods for delimiters
// between those, but this is error prone and hard to reason about
// when it doesn't work as expected (think about a subsystem with
// underscores). To keep things simple, we just replace all initial
// underscores with periods.
b = append(b, '.')
case isValidNameRune(c):
b = append(b, byte(c))
default:
b = append(b, '_')
}
}
return b
}
func appendTagName(b []byte, s string) []byte {
for _, c := range s {
switch {
case isColumn(c):
// Not sure prometheus allows it but just in case.
b = append(b, '_')
case isAlphaUpper(c):
c = toLower(c)
fallthrough
case isValidTagRune(c):
b = append(b, byte(c))
default:
b = append(b, '_')
}
}
return b
}
func appendTagValue(b []byte, s string) []byte {
for _, c := range s {
switch {
case isAlphaUpper(c):
c = toLower(c)
fallthrough
case isValidTagRune(c):
b = append(b, byte(c))
default:
b = append(b, '_')
}
}
return b
}
func isValidNameRune(c rune) bool {
return isAlphaNum(c) || isUnderscore(c) || isPeriod(c)
}
func isValidTagRune(c rune) bool {
return isAlphaNum(c) || isUnderscore(c) || isPeriod(c) || isMinus(c) || isSlash(c) || isColumn(c)
}
func isAlphaNum(c rune) bool { return isAlpha(c) || isNum(c) }
func isAlpha(c rune) bool { return isAlphaUpper(c) || isAlphaLower(c) }
func isAlphaUpper(c rune) bool { return c >= 'A' && c <= 'Z' }
func isAlphaLower(c rune) bool { return c >= 'a' && c <= 'z' }
func isNum(c rune) bool { return c >= '0' && c <= '9' }
func isUnderscore(c rune) bool { return c == '_' }
func isPeriod(c rune) bool { return c == '.' }
func isMinus(c rune) bool { return c == '-' }
func isSlash(c rune) bool { return c == '/' }
func isColumn(c rune) bool { return c == ':' }
func toLower(c rune) rune { return c + ('a' - 'A') }
type tags string
func makeTags(m *dto.Metric) tags {
if len(m.Label) == 0 {
return ""
}
b := make([]byte, 0, 20*len(m.Label))
for i, p := range m.Label {
if i != 0 {
b = append(b, ',')
}
b = appendTagName(b, *p.Name)
b = append(b, ':')
b = appendTagValue(b, *p.Value)
}
return tags(b)
}
type state map[key]metric
func (s state) observe(m metric) (metric, bool) {
k := key{
kind: m.kind,
name: m.name,
tags: m.tags,
index: m.index,
}
v, ok := s[k]
switch m.kind {
case counter:
// For counters we report the difference from the last value seen for
// the metric.
delta := m.value - v.value
if delta < 0 { // counter reset
delta = m.value
}
if ok = delta != 0; ok {
v, m.value = m, delta
}
case gauge:
// For gauges we simply set the value, the prometheus and dogstatsd
// metric models use the same gauge concept.
//
// Gauges are reported on every flush so the metric collection system
// does not "expire" them if it doesn't receive a value for a while.
v, ok = m, true
case histogram:
// For histograms the appraoch is a bit more complex. Each bucket of a
// prometheus histogram is reported as an individual metric where the
// count is the number of changes that the bucket itself observed, and
// the version is the cummulative count of changes on the bucket and the
// buckets before it.
//
// Those two values must be taken into account to determine whether a
// change occured on the metric. If the version is the same as the last
// one we seen for the metric, then we are sure there were no changes.
// However the version may have changed due to a chance in a sub-bucket,
// which means we must also check the count of changes on the current
// bucket to determine where the changes occured.
//
// Finally, we adjust the rate of the metric to represent the weight of
// each bucket.
count := m.count - v.count
if ok = v.version != m.version && count != 0; ok {
m.rate = 1 / float64(count)
v = m
}
}
s[k] = v
return m, ok
}
func isGoMetric(name string) bool { return strings.HasPrefix(name, "go_") }
func isProcessMetric(name string) bool { return strings.HasPrefix(name, "process_") }