-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
derivative.go
183 lines (164 loc) · 4.44 KB
/
derivative.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
//go:generate ../../../tools/readme_config_includer/generator
package derivative
import (
_ "embed"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/aggregators"
)
//go:embed sample.conf
var sampleConfig string
type Derivative struct {
Variable string `toml:"variable"`
Suffix string `toml:"suffix"`
MaxRollOver uint `toml:"max_roll_over"`
Log telegraf.Logger `toml:"-"`
cache map[uint64]*aggregate
}
type aggregate struct {
first *event
last *event
name string
tags map[string]string
rollOver uint
}
type event struct {
fields map[string]float64
time time.Time
}
const defaultSuffix = "_rate"
func NewDerivative() *Derivative {
derivative := &Derivative{Suffix: defaultSuffix, MaxRollOver: 10}
derivative.cache = make(map[uint64]*aggregate)
derivative.Reset()
return derivative
}
func (*Derivative) SampleConfig() string {
return sampleConfig
}
func (d *Derivative) Add(in telegraf.Metric) {
id := in.HashID()
current, ok := d.cache[id]
if !ok {
// hit an uncached metric, create caches for first time:
d.cache[id] = newAggregate(in)
return
}
if current.first.time.After(in.Time()) {
current.first = newEvent(in)
current.rollOver = 0
} else if current.first.time.Equal(in.Time()) {
upsertConvertedFields(in.Fields(), current.first.fields)
current.rollOver = 0
}
if current.last.time.Before(in.Time()) {
current.last = newEvent(in)
current.rollOver = 0
} else if current.last.time.Equal(in.Time()) {
upsertConvertedFields(in.Fields(), current.last.fields)
current.rollOver = 0
}
}
func newAggregate(in telegraf.Metric) *aggregate {
event := newEvent(in)
return &aggregate{
name: in.Name(),
tags: in.Tags(),
first: event,
last: event,
rollOver: 0,
}
}
func newEvent(in telegraf.Metric) *event {
return &event{
fields: extractConvertedFields(in),
time: in.Time(),
}
}
func extractConvertedFields(in telegraf.Metric) map[string]float64 {
fields := make(map[string]float64, len(in.Fields()))
upsertConvertedFields(in.Fields(), fields)
return fields
}
func upsertConvertedFields(source map[string]interface{}, target map[string]float64) {
for k, v := range source {
if value, ok := convert(v); ok {
target[k] = value
}
}
}
func convert(in interface{}) (float64, bool) {
switch v := in.(type) {
case float64:
return v, true
case int64:
return float64(v), true
case uint64:
return float64(v), true
}
return 0, false
}
func (d *Derivative) Push(acc telegraf.Accumulator) {
for _, aggregate := range d.cache {
if aggregate.first == aggregate.last {
d.Log.Debugf("Same first and last event for %q, skipping.", aggregate.name)
continue
}
var denominator float64
denominator = aggregate.last.time.Sub(aggregate.first.time).Seconds()
if len(d.Variable) > 0 {
var first float64
var last float64
var found bool
if first, found = aggregate.first.fields[d.Variable]; !found {
d.Log.Debugf("Did not find %q in first event for %q.", d.Variable, aggregate.name)
continue
}
if last, found = aggregate.last.fields[d.Variable]; !found {
d.Log.Debugf("Did not find %q in last event for %q.", d.Variable, aggregate.name)
continue
}
denominator = last - first
}
if denominator == 0 {
d.Log.Debugf("Got difference 0 in denominator for %q, skipping.", aggregate.name)
continue
}
derivatives := make(map[string]interface{})
for key, start := range aggregate.first.fields {
if key == d.Variable {
// Skip derivation variable
continue
}
if end, ok := aggregate.last.fields[key]; ok {
d.Log.Debugf("Adding derivative %q to %q.", key+d.Suffix, aggregate.name)
derivatives[key+d.Suffix] = (end - start) / denominator
}
}
acc.AddFields(aggregate.name, derivatives, aggregate.tags)
}
}
func (d *Derivative) Reset() {
for id, aggregate := range d.cache {
if aggregate.rollOver < d.MaxRollOver {
aggregate.first = aggregate.last
aggregate.rollOver = aggregate.rollOver + 1
d.cache[id] = aggregate
d.Log.Debugf("Roll-Over %q for the %d time.", aggregate.name, aggregate.rollOver)
} else {
delete(d.cache, id)
d.Log.Debugf("Removed %q from cache.", aggregate.name)
}
}
}
func (d *Derivative) Init() error {
d.Suffix = strings.TrimSpace(d.Suffix)
d.Variable = strings.TrimSpace(d.Variable)
return nil
}
func init() {
aggregators.Add("derivative", func() telegraf.Aggregator {
return NewDerivative()
})
}