2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-11-05 21:34:28 +00:00
|
|
|
"sync"
|
2016-09-08 14:22:10 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RunningAggregator struct {
|
2018-11-05 21:34:28 +00:00
|
|
|
sync.Mutex
|
|
|
|
Aggregator telegraf.Aggregator
|
|
|
|
Config *AggregatorConfig
|
2016-10-10 12:43:47 +00:00
|
|
|
periodStart time.Time
|
|
|
|
periodEnd time.Time
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
MetricsPushed selfstat.Stat
|
|
|
|
MetricsFiltered selfstat.Stat
|
|
|
|
MetricsDropped selfstat.Stat
|
|
|
|
PushTime selfstat.Stat
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunningAggregator(
|
2018-11-05 21:34:28 +00:00
|
|
|
aggregator telegraf.Aggregator,
|
|
|
|
config *AggregatorConfig,
|
2016-09-22 17:10:51 +00:00
|
|
|
) *RunningAggregator {
|
|
|
|
return &RunningAggregator{
|
2018-11-05 21:34:28 +00:00
|
|
|
Aggregator: aggregator,
|
|
|
|
Config: config,
|
|
|
|
MetricsPushed: selfstat.Register(
|
|
|
|
"aggregate",
|
|
|
|
"metrics_pushed",
|
|
|
|
map[string]string{"aggregator": config.Name},
|
|
|
|
),
|
|
|
|
MetricsFiltered: selfstat.Register(
|
|
|
|
"aggregate",
|
|
|
|
"metrics_filtered",
|
|
|
|
map[string]string{"aggregator": config.Name},
|
|
|
|
),
|
|
|
|
MetricsDropped: selfstat.Register(
|
|
|
|
"aggregate",
|
|
|
|
"metrics_dropped",
|
|
|
|
map[string]string{"aggregator": config.Name},
|
|
|
|
),
|
|
|
|
PushTime: selfstat.Register(
|
|
|
|
"aggregate",
|
|
|
|
"push_time_ns",
|
|
|
|
map[string]string{"aggregator": config.Name},
|
|
|
|
),
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// AggregatorConfig is the common config for all aggregators.
|
2016-09-08 14:22:10 +00:00
|
|
|
type AggregatorConfig struct {
|
2018-09-28 21:48:20 +00:00
|
|
|
Name string
|
|
|
|
DropOriginal bool
|
|
|
|
Period time.Duration
|
|
|
|
Delay time.Duration
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
NameOverride string
|
|
|
|
MeasurementPrefix string
|
|
|
|
MeasurementSuffix string
|
|
|
|
Tags map[string]string
|
|
|
|
Filter Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningAggregator) Name() string {
|
|
|
|
return "aggregators." + r.Config.Name
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningAggregator) Period() time.Duration {
|
|
|
|
return r.Config.Period
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningAggregator) SetPeriodStart(start time.Time) {
|
|
|
|
r.periodStart = start
|
|
|
|
r.periodEnd = r.periodStart.Add(r.Config.Period).Add(r.Config.Delay)
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (r *RunningAggregator) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
2016-09-08 14:22:10 +00:00
|
|
|
m := makemetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
metric,
|
2016-09-08 14:22:10 +00:00
|
|
|
r.Config.NameOverride,
|
|
|
|
r.Config.MeasurementPrefix,
|
|
|
|
r.Config.MeasurementSuffix,
|
|
|
|
r.Config.Tags,
|
2018-09-28 21:48:20 +00:00
|
|
|
nil)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2016-12-13 12:24:39 +00:00
|
|
|
if m != nil {
|
|
|
|
m.SetAggregate(true)
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
r.MetricsPushed.Incr(1)
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningAggregator) metricFiltered(metric telegraf.Metric) {
|
|
|
|
r.MetricsFiltered.Incr(1)
|
|
|
|
metric.Accept()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningAggregator) metricDropped(metric telegraf.Metric) {
|
|
|
|
r.MetricsDropped.Incr(1)
|
|
|
|
metric.Accept()
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// Add a metric to the aggregator and return true if the original metric
|
|
|
|
// should be dropped.
|
|
|
|
func (r *RunningAggregator) Add(metric telegraf.Metric) bool {
|
|
|
|
if ok := r.Config.Filter.Select(metric); !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:56:44 +00:00
|
|
|
metric = metric.Copy()
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
r.Config.Filter.Modify(metric)
|
|
|
|
if len(metric.FieldList()) == 0 {
|
|
|
|
return r.Config.DropOriginal
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2018-09-28 21:48:20 +00:00
|
|
|
|
2018-12-11 00:07:08 +00:00
|
|
|
if r.periodStart.IsZero() || metric.Time().After(r.periodEnd) {
|
2018-11-05 21:34:28 +00:00
|
|
|
r.metricDropped(metric)
|
2018-12-11 00:07:08 +00:00
|
|
|
return r.Config.DropOriginal
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
r.Aggregator.Add(metric)
|
|
|
|
return r.Config.DropOriginal
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningAggregator) Push(acc telegraf.Accumulator) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2016-09-22 17:10:51 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
r.periodStart = r.periodEnd
|
|
|
|
r.periodEnd = r.periodStart.Add(r.Config.Period).Add(r.Config.Delay)
|
|
|
|
r.push(acc)
|
|
|
|
r.Aggregator.Reset()
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningAggregator) push(acc telegraf.Accumulator) {
|
|
|
|
start := time.Now()
|
|
|
|
r.Aggregator.Push(acc)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
r.PushTime.Incr(elapsed.Nanoseconds())
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|