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"
|
2016-01-22 18:54:12 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-04-25 23:49:06 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/buffer"
|
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
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
// RunningOutput contains the output configuration
|
2016-01-22 18:54:12 +00:00
|
|
|
type RunningOutput struct {
|
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
|
|
|
|
MetricsWritten selfstat.Stat
|
|
|
|
BufferSize selfstat.Stat
|
|
|
|
BufferLimit selfstat.Stat
|
|
|
|
WriteTime selfstat.Stat
|
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
metrics *buffer.Buffer
|
|
|
|
failMetrics *buffer.Buffer
|
2017-07-12 19:03:23 +00:00
|
|
|
|
2018-09-12 07:23:50 +00:00
|
|
|
// Guards against concurrent calls to Add, Push, Reset
|
|
|
|
aggMutex sync.Mutex
|
2017-07-12 19:03:23 +00:00
|
|
|
// Guards against concurrent calls to the Output as described in #3009
|
2018-09-12 07:23:50 +00:00
|
|
|
writeMutex sync.Mutex
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// OutputConfig containing name and filter
|
|
|
|
type OutputConfig struct {
|
|
|
|
Name string
|
|
|
|
Filter Filter
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-04-25 23:49:06 +00:00
|
|
|
if bufferLimit == 0 {
|
|
|
|
bufferLimit = DEFAULT_METRIC_BUFFER_LIMIT
|
|
|
|
}
|
|
|
|
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,
|
2016-04-25 23:49:06 +00:00
|
|
|
metrics: buffer.NewBuffer(batchSize),
|
|
|
|
failMetrics: buffer.NewBuffer(bufferLimit),
|
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
|
|
|
MetricsWritten: selfstat.Register(
|
|
|
|
"write",
|
|
|
|
"metrics_written",
|
|
|
|
map[string]string{"output": name},
|
|
|
|
),
|
|
|
|
MetricsFiltered: selfstat.Register(
|
|
|
|
"write",
|
|
|
|
"metrics_filtered",
|
|
|
|
map[string]string{"output": name},
|
|
|
|
),
|
|
|
|
BufferSize: selfstat.Register(
|
|
|
|
"write",
|
|
|
|
"buffer_size",
|
|
|
|
map[string]string{"output": name},
|
|
|
|
),
|
|
|
|
BufferLimit: selfstat.Register(
|
|
|
|
"write",
|
|
|
|
"buffer_limit",
|
|
|
|
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-02-20 04:54:02 +00:00
|
|
|
ro.BufferLimit.Set(int64(ro.MetricBufferLimit))
|
2016-01-22 18:54:12 +00:00
|
|
|
return ro
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
// AddMetric adds a metric to the output. This function can also write cached
|
|
|
|
// points if FlushBufferWhenFull is true.
|
2018-09-28 21:48:20 +00:00
|
|
|
func (ro *RunningOutput) AddMetric(metric telegraf.Metric) {
|
|
|
|
if ok := ro.Config.Filter.Select(metric); !ok {
|
|
|
|
ro.MetricsFiltered.Incr(1)
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
ro.metrics.Add(metric)
|
2016-04-25 23:49:06 +00:00
|
|
|
if ro.metrics.Len() == ro.MetricBatchSize {
|
|
|
|
batch := ro.metrics.Batch(ro.MetricBatchSize)
|
|
|
|
err := ro.write(batch)
|
|
|
|
if err != nil {
|
|
|
|
ro.failMetrics.Add(batch...)
|
2018-08-17 20:51:21 +00:00
|
|
|
log.Printf("E! Error writing to output [%s]: %v", ro.Name, err)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
// Write writes all cached points to this output.
|
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()
|
|
|
|
ro.metrics.Add(metrics...)
|
|
|
|
output.Reset()
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.aggMutex.Unlock()
|
2018-09-05 21:50:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
nFails, nMetrics := ro.failMetrics.Len(), ro.metrics.Len()
|
2017-02-16 19:37:55 +00:00
|
|
|
ro.BufferSize.Set(int64(nFails + nMetrics))
|
2016-11-07 08:34:46 +00:00
|
|
|
log.Printf("D! Output [%s] buffer fullness: %d / %d metrics. ",
|
|
|
|
ro.Name, nFails+nMetrics, ro.MetricBufferLimit)
|
2016-04-25 23:49:06 +00:00
|
|
|
var err error
|
|
|
|
if !ro.failMetrics.IsEmpty() {
|
|
|
|
// how many batches of failed writes we need to write.
|
2016-11-07 08:34:46 +00:00
|
|
|
nBatches := nFails/ro.MetricBatchSize + 1
|
2016-04-25 23:49:06 +00:00
|
|
|
batchSize := ro.MetricBatchSize
|
|
|
|
|
|
|
|
for i := 0; i < nBatches; i++ {
|
|
|
|
// If it's the last batch, only grab the metrics that have not had
|
|
|
|
// a write attempt already (this is primarily to preserve order).
|
|
|
|
if i == nBatches-1 {
|
2016-11-07 08:34:46 +00:00
|
|
|
batchSize = nFails % ro.MetricBatchSize
|
2016-04-25 23:49:06 +00:00
|
|
|
}
|
|
|
|
batch := ro.failMetrics.Batch(batchSize)
|
|
|
|
// If we've already failed previous writes, don't bother trying to
|
|
|
|
// write to this output again. We are not exiting the loop just so
|
|
|
|
// that we can rotate the metrics to preserve order.
|
|
|
|
if err == nil {
|
|
|
|
err = ro.write(batch)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ro.failMetrics.Add(batch...)
|
|
|
|
}
|
2016-04-24 10:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
batch := ro.metrics.Batch(ro.MetricBatchSize)
|
|
|
|
// see comment above about not trying to write to an already failed output.
|
|
|
|
// if ro.failMetrics is empty then err will always be nil at this point.
|
|
|
|
if err == nil {
|
|
|
|
err = ro.write(batch)
|
|
|
|
}
|
2016-11-07 08:34:46 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
if err != nil {
|
2016-04-25 23:49:06 +00:00
|
|
|
ro.failMetrics.Add(batch...)
|
2016-02-16 00:21:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
|
2016-11-07 08:34:46 +00:00
|
|
|
nMetrics := len(metrics)
|
|
|
|
if nMetrics == 0 {
|
2016-03-18 16:51:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-12 07:23:50 +00:00
|
|
|
ro.writeMutex.Lock()
|
|
|
|
defer ro.writeMutex.Unlock()
|
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)
|
|
|
|
if err == nil {
|
2016-11-07 08:34:46 +00:00
|
|
|
log.Printf("D! Output [%s] wrote batch of %d metrics in %s\n",
|
|
|
|
ro.Name, nMetrics, elapsed)
|
|
|
|
ro.MetricsWritten.Incr(int64(nMetrics))
|
|
|
|
ro.WriteTime.Incr(elapsed.Nanoseconds())
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|