2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
2017-07-12 19:03:23 +00:00
|
|
|
"sync"
|
2019-01-22 21:43:51 +00:00
|
|
|
"sync/atomic"
|
2016-01-22 18:54:12 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
const (
|
2016-04-24 10:43:54 +00:00
|
|
|
// Default size of metrics batch size.
|
|
|
|
DEFAULT_METRIC_BATCH_SIZE = 1000
|
|
|
|
|
|
|
|
// Default number of metrics kept. It should be a multiple of batch size.
|
|
|
|
DEFAULT_METRIC_BUFFER_LIMIT = 10000
|
2016-02-16 00:21:38 +00:00
|
|
|
)
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
// OutputConfig containing name and filter
|
|
|
|
type OutputConfig struct {
|
|
|
|
Name string
|
2019-08-21 23:49:07 +00:00
|
|
|
Alias string
|
2018-11-05 21:34:28 +00:00
|
|
|
Filter Filter
|
|
|
|
|
|
|
|
FlushInterval time.Duration
|
2019-11-13 00:43:39 +00:00
|
|
|
FlushJitter *time.Duration
|
2018-11-05 21:34:28 +00:00
|
|
|
MetricBufferLimit int
|
|
|
|
MetricBatchSize int
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
// RunningOutput contains the output configuration
|
2016-01-22 18:54:12 +00:00
|
|
|
type RunningOutput struct {
|
2019-01-22 21:43:51 +00:00
|
|
|
// Must be 64-bit aligned
|
|
|
|
newMetricsCount int64
|
2019-06-05 19:34:45 +00:00
|
|
|
droppedMetrics int64
|
2019-01-22 21:43:51 +00:00
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
Output telegraf.Output
|
|
|
|
Config *OutputConfig
|
|
|
|
MetricBufferLimit int
|
|
|
|
MetricBatchSize int
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
MetricsFiltered selfstat.Stat
|
|
|
|
WriteTime selfstat.Stat
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
BatchReady chan time.Time
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2019-01-22 21:43:51 +00:00
|
|
|
buffer *Buffer
|
2019-08-21 23:49:07 +00:00
|
|
|
log telegraf.Logger
|
2019-01-22 21:43:51 +00:00
|
|
|
|
|
|
|
aggMutex sync.Mutex
|
2018-09-28 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
func NewRunningOutput(
|
|
|
|
name string,
|
2016-01-27 21:21:36 +00:00
|
|
|
output telegraf.Output,
|
2019-08-21 23:49:07 +00:00
|
|
|
config *OutputConfig,
|
2016-04-25 23:49:06 +00:00
|
|
|
batchSize int,
|
|
|
|
bufferLimit int,
|
2016-01-22 18:54:12 +00:00
|
|
|
) *RunningOutput {
|
2019-10-23 19:40:31 +00:00
|
|
|
tags := map[string]string{"output": config.Name}
|
|
|
|
if config.Alias != "" {
|
|
|
|
tags["alias"] = config.Alias
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
logger := &Logger{
|
|
|
|
Name: logName("outputs", config.Name, config.Alias),
|
2019-10-23 19:40:31 +00:00
|
|
|
Errs: selfstat.Register("write", "errors", tags),
|
2019-08-21 23:49:07 +00:00
|
|
|
}
|
|
|
|
setLogIfExist(output, logger)
|
|
|
|
|
|
|
|
if config.MetricBufferLimit > 0 {
|
|
|
|
bufferLimit = config.MetricBufferLimit
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
2016-04-25 23:49:06 +00:00
|
|
|
if bufferLimit == 0 {
|
|
|
|
bufferLimit = DEFAULT_METRIC_BUFFER_LIMIT
|
|
|
|
}
|
2019-08-21 23:49:07 +00:00
|
|
|
if config.MetricBatchSize > 0 {
|
|
|
|
batchSize = config.MetricBatchSize
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
2016-04-25 23:49:06 +00:00
|
|
|
if batchSize == 0 {
|
|
|
|
batchSize = DEFAULT_METRIC_BATCH_SIZE
|
|
|
|
}
|
2019-08-21 23:49:07 +00:00
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
ro := &RunningOutput{
|
2019-08-22 01:02:51 +00:00
|
|
|
buffer: NewBuffer(config.Name, config.Alias, bufferLimit),
|
2018-11-05 21:34:28 +00:00
|
|
|
BatchReady: make(chan time.Time, 1),
|
2016-02-16 00:21:38 +00:00
|
|
|
Output: output,
|
2019-08-21 23:49:07 +00:00
|
|
|
Config: config,
|
2016-04-25 23:49:06 +00:00
|
|
|
MetricBufferLimit: bufferLimit,
|
|
|
|
MetricBatchSize: batchSize,
|
2016-11-07 08:34:46 +00:00
|
|
|
MetricsFiltered: selfstat.Register(
|
|
|
|
"write",
|
|
|
|
"metrics_filtered",
|
2019-10-23 19:40:31 +00:00
|
|
|
tags,
|
2016-11-07 08:34:46 +00:00
|
|
|
),
|
|
|
|
WriteTime: selfstat.RegisterTiming(
|
|
|
|
"write",
|
|
|
|
"write_time_ns",
|
2019-10-23 19:40:31 +00:00
|
|
|
tags,
|
2016-11-07 08:34:46 +00:00
|
|
|
),
|
2019-08-21 23:49:07 +00:00
|
|
|
log: logger,
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
return ro
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningOutput) LogName() string {
|
|
|
|
return logName("outputs", r.Config.Name, r.Config.Alias)
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) {
|
|
|
|
ro.MetricsFiltered.Incr(1)
|
|
|
|
metric.Drop()
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningOutput) Init() error {
|
|
|
|
if p, ok := r.Output.(telegraf.Initializer); ok {
|
2019-06-14 22:12:27 +00:00
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-21 23:49:07 +00:00
|
|
|
|
2019-06-14 22:12:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
// AddMetric adds a metric to the output.
|
|
|
|
//
|
|
|
|
// Takes ownership of metric
|
2018-09-28 21:48:20 +00:00
|
|
|
func (ro *RunningOutput) AddMetric(metric telegraf.Metric) {
|
|
|
|
if ok := ro.Config.Filter.Select(metric); !ok {
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.metricFiltered(metric)
|
2017-02-01 08:55:22 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
|
|
|
ro.Config.Filter.Modify(metric)
|
|
|
|
if len(metric.FieldList()) == 0 {
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.metricFiltered(metric)
|
2018-09-28 21:48:20 +00:00
|
|
|
return
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 21:50:32 +00:00
|
|
|
if output, ok := ro.Output.(telegraf.AggregatingOutput); ok {
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.aggMutex.Lock()
|
2018-09-28 21:48:20 +00:00
|
|
|
output.Add(metric)
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.aggMutex.Unlock()
|
2018-09-05 21:50:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:34:45 +00:00
|
|
|
dropped := ro.buffer.Add(metric)
|
|
|
|
atomic.AddInt64(&ro.droppedMetrics, int64(dropped))
|
2018-11-05 21:34:28 +00:00
|
|
|
|
2019-01-22 21:43:51 +00:00
|
|
|
count := atomic.AddInt64(&ro.newMetricsCount, 1)
|
|
|
|
if count == int64(ro.MetricBatchSize) {
|
|
|
|
atomic.StoreInt64(&ro.newMetricsCount, 0)
|
2018-11-05 21:34:28 +00:00
|
|
|
select {
|
|
|
|
case ro.BatchReady <- time.Now():
|
|
|
|
default:
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
// Write writes all metrics to the output, stopping when all have been sent on
|
|
|
|
// or error.
|
2016-01-22 18:54:12 +00:00
|
|
|
func (ro *RunningOutput) Write() error {
|
2018-09-05 21:50:32 +00:00
|
|
|
if output, ok := ro.Output.(telegraf.AggregatingOutput); ok {
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.aggMutex.Lock()
|
2018-09-05 21:50:32 +00:00
|
|
|
metrics := output.Push()
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.buffer.Add(metrics...)
|
2018-09-05 21:50:32 +00:00
|
|
|
output.Reset()
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.aggMutex.Unlock()
|
2018-09-05 21:50:32 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
2019-01-22 21:43:51 +00:00
|
|
|
atomic.StoreInt64(&ro.newMetricsCount, 0)
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
// Only process the metrics in the buffer now. Metrics added while we are
|
|
|
|
// writing will be sent on the next call.
|
2019-01-22 21:43:51 +00:00
|
|
|
nBuffer := ro.buffer.Len()
|
2018-11-05 21:34:28 +00:00
|
|
|
nBatches := nBuffer/ro.MetricBatchSize + 1
|
|
|
|
for i := 0; i < nBatches; i++ {
|
|
|
|
batch := ro.buffer.Batch(ro.MetricBatchSize)
|
|
|
|
if len(batch) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2018-09-05 21:50:32 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
err := ro.write(batch)
|
|
|
|
if err != nil {
|
|
|
|
ro.buffer.Reject(batch)
|
|
|
|
return err
|
2016-04-24 10:43:54 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.buffer.Accept(batch)
|
2016-04-24 10:43:54 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-24 10:43:54 +00:00
|
|
|
|
2019-01-22 21:43:51 +00:00
|
|
|
// WriteBatch writes a single batch of metrics to the output.
|
2018-11-05 21:34:28 +00:00
|
|
|
func (ro *RunningOutput) WriteBatch() error {
|
|
|
|
batch := ro.buffer.Batch(ro.MetricBatchSize)
|
|
|
|
if len(batch) == 0 {
|
|
|
|
return nil
|
2016-04-25 23:49:06 +00:00
|
|
|
}
|
2016-11-07 08:34:46 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
err := ro.write(batch)
|
2016-02-16 00:21:38 +00:00
|
|
|
if err != nil {
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.buffer.Reject(batch)
|
2016-02-16 00:21:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.buffer.Accept(batch)
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 02:46:08 +00:00
|
|
|
// Close closes the output
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningOutput) Close() {
|
|
|
|
err := r.Output.Close()
|
2019-03-22 20:59:30 +00:00
|
|
|
if err != nil {
|
2019-08-21 23:49:07 +00:00
|
|
|
r.log.Errorf("Error closing output: %v", err)
|
2019-03-22 20:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningOutput) write(metrics []telegraf.Metric) error {
|
|
|
|
dropped := atomic.LoadInt64(&r.droppedMetrics)
|
2019-06-05 19:34:45 +00:00
|
|
|
if dropped > 0 {
|
2019-08-21 23:49:07 +00:00
|
|
|
r.log.Warnf("Metric buffer overflow; %d metrics have been dropped", dropped)
|
|
|
|
atomic.StoreInt64(&r.droppedMetrics, 0)
|
2019-06-05 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
start := time.Now()
|
2019-08-21 23:49:07 +00:00
|
|
|
err := r.Output.Write(metrics)
|
2016-01-22 18:54:12 +00:00
|
|
|
elapsed := time.Since(start)
|
2019-08-21 23:49:07 +00:00
|
|
|
r.WriteTime.Incr(elapsed.Nanoseconds())
|
2018-11-05 21:34:28 +00:00
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
if err == nil {
|
2019-08-21 23:49:07 +00:00
|
|
|
r.log.Debugf("Wrote batch of %d metrics in %s", len(metrics), elapsed)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (r *RunningOutput) LogBufferStatus() {
|
|
|
|
nBuffer := r.buffer.Len()
|
|
|
|
r.log.Debugf("Buffer fullness: %d / %d metrics", nBuffer, r.MetricBufferLimit)
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|