2016-01-27 21:21:36 +00:00
|
|
|
package internal_models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"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-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
|
|
|
|
Quiet bool
|
|
|
|
MetricBufferLimit int
|
|
|
|
MetricBatchSize int
|
|
|
|
|
|
|
|
metrics *buffer.Buffer
|
|
|
|
failMetrics *buffer.Buffer
|
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-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.
|
|
|
|
func (ro *RunningOutput) AddMetric(metric telegraf.Metric) {
|
2016-01-22 18:54:12 +00:00
|
|
|
if ro.Config.Filter.IsActive {
|
2016-02-16 00:21:38 +00:00
|
|
|
if !ro.Config.Filter.ShouldMetricPass(metric) {
|
2016-01-22 18:54:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 10:43:54 +00:00
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
// Filter any tagexclude/taginclude parameters before adding metric
|
|
|
|
if len(ro.Config.Filter.TagExclude) != 0 || len(ro.Config.Filter.TagInclude) != 0 {
|
|
|
|
// In order to filter out tags, we need to create a new metric, since
|
|
|
|
// metrics are immutable once created.
|
|
|
|
tags := metric.Tags()
|
|
|
|
fields := metric.Fields()
|
|
|
|
t := metric.Time()
|
|
|
|
name := metric.Name()
|
|
|
|
ro.Config.Filter.FilterTags(tags)
|
|
|
|
// error is not possible if creating from another metric, so ignore.
|
|
|
|
metric, _ = telegraf.NewMetric(name, tags, fields, t)
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
ro.metrics.Add(metric)
|
|
|
|
if ro.metrics.Len() == ro.MetricBatchSize {
|
|
|
|
batch := ro.metrics.Batch(ro.MetricBatchSize)
|
|
|
|
err := ro.write(batch)
|
|
|
|
if err != nil {
|
|
|
|
ro.failMetrics.Add(batch...)
|
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 {
|
2016-04-25 23:49:06 +00:00
|
|
|
if !ro.Quiet {
|
|
|
|
log.Printf("Output [%s] buffer fullness: %d / %d metrics. "+
|
|
|
|
"Total gathered metrics: %d. Total dropped metrics: %d.",
|
|
|
|
ro.Name,
|
|
|
|
ro.failMetrics.Len()+ro.metrics.Len(),
|
|
|
|
ro.MetricBufferLimit,
|
|
|
|
ro.metrics.Total(),
|
|
|
|
ro.metrics.Drops()+ro.failMetrics.Drops())
|
2016-04-26 16:25:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:49:06 +00:00
|
|
|
var err error
|
|
|
|
if !ro.failMetrics.IsEmpty() {
|
|
|
|
bufLen := ro.failMetrics.Len()
|
|
|
|
// how many batches of failed writes we need to write.
|
|
|
|
nBatches := bufLen/ro.MetricBatchSize + 1
|
|
|
|
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 {
|
|
|
|
batchSize = bufLen % ro.MetricBatchSize
|
|
|
|
}
|
|
|
|
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-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-03-18 16:51:14 +00:00
|
|
|
if len(metrics) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
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 {
|
|
|
|
if !ro.Quiet {
|
2016-04-25 23:49:06 +00:00
|
|
|
log.Printf("Output [%s] wrote batch of %d metrics in %s\n",
|
|
|
|
ro.Name, len(metrics), elapsed)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputConfig containing name and filter
|
|
|
|
type OutputConfig struct {
|
|
|
|
Name string
|
|
|
|
Filter Filter
|
|
|
|
}
|