Document and add support to input plugins for logging alias (#6357)

This commit is contained in:
Greg
2019-09-23 16:39:50 -06:00
committed by Daniel Nelson
parent e42d2e39c6
commit 817c9a69a9
111 changed files with 961 additions and 659 deletions

View File

@@ -10,6 +10,7 @@ emitting the aggregate every `period` seconds.
[[aggregators.basicstats]]
## The period on which to flush & clear the aggregator.
period = "30s"
## If true, the original metric will be dropped by the
## aggregator and will not get sent to the output plugins.
drop_original = false

View File

@@ -1,7 +1,6 @@
package basicstats
import (
"log"
"math"
"github.com/influxdata/telegraf"
@@ -10,6 +9,7 @@ import (
type BasicStats struct {
Stats []string `toml:"stats"`
Log telegraf.Logger
cache map[uint64]aggregate
statsConfig *configuredStats
@@ -28,9 +28,9 @@ type configuredStats struct {
}
func NewBasicStats() *BasicStats {
mm := &BasicStats{}
mm.Reset()
return mm
return &BasicStats{
cache: make(map[uint64]aggregate),
}
}
type aggregate struct {
@@ -53,6 +53,7 @@ type basicstats struct {
var sampleConfig = `
## The period on which to flush & clear the aggregator.
period = "30s"
## If true, the original metric will be dropped by the
## aggregator and will not get sent to the output plugins.
drop_original = false
@@ -61,17 +62,17 @@ var sampleConfig = `
# stats = ["count", "min", "max", "mean", "stdev", "s2", "sum"]
`
func (m *BasicStats) SampleConfig() string {
func (*BasicStats) SampleConfig() string {
return sampleConfig
}
func (m *BasicStats) Description() string {
func (*BasicStats) Description() string {
return "Keep the aggregate basicstats of each metric passing through."
}
func (m *BasicStats) Add(in telegraf.Metric) {
func (b *BasicStats) Add(in telegraf.Metric) {
id := in.HashID()
if _, ok := m.cache[id]; !ok {
if _, ok := b.cache[id]; !ok {
// hit an uncached metric, create caches for first time:
a := aggregate{
name: in.Name(),
@@ -92,13 +93,13 @@ func (m *BasicStats) Add(in telegraf.Metric) {
}
}
}
m.cache[id] = a
b.cache[id] = a
} else {
for _, field := range in.FieldList() {
if fv, ok := convert(field.Value); ok {
if _, ok := m.cache[id].fields[field.Key]; !ok {
if _, ok := b.cache[id].fields[field.Key]; !ok {
// hit an uncached field of a cached metric
m.cache[id].fields[field.Key] = basicstats{
b.cache[id].fields[field.Key] = basicstats{
count: 1,
min: fv,
max: fv,
@@ -111,7 +112,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
continue
}
tmp := m.cache[id].fields[field.Key]
tmp := b.cache[id].fields[field.Key]
//https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance
//variable initialization
x := fv
@@ -138,32 +139,30 @@ func (m *BasicStats) Add(in telegraf.Metric) {
//diff compute
tmp.diff = fv - tmp.LAST
//store final data
m.cache[id].fields[field.Key] = tmp
b.cache[id].fields[field.Key] = tmp
}
}
}
}
func (m *BasicStats) Push(acc telegraf.Accumulator) {
config := getConfiguredStats(m)
for _, aggregate := range m.cache {
func (b *BasicStats) Push(acc telegraf.Accumulator) {
for _, aggregate := range b.cache {
fields := map[string]interface{}{}
for k, v := range aggregate.fields {
if config.count {
if b.statsConfig.count {
fields[k+"_count"] = v.count
}
if config.min {
if b.statsConfig.min {
fields[k+"_min"] = v.min
}
if config.max {
if b.statsConfig.max {
fields[k+"_max"] = v.max
}
if config.mean {
if b.statsConfig.mean {
fields[k+"_mean"] = v.mean
}
if config.sum {
if b.statsConfig.sum {
fields[k+"_sum"] = v.sum
}
@@ -171,16 +170,16 @@ func (m *BasicStats) Push(acc telegraf.Accumulator) {
if v.count > 1 {
variance := v.M2 / (v.count - 1)
if config.variance {
if b.statsConfig.variance {
fields[k+"_s2"] = variance
}
if config.stdev {
if b.statsConfig.stdev {
fields[k+"_stdev"] = math.Sqrt(variance)
}
if config.diff {
if b.statsConfig.diff {
fields[k+"_diff"] = v.diff
}
if config.non_negative_diff && v.diff >= 0 {
if b.statsConfig.non_negative_diff && v.diff >= 0 {
fields[k+"_non_negative_diff"] = v.diff
}
@@ -194,14 +193,12 @@ func (m *BasicStats) Push(acc telegraf.Accumulator) {
}
}
func parseStats(names []string) *configuredStats {
// member function for logging.
func (b *BasicStats) parseStats() *configuredStats {
parsed := &configuredStats{}
for _, name := range names {
for _, name := range b.Stats {
switch name {
case "count":
parsed.count = true
case "min":
@@ -222,45 +219,32 @@ func parseStats(names []string) *configuredStats {
parsed.non_negative_diff = true
default:
log.Printf("W! Unrecognized basic stat '%s', ignoring", name)
b.Log.Warnf("Unrecognized basic stat %q, ignoring", name)
}
}
return parsed
}
func defaultStats() *configuredStats {
defaults := &configuredStats{}
defaults.count = true
defaults.min = true
defaults.max = true
defaults.mean = true
defaults.variance = true
defaults.stdev = true
defaults.sum = false
defaults.non_negative_diff = false
return defaults
}
func getConfiguredStats(m *BasicStats) *configuredStats {
if m.statsConfig == nil {
if m.Stats == nil {
m.statsConfig = defaultStats()
} else {
m.statsConfig = parseStats(m.Stats)
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,
}
} else {
b.statsConfig = b.parseStats()
}
return m.statsConfig
}
func (m *BasicStats) Reset() {
m.cache = make(map[uint64]aggregate)
func (b *BasicStats) Reset() {
b.cache = make(map[uint64]aggregate)
}
func convert(in interface{}) (float64, bool) {
@@ -276,6 +260,12 @@ func convert(in interface{}) (float64, bool) {
}
}
func (b *BasicStats) Init() error {
b.getConfiguredStats()
return nil
}
func init() {
aggregators.Add("basicstats", func() telegraf.Aggregator {
return NewBasicStats()

View File

@@ -39,6 +39,8 @@ var m2, _ = metric.New("m1",
func BenchmarkApply(b *testing.B) {
minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
for n := 0; n < b.N; n++ {
minmax.Add(m1)
@@ -50,6 +52,8 @@ func BenchmarkApply(b *testing.B) {
func TestBasicStatsWithPeriod(t *testing.T) {
acc := testutil.Accumulator{}
minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
minmax.Add(m1)
minmax.Add(m2)
@@ -106,6 +110,8 @@ func TestBasicStatsWithPeriod(t *testing.T) {
func TestBasicStatsDifferentPeriods(t *testing.T) {
acc := testutil.Accumulator{}
minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
minmax.Add(m1)
minmax.Push(&acc)
@@ -181,6 +187,8 @@ func TestBasicStatsWithOnlyCount(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"count"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -208,6 +216,8 @@ func TestBasicStatsWithOnlyMin(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"min"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -235,6 +245,8 @@ func TestBasicStatsWithOnlyMax(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"max"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -262,6 +274,8 @@ func TestBasicStatsWithOnlyMean(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"mean"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -289,6 +303,8 @@ func TestBasicStatsWithOnlySum(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"sum"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -347,6 +363,8 @@ func TestBasicStatsWithOnlySumFloatingPointErrata(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"sum"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(sum1)
aggregator.Add(sum2)
@@ -368,6 +386,8 @@ func TestBasicStatsWithOnlyVariance(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"s2"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -393,6 +413,8 @@ func TestBasicStatsWithOnlyStandardDeviation(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"stdev"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -418,6 +440,8 @@ func TestBasicStatsWithMinAndMax(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"min", "max"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -452,6 +476,8 @@ func TestBasicStatsWithDiff(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"diff"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -477,6 +503,8 @@ func TestBasicStatsWithNonNegativeDiff(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"non_negative_diff"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -500,7 +528,9 @@ func TestBasicStatsWithNonNegativeDiff(t *testing.T) {
func TestBasicStatsWithAllStats(t *testing.T) {
acc := testutil.Accumulator{}
minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.Stats = []string{"count", "min", "max", "mean", "stdev", "s2", "sum"}
minmax.getConfiguredStats()
minmax.Add(m1)
minmax.Add(m2)
@@ -564,6 +594,8 @@ func TestBasicStatsWithNoStats(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -579,6 +611,8 @@ func TestBasicStatsWithUnknownStat(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Stats = []string{"crazy"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)
@@ -596,6 +630,8 @@ func TestBasicStatsWithUnknownStat(t *testing.T) {
func TestBasicStatsWithDefaultStats(t *testing.T) {
aggregator := NewBasicStats()
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1)
aggregator.Add(m2)