forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
112 lines (104 loc) · 2.91 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
109
110
111
112
package pipeline
import (
"encoding/json"
"fmt"
"time"
"github.com/influxdata/influxdb/influxql"
)
// A StatsNode emits internal statistics about the another node at a given interval.
//
// The interval represents how often to emit the statistics based on real time.
// This means the interval time is independent of the times of the data points the other node is receiving.
// As a result the StatsNode is a root node in the task pipeline.
//
//
// The currently available internal statistics:
//
// * emitted -- the number of points or batches this node has sent to its children.
//
// Each stat is available as a field in the data stream.
//
// The stats are in groups according to the original data.
// Meaning that if the source node is grouped by the tag 'host' as an example,
// then the counts are output per host with the appropriate 'host' tag.
// Since its possible for groups to change when crossing a node only the emitted groups
// are considered.
//
// Example:
// var data = stream
// |from()...
// // Emit statistics every 1 minute and cache them via the HTTP API.
// data
// |stats(1m)
// |httpOut('stats')
// // Continue normal processing of the data stream
// data...
//
// WARNING: It is not recommended to join the stats stream with the original data stream.
// Since they operate on different clocks you could potentially create a deadlock.
// This is a limitation of the current implementation and may be removed in the future.
type StatsNode struct {
chainnode `json:"-"`
// tick:ignore
SourceNode Node `json:"-"`
// tick:ignore
Interval time.Duration `json:"interval"`
// tick:ignore
AlignFlag bool `tick:"Align" json:"align"`
}
func newStatsNode(n Node, interval time.Duration) *StatsNode {
return &StatsNode{
chainnode: newBasicChainNode("stats", StreamEdge, StreamEdge),
SourceNode: n,
Interval: interval,
}
}
// MarshalJSON converts StatsNode to JSON
// tick:ignore
func (n *StatsNode) MarshalJSON() ([]byte, error) {
type Alias StatsNode
var raw = &struct {
TypeOf
*Alias
Interval string `json:"interval"`
}{
TypeOf: TypeOf{
Type: "stats",
ID: n.ID(),
},
Alias: (*Alias)(n),
Interval: influxql.FormatDuration(n.Interval),
}
return json.Marshal(raw)
}
// UnmarshalJSON converts JSON to an StatsNode
// tick:ignore
func (n *StatsNode) UnmarshalJSON(data []byte) error {
type Alias StatsNode
var raw = &struct {
TypeOf
*Alias
Interval string `json:"interval"`
}{
Alias: (*Alias)(n),
}
err := json.Unmarshal(data, raw)
if err != nil {
return err
}
if raw.Type != "stats" {
return fmt.Errorf("error unmarshaling node %d of type %s as StatsNode", raw.ID, raw.Type)
}
n.Interval, err = influxql.ParseDuration(raw.Interval)
if err != nil {
return err
}
n.setID(raw.ID)
return nil
}
// Round times to the StatsNode.Interval value.
// tick:property
func (n *StatsNode) Align() *StatsNode {
n.AlignFlag = true
return n
}