-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetric_output.go
434 lines (355 loc) · 9.09 KB
/
metric_output.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package circonusgometrics
import (
"bufio"
"bytes"
"fmt"
"strings"
"sync"
"time"
"github.com/circonus-labs/go-apiclient"
"github.com/openhistogram/circonusllhist"
"github.com/pkg/errors"
)
func (m *CirconusMetrics) packageMetrics() (map[string]*apiclient.CheckBundleMetric, Metrics) {
m.packagingmu.Lock()
defer m.packagingmu.Unlock()
// if m.Debug {
// m.Log.Printf("packaging metrics\n")
// }
var ts uint64
// always submitting a timestamp forces the broker to treat the check as though
// it is "async" which doesn't work well for "group" checks with multiple submitters
// e.g. circonus-agent with a group statsd check
// if m.submitTimestamp == nil {
// ts = makeTimestamp(time.Now())
// } else {
if m.submitTimestamp != nil {
ts = makeTimestamp(*m.submitTimestamp)
m.Log.Printf("setting custom timestamp %v -> %v (UTC ms)", *m.submitTimestamp, ts)
}
newMetrics := make(map[string]*apiclient.CheckBundleMetric)
counters, gauges, histograms, text := m.snapshot()
m.custm.Lock()
output := make(Metrics, len(counters)+len(gauges)+len(histograms)+len(text)+len(m.custom))
if len(m.custom) > 0 {
// add and reset any custom metrics
for mn, mv := range m.custom {
output[mn] = mv
}
m.custom = make(map[string]Metric)
}
m.custm.Unlock()
for name, value := range counters {
send := m.check.IsMetricActive(name)
if !send && m.check.ActivateMetric(name) {
send = true
newMetrics[name] = &apiclient.CheckBundleMetric{
Name: name,
Type: "numeric",
Status: "active",
}
}
if send {
metric := Metric{Type: "L", Value: value}
if ts > 0 {
metric.Timestamp = ts
}
output[name] = metric
}
}
for name, value := range gauges {
send := m.check.IsMetricActive(name)
if !send && m.check.ActivateMetric(name) {
send = true
newMetrics[name] = &apiclient.CheckBundleMetric{
Name: name,
Type: "numeric",
Status: "active",
}
}
if send {
metric := Metric{Type: m.getGaugeType(value), Value: value}
if ts > 0 {
metric.Timestamp = ts
}
output[name] = metric
}
}
for name, value := range histograms {
send := m.check.IsMetricActive(name)
if !send && m.check.ActivateMetric(name) {
send = true
newMetrics[name] = &apiclient.CheckBundleMetric{
Name: name,
Type: "histogram",
Status: "active",
}
}
if send {
buf := bytes.NewBuffer([]byte{})
if err := value.SerializeB64(buf); err != nil {
m.Log.Printf("[ERR] serializing histogram %s: %s", name, err)
} else {
// histograms b64 serialized support timestamps
metric := Metric{Type: "h", Value: buf.String()}
if ts > 0 {
metric.Timestamp = ts
}
output[name] = metric
}
// output[name] = Metric{Type: "h", Value: value.DecStrings()} // histograms do NOT get timestamps
}
}
for name, value := range text {
send := m.check.IsMetricActive(name)
if !send && m.check.ActivateMetric(name) {
send = true
newMetrics[name] = &apiclient.CheckBundleMetric{
Name: name,
Type: "text",
Status: "active",
}
}
if send {
metric := Metric{Type: "s", Value: value}
if ts > 0 {
metric.Timestamp = ts
}
output[name] = metric
}
}
m.lastMetrics.metricsmu.Lock()
defer m.lastMetrics.metricsmu.Unlock()
m.lastMetrics.metrics = &output
m.lastMetrics.ts = time.Now()
// reset the submission timestamp
m.submitTimestamp = nil
return newMetrics, output
}
// PromOutput returns lines of metrics in prom format
func (m *CirconusMetrics) PromOutput() (*bytes.Buffer, error) {
m.lastMetrics.metricsmu.Lock()
defer m.lastMetrics.metricsmu.Unlock()
if m.lastMetrics.metrics == nil {
return nil, errors.New("no metrics available")
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
ts := m.lastMetrics.ts.UnixNano() / int64(time.Millisecond)
for name, metric := range *m.lastMetrics.metrics {
switch metric.Type {
case "n":
if strings.HasPrefix(fmt.Sprintf("%v", metric.Value), "[H[") {
continue // circonus histogram != prom "histogram" (aka percentile)
}
case "h":
continue // circonus histogram != prom "histogram" (aka percentile)
case "s":
continue // text metrics unsupported
}
fmt.Fprintf(w, "%s %v %d\n", name, metric.Value, ts)
}
err := w.Flush()
if err != nil {
return nil, errors.Wrap(err, "flushing metric buffer")
}
return &b, err
}
// FlushMetricsNoReset flushes current metrics to a structure and returns it (does NOT send to Circonus).
func (m *CirconusMetrics) FlushMetricsNoReset() *Metrics {
m.flushmu.Lock()
if m.flushing {
m.flushmu.Unlock()
return &Metrics{}
}
m.flushing = true
m.flushmu.Unlock()
// save values configured at startup
resetC := m.resetCounters
resetG := m.resetGauges
resetH := m.resetHistograms
resetT := m.resetText
// override Reset* to false for this call
m.resetCounters = false
m.resetGauges = false
m.resetHistograms = false
m.resetText = false
_, output := m.packageMetrics()
// restore previous values
m.resetCounters = resetC
m.resetGauges = resetG
m.resetHistograms = resetH
m.resetText = resetT
m.flushmu.Lock()
m.flushing = false
m.flushmu.Unlock()
return &output
}
// FlushMetrics flushes current metrics to a structure and returns it (does NOT send to Circonus)
func (m *CirconusMetrics) FlushMetrics() *Metrics {
m.flushmu.Lock()
if m.flushing {
m.flushmu.Unlock()
return &Metrics{}
}
m.flushing = true
m.flushmu.Unlock()
_, output := m.packageMetrics()
m.flushmu.Lock()
m.flushing = false
m.flushmu.Unlock()
return &output
}
// Flush metrics kicks off the process of sending metrics to Circonus
func (m *CirconusMetrics) Flush() {
m.flushmu.Lock()
if m.flushing {
m.flushmu.Unlock()
return
}
m.flushing = true
m.flushmu.Unlock()
newMetrics, output := m.packageMetrics()
if len(output) > 0 {
m.submit(output, newMetrics)
} /* else if m.Debug {
m.Log.Printf("no metrics to send, skipping\n")
}*/
m.flushmu.Lock()
m.flushing = false
m.flushmu.Unlock()
}
// Reset removes all existing counters and gauges.
func (m *CirconusMetrics) Reset() {
m.cm.Lock()
defer m.cm.Unlock()
m.cfm.Lock()
defer m.cfm.Unlock()
m.gm.Lock()
defer m.gm.Unlock()
m.gfm.Lock()
defer m.gfm.Unlock()
m.hm.Lock()
defer m.hm.Unlock()
m.tm.Lock()
defer m.tm.Unlock()
m.tfm.Lock()
defer m.tfm.Unlock()
m.counters = make(map[string]uint64)
m.counterFuncs = make(map[string]func() uint64)
m.gauges = make(map[string]interface{})
m.gaugeFuncs = make(map[string]func() int64)
m.histograms = make(map[string]*Histogram)
m.text = make(map[string]string)
m.textFuncs = make(map[string]func() string)
}
// snapshot returns a copy of the values of all registered counters and gauges.
func (m *CirconusMetrics) snapshot() (
map[string]uint64, // counters
map[string]interface{}, // gauges
map[string]*circonusllhist.Histogram, // histograms
map[string]string) { // text
var h map[string]*circonusllhist.Histogram
var c map[string]uint64
var g map[string]interface{}
var t map[string]string
var wg sync.WaitGroup
wg.Add(1)
go func() {
h = m.snapHistograms()
wg.Done()
}()
wg.Add(1)
go func() {
c = m.snapCounters()
wg.Done()
}()
wg.Add(1)
go func() {
g = m.snapGauges()
wg.Done()
}()
wg.Add(1)
go func() {
t = m.snapText()
wg.Done()
}()
wg.Wait()
return c, g, h, t
}
func (m *CirconusMetrics) snapCounters() map[string]uint64 {
m.cm.Lock()
m.cfm.Lock()
c := make(map[string]uint64, len(m.counters)+len(m.counterFuncs))
for n, v := range m.counters {
c[n] = v
}
if m.resetCounters && len(c) > 0 {
m.counters = make(map[string]uint64)
}
for n, f := range m.counterFuncs {
c[n] = f()
}
m.cm.Unlock()
m.cfm.Unlock()
return c
}
func (m *CirconusMetrics) snapGauges() map[string]interface{} {
m.gm.Lock()
m.gfm.Lock()
g := make(map[string]interface{}, len(m.gauges)+len(m.gaugeFuncs))
for n, v := range m.gauges {
g[n] = v
}
if m.resetGauges && len(g) > 0 {
m.gauges = make(map[string]interface{})
}
for n, f := range m.gaugeFuncs {
g[n] = f()
}
m.gm.Unlock()
m.gfm.Unlock()
return g
}
func (m *CirconusMetrics) snapHistograms() map[string]*circonusllhist.Histogram {
m.hm.Lock()
h := make(map[string]*circonusllhist.Histogram, len(m.histograms))
for n, hist := range m.histograms {
hist.rw.Lock()
if m.resetHistograms {
h[n] = hist.hist.CopyAndReset()
} else {
h[n] = hist.hist.Copy()
}
hist.rw.Unlock()
}
if m.resetHistograms && len(h) > 0 {
m.histograms = make(map[string]*Histogram)
}
m.hm.Unlock()
return h
}
func (m *CirconusMetrics) snapText() map[string]string {
m.tm.Lock()
m.tfm.Lock()
t := make(map[string]string, len(m.text)+len(m.textFuncs))
for n, v := range m.text {
t[n] = v
}
if m.resetText && len(t) > 0 {
m.text = make(map[string]string)
}
for n, f := range m.textFuncs {
t[n] = f()
}
m.tm.Unlock()
m.tfm.Unlock()
return t
}
// makeTimestamp returns timestamp in ms units for _ts metric value
func makeTimestamp(ts time.Time) uint64 {
return uint64(ts.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)))
}