Files
2018-08-24 12:30:02 -07:00

58 lines
1.1 KiB
Go

package sampler
import (
"fmt"
"hash/fnv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
type Sampler struct {
PercentOfMetrics int `toml:"percent_of_metrics"`
SampleField string `toml:"sample_field"`
}
func (s *Sampler) SampleConfig() string {
return `
[[processors.sampler]]
percent_of_metrics = 5
## field to be sampled over
sample_field = "trace_id"`
}
func (s *Sampler) Description() string {
return "will pass through a random sampling of metrics"
}
func (s *Sampler) Apply(in ...telegraf.Metric) []telegraf.Metric {
nMetrics := make([]telegraf.Metric, 0)
for _, metric := range in {
value := metric.Fields()[s.SampleField]
if value == "" {
return nil
}
if metric.Fields()["stddev_away"] != nil {
nMetrics = append(nMetrics, metric)
}
h := fnv.New64a()
h.Write([]byte(fmt.Sprintf("%v", value)))
hash := h.Sum64()
hash = hash % 100
if hash >= 0 && hash <= uint64(s.PercentOfMetrics) {
nMetrics = append(nMetrics, metric)
}
}
return nMetrics
}
func init() {
processors.Add("sampler", func() telegraf.Processor {
return &Sampler{}
})
}