2017-10-10 19:02:01 +00:00
|
|
|
package basicstats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/aggregators"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BasicStats struct {
|
2017-12-15 00:56:10 +00:00
|
|
|
Stats []string `toml:"stats"`
|
2019-09-23 22:39:50 +00:00
|
|
|
Log telegraf.Logger
|
2017-12-15 00:56:10 +00:00
|
|
|
|
|
|
|
cache map[uint64]aggregate
|
|
|
|
statsConfig *configuredStats
|
|
|
|
}
|
|
|
|
|
|
|
|
type configuredStats struct {
|
2019-07-31 06:50:49 +00:00
|
|
|
count bool
|
|
|
|
min bool
|
|
|
|
max bool
|
|
|
|
mean bool
|
|
|
|
variance bool
|
|
|
|
stdev bool
|
|
|
|
sum bool
|
|
|
|
diff bool
|
|
|
|
non_negative_diff bool
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 00:56:10 +00:00
|
|
|
func NewBasicStats() *BasicStats {
|
2019-09-23 22:39:50 +00:00
|
|
|
return &BasicStats{
|
|
|
|
cache: make(map[uint64]aggregate),
|
|
|
|
}
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type aggregate struct {
|
|
|
|
fields map[string]basicstats
|
|
|
|
name string
|
|
|
|
tags map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type basicstats struct {
|
|
|
|
count float64
|
|
|
|
min float64
|
|
|
|
max float64
|
2018-03-05 23:26:31 +00:00
|
|
|
sum float64
|
2017-10-10 19:02:01 +00:00
|
|
|
mean float64
|
2019-07-31 06:50:49 +00:00
|
|
|
diff float64
|
|
|
|
M2 float64 //intermediate value for variance/stdev
|
|
|
|
LAST float64 //intermediate value for diff
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
## The period on which to flush & clear the aggregator.
|
|
|
|
period = "30s"
|
2019-09-23 22:39:50 +00:00
|
|
|
|
2017-10-10 19:02:01 +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
|
2019-02-01 20:12:15 +00:00
|
|
|
|
|
|
|
## Configures which basic stats to push as fields
|
|
|
|
# stats = ["count", "min", "max", "mean", "stdev", "s2", "sum"]
|
2017-10-10 19:02:01 +00:00
|
|
|
`
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (*BasicStats) SampleConfig() string {
|
2017-10-10 19:02:01 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (*BasicStats) Description() string {
|
2017-10-10 19:02:01 +00:00
|
|
|
return "Keep the aggregate basicstats of each metric passing through."
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (b *BasicStats) Add(in telegraf.Metric) {
|
2017-10-10 19:02:01 +00:00
|
|
|
id := in.HashID()
|
2019-09-23 22:39:50 +00:00
|
|
|
if _, ok := b.cache[id]; !ok {
|
2017-10-10 19:02:01 +00:00
|
|
|
// hit an uncached metric, create caches for first time:
|
|
|
|
a := aggregate{
|
|
|
|
name: in.Name(),
|
|
|
|
tags: in.Tags(),
|
|
|
|
fields: make(map[string]basicstats),
|
|
|
|
}
|
2018-09-27 02:08:46 +00:00
|
|
|
for _, field := range in.FieldList() {
|
|
|
|
if fv, ok := convert(field.Value); ok {
|
|
|
|
a.fields[field.Key] = basicstats{
|
2017-10-10 19:02:01 +00:00
|
|
|
count: 1,
|
|
|
|
min: fv,
|
|
|
|
max: fv,
|
|
|
|
mean: fv,
|
2018-03-05 23:26:31 +00:00
|
|
|
sum: fv,
|
2019-07-31 06:50:49 +00:00
|
|
|
diff: 0.0,
|
2017-10-10 19:02:01 +00:00
|
|
|
M2: 0.0,
|
2019-07-31 06:50:49 +00:00
|
|
|
LAST: fv,
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
b.cache[id] = a
|
2017-10-10 19:02:01 +00:00
|
|
|
} else {
|
2018-09-27 02:08:46 +00:00
|
|
|
for _, field := range in.FieldList() {
|
|
|
|
if fv, ok := convert(field.Value); ok {
|
2019-09-23 22:39:50 +00:00
|
|
|
if _, ok := b.cache[id].fields[field.Key]; !ok {
|
2017-10-10 19:02:01 +00:00
|
|
|
// hit an uncached field of a cached metric
|
2019-09-23 22:39:50 +00:00
|
|
|
b.cache[id].fields[field.Key] = basicstats{
|
2017-10-10 19:02:01 +00:00
|
|
|
count: 1,
|
|
|
|
min: fv,
|
|
|
|
max: fv,
|
|
|
|
mean: fv,
|
2018-03-05 23:26:31 +00:00
|
|
|
sum: fv,
|
2019-07-31 06:50:49 +00:00
|
|
|
diff: 0.0,
|
2017-10-10 19:02:01 +00:00
|
|
|
M2: 0.0,
|
2019-07-31 06:50:49 +00:00
|
|
|
LAST: fv,
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
tmp := b.cache[id].fields[field.Key]
|
2017-10-10 19:02:01 +00:00
|
|
|
//https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance
|
|
|
|
//variable initialization
|
|
|
|
x := fv
|
|
|
|
mean := tmp.mean
|
|
|
|
M2 := tmp.M2
|
|
|
|
//counter compute
|
|
|
|
n := tmp.count + 1
|
|
|
|
tmp.count = n
|
|
|
|
//mean compute
|
|
|
|
delta := x - mean
|
|
|
|
mean = mean + delta/n
|
|
|
|
tmp.mean = mean
|
|
|
|
//variance/stdev compute
|
|
|
|
M2 = M2 + delta*(x-mean)
|
|
|
|
tmp.M2 = M2
|
|
|
|
//max/min compute
|
|
|
|
if fv < tmp.min {
|
|
|
|
tmp.min = fv
|
|
|
|
} else if fv > tmp.max {
|
|
|
|
tmp.max = fv
|
|
|
|
}
|
2018-03-05 23:26:31 +00:00
|
|
|
//sum compute
|
|
|
|
tmp.sum += fv
|
2019-07-31 06:50:49 +00:00
|
|
|
//diff compute
|
|
|
|
tmp.diff = fv - tmp.LAST
|
2017-10-10 19:02:01 +00:00
|
|
|
//store final data
|
2019-09-23 22:39:50 +00:00
|
|
|
b.cache[id].fields[field.Key] = tmp
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (b *BasicStats) Push(acc telegraf.Accumulator) {
|
|
|
|
for _, aggregate := range b.cache {
|
2017-10-10 19:02:01 +00:00
|
|
|
fields := map[string]interface{}{}
|
|
|
|
for k, v := range aggregate.fields {
|
2017-12-15 00:56:10 +00:00
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.count {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_count"] = v.count
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.min {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_min"] = v.min
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.max {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_max"] = v.max
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.mean {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_mean"] = v.mean
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.sum {
|
2018-03-05 23:26:31 +00:00
|
|
|
fields[k+"_sum"] = v.sum
|
|
|
|
}
|
2017-12-15 00:56:10 +00:00
|
|
|
|
2017-10-10 19:02:01 +00:00
|
|
|
//v.count always >=1
|
|
|
|
if v.count > 1 {
|
|
|
|
variance := v.M2 / (v.count - 1)
|
2017-12-15 00:56:10 +00:00
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.variance {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_s2"] = variance
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.stdev {
|
2017-12-15 00:56:10 +00:00
|
|
|
fields[k+"_stdev"] = math.Sqrt(variance)
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.diff {
|
2019-07-31 06:50:49 +00:00
|
|
|
fields[k+"_diff"] = v.diff
|
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
if b.statsConfig.non_negative_diff && v.diff >= 0 {
|
2019-07-31 06:50:49 +00:00
|
|
|
fields[k+"_non_negative_diff"] = v.diff
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
//if count == 1 StdDev = infinite => so I won't send data
|
|
|
|
}
|
2017-12-15 00:56:10 +00:00
|
|
|
|
|
|
|
if len(fields) > 0 {
|
|
|
|
acc.AddFields(aggregate.name, fields, aggregate.tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
// member function for logging.
|
|
|
|
func (b *BasicStats) parseStats() *configuredStats {
|
2017-12-15 00:56:10 +00:00
|
|
|
parsed := &configuredStats{}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
for _, name := range b.Stats {
|
2017-12-15 00:56:10 +00:00
|
|
|
switch name {
|
|
|
|
case "count":
|
|
|
|
parsed.count = true
|
|
|
|
case "min":
|
|
|
|
parsed.min = true
|
|
|
|
case "max":
|
|
|
|
parsed.max = true
|
|
|
|
case "mean":
|
|
|
|
parsed.mean = true
|
|
|
|
case "s2":
|
|
|
|
parsed.variance = true
|
|
|
|
case "stdev":
|
|
|
|
parsed.stdev = true
|
2018-03-05 23:26:31 +00:00
|
|
|
case "sum":
|
|
|
|
parsed.sum = true
|
2019-07-31 06:50:49 +00:00
|
|
|
case "diff":
|
|
|
|
parsed.diff = true
|
|
|
|
case "non_negative_diff":
|
|
|
|
parsed.non_negative_diff = true
|
2017-12-15 00:56:10 +00:00
|
|
|
|
|
|
|
default:
|
2019-09-23 22:39:50 +00:00
|
|
|
b.Log.Warnf("Unrecognized basic stat %q, ignoring", name)
|
2017-12-15 00:56:10 +00:00
|
|
|
}
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
2017-12-15 00:56:10 +00:00
|
|
|
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (b *BasicStats) getConfiguredStats() {
|
|
|
|
if b.Stats == nil {
|
|
|
|
b.statsConfig = &configuredStats{
|
|
|
|
count: true,
|
|
|
|
min: true,
|
|
|
|
max: true,
|
|
|
|
mean: true,
|
|
|
|
variance: true,
|
|
|
|
stdev: true,
|
|
|
|
sum: false,
|
|
|
|
non_negative_diff: false,
|
2017-12-15 00:56:10 +00:00
|
|
|
}
|
2019-09-23 22:39:50 +00:00
|
|
|
} else {
|
|
|
|
b.statsConfig = b.parseStats()
|
2017-12-15 00:56:10 +00:00
|
|
|
}
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (b *BasicStats) Reset() {
|
|
|
|
b.cache = make(map[uint64]aggregate)
|
2017-10-10 19:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func convert(in interface{}) (float64, bool) {
|
|
|
|
switch v := in.(type) {
|
|
|
|
case float64:
|
|
|
|
return v, true
|
|
|
|
case int64:
|
|
|
|
return float64(v), true
|
2018-06-30 02:07:08 +00:00
|
|
|
case uint64:
|
|
|
|
return float64(v), true
|
2017-10-10 19:02:01 +00:00
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
func (b *BasicStats) Init() error {
|
|
|
|
b.getConfiguredStats()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:02:01 +00:00
|
|
|
func init() {
|
|
|
|
aggregators.Add("basicstats", func() telegraf.Aggregator {
|
|
|
|
return NewBasicStats()
|
|
|
|
})
|
|
|
|
}
|