-
Notifications
You must be signed in to change notification settings - Fork 6
/
metrics.go
693 lines (601 loc) · 16.7 KB
/
metrics.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//
// June 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Extract metrics from telemetry data. This module is independent of
// the specific metrics package (e.g. prometheus, opentsdb etc). The
// specific metrics package handling can be found in metrics_x.go.
//
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"sync"
"sync/atomic"
"time"
)
type metricsOutputType int
type metricsOutputContext interface{}
//
// Defaults
const (
//
// Period when we look for stale stat records to evict
STATEVICTIONPERIOD = 1 * time.Minute
//
// Stat for sensor which is not updated for 5 minutes is dropped
// as stale making way for others.
STATEVICTIONSTALE = 300000
)
const (
metricsTypePrometheus = iota
metricsTypeInflux
metricsTypeTest
)
var metricsTypeMap = map[string]metricsOutputType{
"prometheus": metricsTypePrometheus,
"influx": metricsTypeInflux,
"test": metricsTypeTest,
}
type metricsOutputHandler interface {
setupWorkers(module *metricsOutputModule)
buildMetric(tags []metricsAtom, sensor metricsAtom, ts uint64,
context metricsOutputContext)
flushMetric(tags []metricsAtom, ts uint64, context metricsOutputContext)
adaptSensorName(name string) string
adaptTagName(name string) string
}
var testOutputHandler metricsOutputHandler
//
// Structures used to collect metrics and tags.
type metricsAtom struct {
key string
val interface{}
}
//
// The metricsSpecForBasepath is made of metricSpecNode objects which
// are themselves organised as a hierarchy and together they describe
// the metrics extracted from a message associated with a basepath.
//
// The global metricSpec represents a collection of
// metricsSpecForBasepath objects, and describes the set of metrics
// which are extracted from the streams.
//
// The metrics module is expected to take dMs in (message atoms
// produced by the input side), and produces:
//
// sensor_name {key1=value1, key2=value2, ...} value timestamp
//
type metricsSpecNodeType int
const (
metricsSpecNodeTypeSensor = iota
metricsSpecNodeTypeTag
metricsSpecNodeTypeContainer
// Last value for sizing array
metricsSpecNodeTypeARRAYSIZE
)
type metricsSpecNode struct {
//
// Content directly from the spec.
//
// - Name is the sensor or tag field name
// - Tag indicates that field is tag field name if set to true
// - Track indicate that we are exporting delta between
// collections as part of metamonitoring.
// - Fields provides for recursion
//
Name string
Tag bool
Track bool
Fields []*metricsSpecNode
//
// Derived content to support efficient generation of metrics
//
// Track derived nodetype to support efficient traversal
nodeType metricsSpecNodeType
//
// If we have fields beneath this node, than we have a map
// which allows us to find nodes quickly. We also track
// composition.
fieldsMap map[string]*metricsSpecNode
fieldsMapsByType [metricsSpecNodeTypeARRAYSIZE]map[string]*metricsSpecNode
//
// For sensor and tag nodes, track the sensor name which will be
// the path down the hierarchy (for disambiguation).
fqName string
//
// Internal names of children nodes have been cached per
// dataMsgStreamType. Value is loaded/stored atomically
//
// list is indexed by dMStreamType.
internalNamesCached []atomic.Value
internalNamesCachedLock sync.Mutex
//
// Map, per encoding, the name specified externally and how it
// maps to the internal name. e.g. for compact gpb, we go from
// process-cpu-fifteen-minute to ProcessCpuFifteenMinute;
// ProcessCpuFifteenMinute uint32
// `protobuf:"varint,5,opt,name=process_cpu_fifteen_minute,json=processCpuFifteenMinute"
// json:"process_cpu_fifteen_minute,omitempty"` Note that we
// populate this field the first time we use it. We could improve
// this by doing it when we create the spec.
//
// list is indexed by dMStreamType.
internalName []string
}
type metricsSpecForBasepath struct {
// Base path
Basepath string
// Root specNode at this basepath
Spec *metricsSpecNode
}
//
// Stat collection
type statRecord struct {
sync.Mutex
lastTS uint64
deltaTS prometheus.Observer
}
type statRecordTable struct {
sync.Mutex
statRecords map[string]*statRecord
//
// If we are collecting statistics, we will have specs here.
//
// - Number of sensors stated
//
statSensorCount int
//
// module name
name string
//
// Channel used for eviction timer
evict chan bool
}
func (rt *statRecordTable) statRecordFindOrCreate(
key string, ts uint64) *statRecord {
rt.Lock()
defer rt.Unlock()
r, ok := rt.statRecords[key]
if !ok {
if len(rt.statRecords) >= rt.statSensorCount {
// We are oversubscribed, no space. We won't sample this
// one until some stale ones are evicted.
return nil
}
r = &statRecord{
lastTS: ts,
deltaTS: metricsStatMonitor.deltaTS.WithLabelValues(key),
}
rt.statRecords[key] = r
}
return r
}
func (rt *statRecordTable) statRecordUpdate(
key string,
ts uint64) {
r := rt.statRecordFindOrCreate(key, ts)
if r == nil {
metricsStatMonitor.statCacheFull.WithLabelValues(key).Inc()
return
}
//
// We have one of these already. We need to compute delta from previous and
// reigster observation, and update to new ts
r.Lock()
defer r.Unlock()
// logger.WithFields(
// log.Fields{
// "name": rt.name,
// "key": key,
// "newts": ts,
// "oldts": r.lastTS,
// "delta": float64(ts - r.lastTS),
// }).Debug("stat tracking record update")
if ts > r.lastTS {
r.deltaTS.Observe(float64(ts - r.lastTS))
r.lastTS = ts
} else if ts < r.lastTS {
//
// Protest loudly. No time travel allowed.
metricsStatMonitor.outOfSeq.WithLabelValues(key).Inc()
}
}
func (rt *statRecordTable) scheduleEviction() {
time.Sleep(STATEVICTIONPERIOD)
rt.evict <- true
}
func (rt *statRecordTable) evictStale() {
ts := time.Now().Unix() * 1000
count := 0
rt.Lock()
defer rt.Unlock()
for key, r := range rt.statRecords {
r.Lock()
if uint64(ts) > r.lastTS && uint64(ts)-r.lastTS > STATEVICTIONSTALE {
delete(rt.statRecords, key)
count++
}
r.Unlock()
}
if count > 0 {
logger.WithFields(
log.Fields{
"name": rt.name,
"evicted count": count,
"occupied": len(rt.statRecords),
"max": rt.statSensorCount,
}).Debug("stats tracking evicted statRecord entries from cache")
}
}
type metricsSpec struct {
specSet []metricsSpecForBasepath
//
// Lookup support mapping input to metrics derived
// from metrics specification
specDB map[string]*metricsSpecNode
//
// Statistics records tracked against his spec.
stats *statRecordTable
}
type metricsOutputModule struct {
//
// Module section name
name string
//
// Metrics specification provided by operator, and derived
// structures used in the datapath
inputSpecFile string
inputSpec metricsSpec
//
// Output handler shipping the metrics out
output string
outputHandler metricsOutputHandler
//
// Interaction with producers and orchestrator
dataChannelDepth int
ctrlChan chan *ctrlMsg
dataChan chan dataMsg
// Interaction with output handlers. When control loop learns it
// is time to leave, shutdownChan is closed, and wait on wait
// group. This allows us to exit clean.
shutdownSyncPoint sync.WaitGroup
shutdownChan chan struct{}
}
func metricsOutputModuleNew() outputNodeModule {
return &metricsOutputModule{}
}
func (m *metricsOutputModule) buildSpecDBSubtree(
node *metricsSpecNode, path string, conjoinsymbol string) error {
if node.Name != "" {
if path != "" {
path = path + conjoinsymbol + node.Name
} else {
path = node.Name
}
}
logCtx := logger.WithFields(log.Fields{
"name": m.name,
"path": path,
})
//
// Initialise internal name translation
// machinery. internalNamesCached is set up to indicate that child
// nodes name mapping per stream type is uncached. Set internal
// node name to empty too.
node.internalNamesCached = make([]atomic.Value, dMStreamMsgDefault)
for i := 0; i < int(dMStreamMsgDefault); i++ {
node.internalNamesCached[i].Store(false)
}
node.internalName = make([]string, dMStreamMsgDefault)
//
// Initialise maps tracking the various fields
node.fieldsMap = make(map[string]*metricsSpecNode)
for i := 0; i < metricsSpecNodeTypeARRAYSIZE; i++ {
node.fieldsMapsByType[i] = make(map[string]*metricsSpecNode)
}
//
// Validate and determine node type
if node.Tag {
if len(node.Fields) != 0 {
err := fmt.Errorf(
"Node %v is labelled 'Tag' but contains 'Fields'\n", node)
logCtx.WithError(err).Error("Setting up metrics spec")
return err
}
if node.Track {
err := fmt.Errorf(
"Node %v is labelled 'Tag' and 'Track' (track ignored)\n", node)
logCtx.WithError(err).Error("Setting up metrics spec")
}
node.nodeType = metricsSpecNodeTypeTag
node.fqName = m.outputHandler.adaptTagName(path)
} else if len(node.Fields) != 0 {
node.nodeType = metricsSpecNodeTypeContainer
if node.Track {
err := fmt.Errorf(
"Node %v has children and is tagged 'Track' (track ignored)\n",
node)
logCtx.WithError(err).Error("Setting up metrics spec")
}
//
// We must populate the fieldsMap such that as we
// navigate, we can easily identify child nodes.
for _, child := range node.Fields {
node.fieldsMap[child.Name] = child
err := m.buildSpecDBSubtree(child, path, conjoinsymbol)
if err != nil {
logCtx.WithError(err).Error("Setting up metrics spec")
return err
}
node.fieldsMapsByType[child.nodeType][child.Name] = child
}
} else {
node.nodeType = metricsSpecNodeTypeSensor
node.fqName = m.outputHandler.adaptSensorName(path)
if node.Track {
logCtx.WithFields(log.Fields{
"track": node.fqName,
}).Debug("Setting up metrics spec")
}
}
return nil
}
func (m *metricsOutputModule) buildSpecDB() error {
m.inputSpec.specDB = make(map[string]*metricsSpecNode)
for _, sbp := range m.inputSpec.specSet {
//
// Taverse the node hierarchy and build map per node level for
// fast access to corresponding children nodes. Loop rather
// than recurse. If we were to include the base path in the
// metric name, we would replace "" with sbp.Basepath.
err := m.buildSpecDBSubtree(sbp.Spec, "", "__")
if err != nil {
return err
}
//
// Finally link the root node to the root of the lookup tree
m.inputSpec.specDB[sbp.Basepath] = sbp.Spec
logger.WithFields(log.Fields{
"name": m.name,
"basepath": sbp.Basepath,
}).Info("setup metrics collection")
}
return nil
}
func (m *metricsOutputModule) controlLoop() {
var stats msgStats
logCtx := logger.WithFields(log.Fields{
"name": m.name,
"output": m.output,
"file": m.inputSpecFile,
})
//
// Kick off data handlers and run control loop
m.outputHandler.setupWorkers(m)
for {
select {
case <-m.inputSpec.stats.evict:
m.inputSpec.stats.evictStale()
go m.inputSpec.stats.scheduleEviction()
case msg := <-m.ctrlChan:
switch msg.id {
case REPORT:
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
logCtx.Info("metrics producer rxed SHUTDOWN")
//
// Signal any children that we are done.
close(m.shutdownChan)
m.shutdownSyncPoint.Wait()
//
// We're done pass it on. Would have been so nice to
// use this wait group pattern trhoughout.
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return
default:
logCtx.Error("metrics producer, unknown ctrl message")
}
}
}
}
//
// Handle configuration for metrics
func (m *metricsOutputModule) configure(name string, nc nodeConfig) (
error, chan<- dataMsg, chan<- *ctrlMsg) {
var stat statRecordTable
var err error
//
// Who am I?
m.name = name
//
// Load configuration of what metrics to extract.
m.inputSpecFile, err = nc.config.GetString(name, "file")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"file option: metrics to collect required")
return err, nil, nil
}
//
// Parse input file in preparation for handling telemetry and to
// indicate any input file errors at startup right here. Read it
// in one go.
specByteStream, err := ioutil.ReadFile(m.inputSpecFile)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
"file": m.inputSpecFile,
}).Error("file option: reading file")
return err, nil, nil
}
err = json.Unmarshal(specByteStream, &m.inputSpec.specSet)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
"file": m.inputSpecFile,
}).Error("file option: parsing JSON metric spec")
return err, nil, nil
}
m.output, err = nc.config.GetString(name, "output")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
"options": metricsTypeMap}).Error(
"output option: required")
return err, nil, nil
}
outputType, ok := metricsTypeMap[m.output]
if !ok {
err = fmt.Errorf(
"invalid 'output' [%s], must be one of [%v]",
m.output, metricsTypeMap)
logger.WithError(err).WithFields(
log.Fields{
"name": name,
"options": metricsTypeMap}).Error(
"output option: unsupported")
return err, nil, nil
}
//
// Let output handler set itself up.
switch outputType {
case metricsTypePrometheus:
m.outputHandler, err = metricsPrometheusNew(name, nc)
if err != nil {
//
// Errors logged in called function
return err, nil, nil
}
case metricsTypeInflux:
m.outputHandler, err = metricsInfluxNew(name, nc)
if err != nil {
//
// Errors logged in called function
return err, nil, nil
}
case metricsTypeTest:
m.outputHandler = testOutputHandler
if m.outputHandler == nil {
return fmt.Errorf("test metric extraction unset"), nil, nil
}
default:
logger.WithFields(
log.Fields{
"name": name,
"output": m.output,
}).Error("output option: failed to setup")
return err, nil, nil
}
err = m.buildSpecDB()
if err != nil {
//
// Logging in called function.
return err, nil, nil
}
//
// Check if we are tracking statistics on collections
// up.
m.inputSpec.stats = &stat
stat.statSensorCount, _ = nc.config.GetInt(name, "statsensorcount")
if stat.statSensorCount != 0 {
stat.statRecords = make(map[string]*statRecord, stat.statSensorCount)
//
// We want to clean stale statRecord entries to make room for
// new ones.
stat.name = name
stat.evict = make(chan bool, 1)
go stat.scheduleEviction()
}
m.dataChannelDepth, err = nc.config.GetInt(name, "datachanneldepth")
if err != nil {
m.dataChannelDepth = DATACHANNELDEPTH
}
logger.WithFields(
log.Fields{
"name": name,
"output": m.output,
"file": m.inputSpecFile,
"metricSpec": m.inputSpec,
}).Debug("metrics export configured")
//
// Setup control and data channels
m.ctrlChan = make(chan *ctrlMsg)
m.dataChan = make(chan dataMsg, m.dataChannelDepth)
m.shutdownChan = make(chan struct{})
go m.controlLoop()
return nil, m.dataChan, m.ctrlChan
}
func metricsSetupDumpfile(filename string, logctx *log.Entry) (
*os.File, *bufio.Writer) {
dumpfile, err := os.OpenFile(
filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0660)
if err != nil {
logctx.WithError(err).WithFields(log.Fields{
"metricsDumpFilename": filename,
}).Error("Setting up metrics debug dump file")
return nil, nil
}
dumpwriter := bufio.NewWriter(dumpfile)
return dumpfile, dumpwriter
}
type metricsStatMonitorType struct {
deltaTS *prometheus.SummaryVec
outOfSeq *prometheus.CounterVec
statCacheFull *prometheus.CounterVec
basePathMetricsError *prometheus.CounterVec
}
var metricsStatMonitor metricsStatMonitorType
func init() {
metricsStatMonitor = metricsStatMonitorType{
deltaTS: prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "timestamp_delta",
Help: "Summary for timestamp delta between updates",
},
[]string{"key"}),
outOfSeq: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "outofseq",
Help: "Out Of Sequence Timestamps",
},
[]string{"key"}),
statCacheFull: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "statCacheFull",
Help: "Exhausted stats record cache",
},
[]string{"key"}),
basePathMetricsError: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "metrics_base_path_error",
Help: "Counter tracking metrics errors per-base_path",
},
[]string{"section", "source", "base_path", "errortype"}),
}
prometheus.MustRegister(metricsStatMonitor.deltaTS)
prometheus.MustRegister(metricsStatMonitor.outOfSeq)
prometheus.MustRegister(metricsStatMonitor.statCacheFull)
prometheus.MustRegister(metricsStatMonitor.basePathMetricsError)
}