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"
|
2019-08-21 23:49:07 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RunningProcessor struct {
|
2017-07-13 22:34:21 +00:00
|
|
|
sync.Mutex
|
2019-08-21 23:49:07 +00:00
|
|
|
log telegraf.Logger
|
2020-06-05 14:43:43 +00:00
|
|
|
Processor telegraf.StreamingProcessor
|
2016-09-08 14:22:10 +00:00
|
|
|
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
|
2019-08-21 23:49:07 +00:00
|
|
|
Alias string
|
2016-09-27 15:17:58 +00:00
|
|
|
Order int64
|
2016-09-08 14:22:10 +00:00
|
|
|
Filter Filter
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func NewRunningProcessor(processor telegraf.StreamingProcessor, config *ProcessorConfig) *RunningProcessor {
|
2019-10-23 19:40:31 +00:00
|
|
|
tags := map[string]string{"processor": config.Name}
|
|
|
|
if config.Alias != "" {
|
|
|
|
tags["alias"] = config.Alias
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:40:29 +00:00
|
|
|
processErrorsRegister := selfstat.Register("process", "errors", tags)
|
|
|
|
logger := NewLogger("processors", config.Name, config.Alias)
|
|
|
|
logger.OnErr(func() {
|
|
|
|
processErrorsRegister.Incr(1)
|
|
|
|
})
|
2019-08-21 23:49:07 +00:00
|
|
|
setLogIfExist(processor, logger)
|
|
|
|
|
|
|
|
return &RunningProcessor{
|
|
|
|
Processor: processor,
|
|
|
|
Config: config,
|
|
|
|
log: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (rp *RunningProcessor) metricFiltered(metric telegraf.Metric) {
|
|
|
|
metric.Drop()
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningProcessor) Init() error {
|
|
|
|
if p, ok := r.Processor.(telegraf.Initializer); ok {
|
2019-06-14 22:12:27 +00:00
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func (r *RunningProcessor) Log() telegraf.Logger {
|
|
|
|
return r.log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningProcessor) LogName() string {
|
|
|
|
return logName("processors", r.Config.Name, r.Config.Alias)
|
|
|
|
}
|
2017-07-13 22:34:21 +00:00
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func (r *RunningProcessor) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
|
|
|
return metric
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func (r *RunningProcessor) Start(acc telegraf.Accumulator) error {
|
|
|
|
return r.Processor.Start(acc)
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func (r *RunningProcessor) Add(m telegraf.Metric, acc telegraf.Accumulator) {
|
|
|
|
if ok := r.Config.Filter.Select(m); !ok {
|
|
|
|
// pass downstream
|
|
|
|
acc.AddMetric(m)
|
|
|
|
return
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
r.Config.Filter.Modify(m)
|
|
|
|
if len(m.FieldList()) == 0 {
|
|
|
|
// drop metric
|
|
|
|
r.metricFiltered(m)
|
|
|
|
return
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
r.Processor.Add(m, acc)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2020-02-25 18:40:29 +00:00
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
func (r *RunningProcessor) Stop() {
|
|
|
|
r.Processor.Stop()
|
2020-02-25 18:40:29 +00:00
|
|
|
}
|