2016-01-27 21:21:36 +00:00
|
|
|
package internal_models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-02-16 00:21:38 +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-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-24 10:43:54 +00:00
|
|
|
// tmpmetrics point to batch of metrics ready to be wrote to output.
|
|
|
|
// readI point to the oldest batch of metrics (the first to sent to output). It
|
|
|
|
// may point to nil value if tmpmetrics is empty.
|
|
|
|
// writeI point to the next slot to buffer a batch of metrics is output fail to
|
|
|
|
// write.
|
2016-01-22 18:54:12 +00:00
|
|
|
type RunningOutput struct {
|
2016-02-16 00:21:38 +00:00
|
|
|
Name string
|
|
|
|
Output telegraf.Output
|
|
|
|
Config *OutputConfig
|
|
|
|
Quiet bool
|
|
|
|
MetricBufferLimit int
|
2016-04-24 10:43:54 +00:00
|
|
|
MetricBatchSize int
|
2016-02-16 00:21:38 +00:00
|
|
|
FlushBufferWhenFull bool
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
metrics []telegraf.Metric
|
2016-04-24 10:43:54 +00:00
|
|
|
tmpmetrics []([]telegraf.Metric)
|
|
|
|
writeI int
|
|
|
|
readI int
|
2016-02-16 00:21:38 +00:00
|
|
|
|
|
|
|
sync.Mutex
|
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,
|
|
|
|
) *RunningOutput {
|
|
|
|
ro := &RunningOutput{
|
2016-02-16 00:21:38 +00:00
|
|
|
Name: name,
|
|
|
|
metrics: make([]telegraf.Metric, 0),
|
|
|
|
Output: output,
|
|
|
|
Config: conf,
|
|
|
|
MetricBufferLimit: DEFAULT_METRIC_BUFFER_LIMIT,
|
2016-04-24 10:43:54 +00:00
|
|
|
MetricBatchSize: DEFAULT_METRIC_BATCH_SIZE,
|
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-02-16 00:21:38 +00:00
|
|
|
ro.Lock()
|
|
|
|
defer ro.Unlock()
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2016-04-24 10:43:54 +00:00
|
|
|
if ro.tmpmetrics == nil {
|
|
|
|
size := ro.MetricBufferLimit / ro.MetricBatchSize
|
|
|
|
// ro.metrics already contains one batch
|
|
|
|
size = size - 1
|
|
|
|
|
|
|
|
if size < 1 {
|
|
|
|
size = 1
|
|
|
|
}
|
|
|
|
ro.tmpmetrics = make([]([]telegraf.Metric), size)
|
|
|
|
}
|
|
|
|
|
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-24 10:43:54 +00:00
|
|
|
if len(ro.metrics) < ro.MetricBatchSize {
|
2016-02-16 00:21:38 +00:00
|
|
|
ro.metrics = append(ro.metrics, metric)
|
2016-01-22 18:54:12 +00:00
|
|
|
} else {
|
2016-04-24 10:43:54 +00:00
|
|
|
flushSuccess := true
|
2016-02-16 00:21:38 +00:00
|
|
|
if ro.FlushBufferWhenFull {
|
2016-04-24 10:43:54 +00:00
|
|
|
err := ro.write(ro.metrics)
|
2016-02-16 00:21:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR writing full metric buffer to output %s, %s",
|
|
|
|
ro.Name, err)
|
2016-04-24 10:43:54 +00:00
|
|
|
flushSuccess = false
|
2016-02-16 00:21:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-24 10:43:54 +00:00
|
|
|
flushSuccess = false
|
|
|
|
}
|
|
|
|
if !flushSuccess {
|
|
|
|
if ro.tmpmetrics[ro.writeI] != nil && ro.writeI == ro.readI {
|
2016-03-09 13:44:32 +00:00
|
|
|
log.Printf("WARNING: overwriting cached metrics, you may want to " +
|
|
|
|
"increase the metric_buffer_limit setting in your [agent] " +
|
|
|
|
"config if you do not wish to overwrite metrics.\n")
|
2016-04-24 10:43:54 +00:00
|
|
|
ro.readI = (ro.readI + 1) % cap(ro.tmpmetrics)
|
2016-03-09 13:44:32 +00:00
|
|
|
}
|
2016-04-24 10:43:54 +00:00
|
|
|
ro.tmpmetrics[ro.writeI] = ro.metrics
|
|
|
|
ro.writeI = (ro.writeI + 1) % cap(ro.tmpmetrics)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2016-04-24 10:43:54 +00:00
|
|
|
ro.metrics = make([]telegraf.Metric, 0)
|
|
|
|
ro.metrics = append(ro.metrics, metric)
|
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-02-16 00:21:38 +00:00
|
|
|
ro.Lock()
|
|
|
|
defer ro.Unlock()
|
2016-04-24 10:43:54 +00:00
|
|
|
|
|
|
|
// Write any cached metric buffers before, as those metrics are the
|
|
|
|
// oldest
|
|
|
|
for ro.tmpmetrics[ro.readI] != nil {
|
|
|
|
if err := ro.write(ro.tmpmetrics[ro.readI]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
ro.tmpmetrics[ro.readI] = nil
|
|
|
|
ro.readI = (ro.readI + 1) % cap(ro.tmpmetrics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
err := ro.write(ro.metrics)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
ro.metrics = make([]telegraf.Metric, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Printf("Wrote %d metrics to output %s in %s\n",
|
2016-02-16 00:21:38 +00:00
|
|
|
len(metrics), ro.Name, elapsed)
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputConfig containing name and filter
|
|
|
|
type OutputConfig struct {
|
|
|
|
Name string
|
|
|
|
Filter Filter
|
|
|
|
}
|