2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
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
|
|
|
|
Filter Filter
|
|
|
|
|
|
|
|
FlushInterval time.Duration
|
|
|
|
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
|
|
|
Name string
|
|
|
|
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
|
|
|
|
|
|
|
|
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,
|
2016-01-22 18:54:12 +00:00
|
|
|
conf *OutputConfig,
|
2016-04-25 23:49:06 +00:00
|
|
|
batchSize int,
|
|
|
|
bufferLimit int,
|
2016-01-22 18:54:12 +00:00
|
|
|
) *RunningOutput {
|
2018-11-05 21:34:28 +00:00
|
|
|
if conf.MetricBufferLimit > 0 {
|
|
|
|
bufferLimit = conf.MetricBufferLimit
|
|
|
|
}
|
2016-04-25 23:49:06 +00:00
|
|
|
if bufferLimit == 0 {
|
|
|
|
bufferLimit = DEFAULT_METRIC_BUFFER_LIMIT
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
if conf.MetricBatchSize > 0 {
|
|
|
|
batchSize = conf.MetricBatchSize
|
|
|
|
}
|
2016-04-25 23:49:06 +00:00
|
|
|
if batchSize == 0 {
|
|
|
|
batchSize = DEFAULT_METRIC_BATCH_SIZE
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
ro := &RunningOutput{
|
2016-02-16 00:21:38 +00:00
|
|
|
Name: name,
|
2018-11-05 21:34:28 +00:00
|
|
|
buffer: NewBuffer(name, bufferLimit),
|
|
|
|
BatchReady: make(chan time.Time, 1),
|
2016-02-16 00:21:38 +00:00
|
|
|
Output: output,
|
|
|
|
Config: conf,
|
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",
|
|
|
|
map[string]string{"output": name},
|
|
|
|
),
|
|
|
|
WriteTime: selfstat.RegisterTiming(
|
|
|
|
"write",
|
|
|
|
"write_time_ns",
|
|
|
|
map[string]string{"output": name},
|
|
|
|
),
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) {
|
|
|
|
ro.MetricsFiltered.Incr(1)
|
|
|
|
metric.Drop()
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:12:27 +00:00
|
|
|
func (ro *RunningOutput) Init() error {
|
|
|
|
if p, ok := ro.Output.(telegraf.Initializer); ok {
|
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:30 +00:00
|
|
|
func (ro *RunningOutput) Close() {
|
|
|
|
err := ro.Output.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! [outputs.%s] Error closing output: %v", ro.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
|
2019-06-05 19:34:45 +00:00
|
|
|
dropped := atomic.LoadInt64(&ro.droppedMetrics)
|
|
|
|
if dropped > 0 {
|
|
|
|
log.Printf("W! [outputs.%s] Metric buffer overflow; %d metrics have been dropped",
|
|
|
|
ro.Name, dropped)
|
|
|
|
atomic.StoreInt64(&ro.droppedMetrics, 0)
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
start := time.Now()
|
2016-02-16 00:21:38 +00:00
|
|
|
err := ro.Output.Write(metrics)
|
2016-01-22 18:54:12 +00:00
|
|
|
elapsed := time.Since(start)
|
2018-11-05 21:34:28 +00:00
|
|
|
ro.WriteTime.Incr(elapsed.Nanoseconds())
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
if err == nil {
|
2018-11-05 21:34:28 +00:00
|
|
|
log.Printf("D! [outputs.%s] wrote batch of %d metrics in %s\n",
|
|
|
|
ro.Name, len(metrics), elapsed)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
func (ro *RunningOutput) LogBufferStatus() {
|
|
|
|
nBuffer := ro.buffer.Len()
|
|
|
|
log.Printf("D! [outputs.%s] buffer fullness: %d / %d metrics. ",
|
|
|
|
ro.Name, nBuffer, ro.MetricBufferLimit)
|
|
|
|
}
|