forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
64 lines (58 loc) · 1.3 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
package kapacitor
import (
"fmt"
"log"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type StatsNode struct {
node
s *pipeline.StatsNode
en Node
closing chan struct{}
}
// Create a new StreamNode 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:
point.Time = now.UTC()
count := s.en.collectedCount()
point.Fields = models.Fields{"collected": count}
for _, out := range s.outs {
err := out.CollectPoint(point)
if err != nil {
return err
}
}
}
}
}
func (s *StatsNode) stopStats() {
close(s.closing)
}