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 (
|
|
|
|
// Default number of metrics kept between flushes.
|
|
|
|
DEFAULT_METRIC_BUFFER_LIMIT = 10000
|
|
|
|
|
|
|
|
// Limit how many full metric buffers are kept due to failed writes.
|
|
|
|
FULL_METRIC_BUFFERS_LIMIT = 100
|
|
|
|
)
|
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
|
|
|
|
FlushBufferWhenFull bool
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
metrics []telegraf.Metric
|
|
|
|
tmpmetrics map[int][]telegraf.Metric
|
|
|
|
overwriteI int
|
|
|
|
mapI int
|
|
|
|
|
|
|
|
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),
|
|
|
|
tmpmetrics: make(map[int][]telegraf.Metric),
|
|
|
|
Output: output,
|
|
|
|
Config: conf,
|
|
|
|
MetricBufferLimit: DEFAULT_METRIC_BUFFER_LIMIT,
|
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-02-16 00:21:38 +00:00
|
|
|
if len(ro.metrics) < ro.MetricBufferLimit {
|
|
|
|
ro.metrics = append(ro.metrics, metric)
|
2016-01-22 18:54:12 +00:00
|
|
|
} else {
|
2016-02-16 00:21:38 +00:00
|
|
|
if ro.FlushBufferWhenFull {
|
|
|
|
tmpmetrics := make([]telegraf.Metric, len(ro.metrics))
|
|
|
|
copy(tmpmetrics, ro.metrics)
|
|
|
|
ro.metrics = make([]telegraf.Metric, 0)
|
|
|
|
err := ro.write(tmpmetrics)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR writing full metric buffer to output %s, %s",
|
|
|
|
ro.Name, err)
|
|
|
|
if len(ro.tmpmetrics) == FULL_METRIC_BUFFERS_LIMIT {
|
|
|
|
ro.mapI = 0
|
|
|
|
// overwrite one
|
|
|
|
ro.tmpmetrics[ro.mapI] = tmpmetrics
|
|
|
|
ro.mapI++
|
|
|
|
} else {
|
|
|
|
ro.tmpmetrics[ro.mapI] = tmpmetrics
|
|
|
|
ro.mapI++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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")
|
|
|
|
if ro.overwriteI == len(ro.metrics) {
|
|
|
|
ro.overwriteI = 0
|
|
|
|
}
|
|
|
|
ro.metrics[ro.overwriteI] = metric
|
|
|
|
ro.overwriteI++
|
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()
|
|
|
|
err := ro.write(ro.metrics)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
ro.metrics = make([]telegraf.Metric, 0)
|
|
|
|
ro.overwriteI = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write any cached metric buffers that failed previously
|
|
|
|
for i, tmpmetrics := range ro.tmpmetrics {
|
|
|
|
if err := ro.write(tmpmetrics); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
delete(ro.tmpmetrics, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
|
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
|
|
|
|
}
|