forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
81 lines (75 loc) · 1.6 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
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 {
ticker := time.NewTicker(s.s.Interval)
defer ticker.Stop()
point := models.Point{
Name: "stats",
Tags: map[string]string{"node": s.en.Name()},
}
for {
select {
case <-s.closing:
return nil
case now := <-ticker.C:
s.timer.Start()
point.Time = now.UTC()
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()
}
}
}
func (s *StatsNode) stopStats() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.closed {
s.closed = true
close(s.closing)
}
}