2016-09-08 14:22:10 +00:00
|
|
|
package minmax
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/aggregators"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MinMax struct {
|
2016-10-07 15:43:44 +00:00
|
|
|
cache map[uint64]aggregate
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func NewMinMax() telegraf.Aggregator {
|
|
|
|
mm := &MinMax{}
|
|
|
|
mm.Reset()
|
|
|
|
return mm
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:43:44 +00:00
|
|
|
type aggregate struct {
|
|
|
|
fields map[string]minmax
|
|
|
|
name string
|
|
|
|
tags map[string]string
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
type minmax struct {
|
2016-09-20 13:15:23 +00:00
|
|
|
min float64
|
|
|
|
max float64
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-10-06 12:29:46 +00:00
|
|
|
## General Aggregator Arguments:
|
|
|
|
## The period on which to flush & clear the aggregator.
|
2016-09-08 14:22:10 +00:00
|
|
|
period = "30s"
|
2016-10-06 12:29:46 +00:00
|
|
|
## If true, the original metric will be dropped by the
|
|
|
|
## aggregator and will not get sent to the output plugins.
|
|
|
|
drop_original = false
|
2016-09-08 14:22:10 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (m *MinMax) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MinMax) Description() string {
|
|
|
|
return "Keep the aggregate min/max of each metric passing through."
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func (m *MinMax) Add(in telegraf.Metric) {
|
2016-09-08 14:22:10 +00:00
|
|
|
id := in.HashID()
|
2016-10-07 15:43:44 +00:00
|
|
|
if _, ok := m.cache[id]; !ok {
|
2016-09-08 14:22:10 +00:00
|
|
|
// hit an uncached metric, create caches for first time:
|
2016-10-07 15:43:44 +00:00
|
|
|
a := aggregate{
|
|
|
|
name: in.Name(),
|
|
|
|
tags: in.Tags(),
|
|
|
|
fields: make(map[string]minmax),
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
for k, v := range in.Fields() {
|
2016-09-20 13:15:23 +00:00
|
|
|
if fv, ok := convert(v); ok {
|
2016-10-07 15:43:44 +00:00
|
|
|
a.fields[k] = minmax{
|
2016-09-20 13:15:23 +00:00
|
|
|
min: fv,
|
|
|
|
max: fv,
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-07 15:43:44 +00:00
|
|
|
m.cache[id] = a
|
2016-09-08 14:22:10 +00:00
|
|
|
} else {
|
|
|
|
for k, v := range in.Fields() {
|
2016-09-20 13:15:23 +00:00
|
|
|
if fv, ok := convert(v); ok {
|
2016-10-07 15:43:44 +00:00
|
|
|
if _, ok := m.cache[id].fields[k]; !ok {
|
2016-09-20 13:15:23 +00:00
|
|
|
// hit an uncached field of a cached metric
|
2016-10-07 15:43:44 +00:00
|
|
|
m.cache[id].fields[k] = minmax{
|
2016-09-20 13:15:23 +00:00
|
|
|
min: fv,
|
|
|
|
max: fv,
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-10-07 15:43:44 +00:00
|
|
|
if fv < m.cache[id].fields[k].min {
|
|
|
|
tmp := m.cache[id].fields[k]
|
2016-09-20 13:15:23 +00:00
|
|
|
tmp.min = fv
|
2016-10-07 15:43:44 +00:00
|
|
|
m.cache[id].fields[k] = tmp
|
|
|
|
} else if fv > m.cache[id].fields[k].max {
|
|
|
|
tmp := m.cache[id].fields[k]
|
2016-09-20 13:15:23 +00:00
|
|
|
tmp.max = fv
|
2016-10-07 15:43:44 +00:00
|
|
|
m.cache[id].fields[k] = tmp
|
2016-09-20 13:15:23 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func (m *MinMax) Push(acc telegraf.Accumulator) {
|
2016-10-07 15:43:44 +00:00
|
|
|
for _, aggregate := range m.cache {
|
2016-09-22 17:10:51 +00:00
|
|
|
fields := map[string]interface{}{}
|
2016-10-07 15:43:44 +00:00
|
|
|
for k, v := range aggregate.fields {
|
2016-09-22 17:10:51 +00:00
|
|
|
fields[k+"_min"] = v.min
|
|
|
|
fields[k+"_max"] = v.max
|
|
|
|
}
|
2016-10-07 15:43:44 +00:00
|
|
|
acc.AddFields(aggregate.name, fields, aggregate.tags)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func (m *MinMax) Reset() {
|
2016-10-07 15:43:44 +00:00
|
|
|
m.cache = make(map[uint64]aggregate)
|
2016-09-20 13:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func convert(in interface{}) (float64, bool) {
|
|
|
|
switch v := in.(type) {
|
2016-09-08 14:22:10 +00:00
|
|
|
case float64:
|
2016-09-20 13:15:23 +00:00
|
|
|
return v, true
|
|
|
|
case int64:
|
|
|
|
return float64(v), true
|
2018-06-30 02:07:08 +00:00
|
|
|
case uint64:
|
|
|
|
return float64(v), true
|
2016-09-08 14:22:10 +00:00
|
|
|
default:
|
2016-09-20 13:15:23 +00:00
|
|
|
return 0, false
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
aggregators.Add("minmax", func() telegraf.Aggregator {
|
2016-09-22 17:10:51 +00:00
|
|
|
return NewMinMax()
|
2016-09-08 14:22:10 +00:00
|
|
|
})
|
|
|
|
}
|