2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2017-07-13 22:34:21 +00:00
|
|
|
"sync"
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RunningProcessor struct {
|
2017-07-13 22:34:21 +00:00
|
|
|
Name string
|
|
|
|
|
|
|
|
sync.Mutex
|
2016-09-08 14:22:10 +00:00
|
|
|
Processor telegraf.Processor
|
|
|
|
Config *ProcessorConfig
|
|
|
|
}
|
|
|
|
|
2016-09-27 15:17:58 +00:00
|
|
|
type RunningProcessors []*RunningProcessor
|
|
|
|
|
|
|
|
func (rp RunningProcessors) Len() int { return len(rp) }
|
|
|
|
func (rp RunningProcessors) Swap(i, j int) { rp[i], rp[j] = rp[j], rp[i] }
|
|
|
|
func (rp RunningProcessors) Less(i, j int) bool { return rp[i].Config.Order < rp[j].Config.Order }
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
// FilterConfig containing a name and filter
|
|
|
|
type ProcessorConfig struct {
|
|
|
|
Name string
|
2016-09-27 15:17:58 +00:00
|
|
|
Order int64
|
2016-09-08 14:22:10 +00:00
|
|
|
Filter Filter
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (rp *RunningProcessor) metricFiltered(metric telegraf.Metric) {
|
|
|
|
metric.Drop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool {
|
|
|
|
for _, m := range metrics {
|
|
|
|
if item == m {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:12:27 +00:00
|
|
|
func (rp *RunningProcessor) Init() error {
|
|
|
|
if p, ok := rp.Processor.(telegraf.Initializer); ok {
|
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func (rp *RunningProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
2017-07-13 22:34:21 +00:00
|
|
|
rp.Lock()
|
|
|
|
defer rp.Unlock()
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ret := []telegraf.Metric{}
|
|
|
|
|
|
|
|
for _, metric := range in {
|
2018-09-28 21:48:20 +00:00
|
|
|
// In processors when a filter selects a metric it is sent through the
|
|
|
|
// processor. Otherwise the metric continues downstream unmodified.
|
|
|
|
if ok := rp.Config.Filter.Select(metric); !ok {
|
|
|
|
ret = append(ret, metric)
|
|
|
|
continue
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
|
|
|
rp.Config.Filter.Modify(metric)
|
|
|
|
if len(metric.FieldList()) == 0 {
|
2018-11-05 21:34:28 +00:00
|
|
|
rp.metricFiltered(metric)
|
2018-09-28 21:48:20 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
// This metric should pass through the filter, so call the filter Apply
|
|
|
|
// function and append results to the output slice.
|
|
|
|
ret = append(ret, rp.Processor.Apply(metric)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|