2016-01-27 21:21:36 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-11-07 08:34:46 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
NErrors = selfstat.Register("agent", "gather_errors", map[string]string{})
|
2016-01-27 21:21:36 +00:00
|
|
|
)
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
type MetricMaker interface {
|
2019-08-21 23:49:07 +00:00
|
|
|
LogName() string
|
2018-09-28 21:48:20 +00:00
|
|
|
MakeMetric(metric telegraf.Metric) telegraf.Metric
|
|
|
|
}
|
|
|
|
|
|
|
|
type accumulator struct {
|
|
|
|
maker MetricMaker
|
2018-11-05 21:34:28 +00:00
|
|
|
metrics chan<- telegraf.Metric
|
2018-09-28 21:48:20 +00:00
|
|
|
precision time.Duration
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func NewAccumulator(
|
2016-09-08 14:22:10 +00:00
|
|
|
maker MetricMaker,
|
2018-11-05 21:34:28 +00:00
|
|
|
metrics chan<- telegraf.Metric,
|
2018-01-26 19:39:58 +00:00
|
|
|
) telegraf.Accumulator {
|
2016-09-08 14:22:10 +00:00
|
|
|
acc := accumulator{
|
|
|
|
maker: maker,
|
|
|
|
metrics: metrics,
|
|
|
|
precision: time.Nanosecond,
|
|
|
|
}
|
2016-01-27 21:21:36 +00:00
|
|
|
return &acc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accumulator) AddFields(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2018-11-05 21:34:28 +00:00
|
|
|
ac.addFields(measurement, tags, fields, telegraf.Untyped, t...)
|
2016-08-31 16:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accumulator) AddGauge(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2018-11-05 21:34:28 +00:00
|
|
|
ac.addFields(measurement, tags, fields, telegraf.Gauge, t...)
|
2016-08-31 16:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accumulator) AddCounter(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2018-11-05 21:34:28 +00:00
|
|
|
ac.addFields(measurement, tags, fields, telegraf.Counter, t...)
|
2016-10-03 17:49:30 +00:00
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2017-10-24 23:28:52 +00:00
|
|
|
func (ac *accumulator) AddSummary(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2018-11-05 21:34:28 +00:00
|
|
|
ac.addFields(measurement, tags, fields, telegraf.Summary, t...)
|
2017-10-24 23:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accumulator) AddHistogram(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2018-11-05 21:34:28 +00:00
|
|
|
ac.addFields(measurement, tags, fields, telegraf.Histogram, t...)
|
2018-09-28 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (ac *accumulator) AddMetric(m telegraf.Metric) {
|
2018-12-12 03:11:33 +00:00
|
|
|
m.SetTime(m.Time().Round(ac.precision))
|
2018-11-05 21:34:28 +00:00
|
|
|
if m := ac.maker.MakeMetric(m); m != nil {
|
|
|
|
ac.metrics <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accumulator) addFields(
|
2018-09-28 21:48:20 +00:00
|
|
|
measurement string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tp telegraf.ValueType,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
|
|
|
m, err := metric.New(measurement, tags, fields, ac.getTime(t), tp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m := ac.maker.MakeMetric(m); m != nil {
|
2017-10-24 23:28:52 +00:00
|
|
|
ac.metrics <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 12:09:49 +00:00
|
|
|
// AddError passes a runtime error to the accumulator.
|
|
|
|
// The error will be tagged with the plugin name and written to the log.
|
|
|
|
func (ac *accumulator) AddError(err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-11-07 08:34:46 +00:00
|
|
|
NErrors.Incr(1)
|
2019-08-21 23:49:07 +00:00
|
|
|
log.Printf("E! [%s] Error in plugin: %v", ac.maker.LogName(), err)
|
2016-05-19 15:36:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 22:40:33 +00:00
|
|
|
func (ac *accumulator) SetPrecision(precision time.Duration) {
|
|
|
|
ac.precision = precision
|
2016-06-13 14:21:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (ac *accumulator) getTime(t []time.Time) time.Time {
|
2016-09-08 14:22:10 +00:00
|
|
|
var timestamp time.Time
|
|
|
|
if len(t) > 0 {
|
|
|
|
timestamp = t[0]
|
|
|
|
} else {
|
|
|
|
timestamp = time.Now()
|
2016-10-03 17:49:30 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
return timestamp.Round(ac.precision)
|
2016-01-27 21:21:36 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
func (ac *accumulator) WithTracking(maxTracked int) telegraf.TrackingAccumulator {
|
|
|
|
return &trackingAccumulator{
|
|
|
|
Accumulator: ac,
|
|
|
|
delivered: make(chan telegraf.DeliveryInfo, maxTracked),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type trackingAccumulator struct {
|
|
|
|
telegraf.Accumulator
|
|
|
|
delivered chan telegraf.DeliveryInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *trackingAccumulator) AddTrackingMetric(m telegraf.Metric) telegraf.TrackingID {
|
|
|
|
dm, id := metric.WithTracking(m, a.onDelivery)
|
|
|
|
a.AddMetric(dm)
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *trackingAccumulator) AddTrackingMetricGroup(group []telegraf.Metric) telegraf.TrackingID {
|
|
|
|
db, id := metric.WithGroupTracking(group, a.onDelivery)
|
|
|
|
for _, m := range db {
|
|
|
|
a.AddMetric(m)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *trackingAccumulator) Delivered() <-chan telegraf.DeliveryInfo {
|
|
|
|
return a.delivered
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *trackingAccumulator) onDelivery(info telegraf.DeliveryInfo) {
|
|
|
|
select {
|
|
|
|
case a.delivered <- info:
|
|
|
|
default:
|
|
|
|
// This is a programming error in the input. More items were sent for
|
|
|
|
// tracking than space requested.
|
|
|
|
panic("channel is full")
|
|
|
|
}
|
|
|
|
}
|