2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-07 08:34:46 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
2020-02-25 18:40:29 +00:00
|
|
|
var (
|
|
|
|
GlobalMetricsGathered = selfstat.Register("agent", "metrics_gathered", map[string]string{})
|
|
|
|
GlobalGatherErrors = selfstat.Register("agent", "gather_errors", map[string]string{})
|
|
|
|
)
|
2016-11-07 08:34:46 +00:00
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
type RunningInput struct {
|
2016-01-27 21:21:36 +00:00
|
|
|
Input telegraf.Input
|
2016-01-22 18:54:12 +00:00
|
|
|
Config *InputConfig
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
log telegraf.Logger
|
2016-09-08 14:22:10 +00:00
|
|
|
defaultTags map[string]string
|
2016-11-07 08:34:46 +00:00
|
|
|
|
|
|
|
MetricsGathered selfstat.Stat
|
2018-11-05 21:34:28 +00:00
|
|
|
GatherTime selfstat.Stat
|
2016-11-07 08:34:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput {
|
2019-10-23 19:40:31 +00:00
|
|
|
tags := map[string]string{"input": config.Name}
|
|
|
|
if config.Alias != "" {
|
|
|
|
tags["alias"] = config.Alias
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:40:29 +00:00
|
|
|
inputErrorsRegister := selfstat.Register("gather", "errors", tags)
|
|
|
|
logger := NewLogger("inputs", config.Name, config.Alias)
|
|
|
|
logger.OnErr(func() {
|
|
|
|
inputErrorsRegister.Incr(1)
|
|
|
|
GlobalGatherErrors.Incr(1)
|
|
|
|
})
|
2019-08-21 23:49:07 +00:00
|
|
|
setLogIfExist(input, logger)
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
return &RunningInput{
|
|
|
|
Input: input,
|
|
|
|
Config: config,
|
|
|
|
MetricsGathered: selfstat.Register(
|
|
|
|
"gather",
|
|
|
|
"metrics_gathered",
|
2019-10-23 19:40:31 +00:00
|
|
|
tags,
|
2016-11-07 08:34:46 +00:00
|
|
|
),
|
2018-11-05 21:34:28 +00:00
|
|
|
GatherTime: selfstat.RegisterTiming(
|
|
|
|
"gather",
|
|
|
|
"gather_time_ns",
|
2019-10-23 19:40:31 +00:00
|
|
|
tags,
|
2018-11-05 21:34:28 +00:00
|
|
|
),
|
2019-08-21 23:49:07 +00:00
|
|
|
log: logger,
|
2016-11-07 08:34:46 +00:00
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// InputConfig is the common config for all inputs.
|
2016-01-22 18:54:12 +00:00
|
|
|
type InputConfig struct {
|
2018-09-28 21:48:20 +00:00
|
|
|
Name string
|
2019-08-21 23:49:07 +00:00
|
|
|
Alias string
|
2018-09-28 21:48:20 +00:00
|
|
|
Interval time.Duration
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
NameOverride string
|
|
|
|
MeasurementPrefix string
|
|
|
|
MeasurementSuffix string
|
|
|
|
Tags map[string]string
|
|
|
|
Filter Filter
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningInput) metricFiltered(metric telegraf.Metric) {
|
|
|
|
metric.Drop()
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningInput) LogName() string {
|
|
|
|
return logName("inputs", r.Config.Name, r.Config.Alias)
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:12:27 +00:00
|
|
|
func (r *RunningInput) Init() error {
|
|
|
|
if p, ok := r.Input.(telegraf.Initializer); ok {
|
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (r *RunningInput) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
|
|
|
if ok := r.Config.Filter.Select(metric); !ok {
|
2018-11-05 21:34:28 +00:00
|
|
|
r.metricFiltered(metric)
|
2018-09-28 21:48:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
r.defaultTags)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-12-18 22:20:43 +00:00
|
|
|
r.Config.Filter.Modify(metric)
|
|
|
|
if len(metric.FieldList()) == 0 {
|
|
|
|
r.metricFiltered(metric)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
r.MetricsGathered.Incr(1)
|
|
|
|
GlobalMetricsGathered.Incr(1)
|
2016-09-08 14:22:10 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (r *RunningInput) Gather(acc telegraf.Accumulator) error {
|
|
|
|
start := time.Now()
|
|
|
|
err := r.Input.Gather(acc)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
r.GatherTime.Incr(elapsed.Nanoseconds())
|
|
|
|
return err
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningInput) SetDefaultTags(tags map[string]string) {
|
|
|
|
r.defaultTags = tags
|
|
|
|
}
|
2020-02-25 18:40:29 +00:00
|
|
|
|
|
|
|
func (r *RunningInput) Log() telegraf.Logger {
|
|
|
|
return r.log
|
|
|
|
}
|