forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
108 lines (101 loc) · 2.13 KB
/
stats.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
package kapacitor
import (
"fmt"
"log"
"sync"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type StatsNode struct {
node
s *pipeline.StatsNode
en Node
closing chan struct{}
closed bool
mu sync.Mutex
}
// Create a new FromNode which filters data from a source.
func newStatsNode(et *ExecutingTask, n *pipeline.StatsNode, l *log.Logger) (*StatsNode, error) {
// Lookup the executing node for stats.
en := et.lookup[n.SourceNode.ID()]
if en == nil {
return nil, fmt.Errorf("no node found for %s", n.SourceNode.Name())
}
sn := &StatsNode{
node: node{Node: n, et: et, logger: l},
s: n,
en: en,
closing: make(chan struct{}),
}
sn.node.runF = sn.runStats
sn.node.stopF = sn.stopStats
return sn, nil
}
func (s *StatsNode) runStats([]byte) error {
if s.s.AlignFlag {
// Wait till we are roughly aligned with the interval.
now := time.Now()
next := now.Truncate(s.s.Interval).Add(s.s.Interval)
after := time.NewTicker(next.Sub(now))
select {
case <-after.C:
after.Stop()
case <-s.closing:
after.Stop()
return nil
}
if err := s.emit(now); err != nil {
return err
}
}
ticker := time.NewTicker(s.s.Interval)
defer ticker.Stop()
for {
select {
case <-s.closing:
return nil
case now := <-ticker.C:
if err := s.emit(now); err != nil {
return err
}
}
}
}
// Emit a set of stats data points.
func (s *StatsNode) emit(now time.Time) error {
s.timer.Start()
point := models.Point{
Name: "stats",
Tags: map[string]string{"node": s.en.Name()},
Time: now.UTC(),
}
if s.s.AlignFlag {
point.Time = point.Time.Round(s.s.Interval)
}
stats := s.en.nodeStatsByGroup()
for group, stat := range stats {
point.Fields = stat.Fields
point.Group = group
point.Dimensions = stat.Dimensions
point.Tags = stat.Tags
s.timer.Pause()
for _, out := range s.outs {
err := out.CollectPoint(point)
if err != nil {
return err
}
}
s.timer.Resume()
}
s.timer.Stop()
return nil
}
func (s *StatsNode) stopStats() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.closed {
s.closed = true
close(s.closing)
}
}