Skip to content

Commit ee468be

Browse files
committed
Flush based on buffer size rather than time
this includes: - Add Accumulator to the Start() function of service inputs - For message consumer plugins, use the Accumulator to constantly add metrics and make Gather a dummy function - rework unit tests to match this new behavior. - make "flush_buffer_when_full" a config option that defaults to true closes #666
1 parent 7f539c9 commit ee468be

File tree

15 files changed

+271
-285
lines changed

15 files changed

+271
-285
lines changed

agent/agent.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func (a *Agent) Connect() error {
5858
}
5959
err := o.Output.Connect()
6060
if err != nil {
61-
log.Printf("Failed to connect to output %s, retrying in 15s, error was '%s' \n", o.Name, err)
61+
log.Printf("Failed to connect to output %s, retrying in 15s, "+
62+
"error was '%s' \n", o.Name, err)
6263
time.Sleep(15 * time.Second)
6364
err = o.Output.Connect()
6465
if err != nil {
@@ -241,7 +242,7 @@ func (a *Agent) Test() error {
241242
return nil
242243
}
243244

244-
// flush writes a list of points to all configured outputs
245+
// flush writes a list of metrics to all configured outputs
245246
func (a *Agent) flush() {
246247
var wg sync.WaitGroup
247248

@@ -260,7 +261,7 @@ func (a *Agent) flush() {
260261
wg.Wait()
261262
}
262263

263-
// flusher monitors the points input channel and flushes on the minimum interval
264+
// flusher monitors the metrics input channel and flushes on the minimum interval
264265
func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) error {
265266
// Inelegant, but this sleep is to allow the Gather threads to run, so that
266267
// the flusher will flush after metrics are collected.
@@ -271,14 +272,14 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er
271272
for {
272273
select {
273274
case <-shutdown:
274-
log.Println("Hang on, flushing any cached points before shutdown")
275+
log.Println("Hang on, flushing any cached metrics before shutdown")
275276
a.flush()
276277
return nil
277278
case <-ticker.C:
278279
a.flush()
279280
case m := <-metricC:
280281
for _, o := range a.Config.Outputs {
281-
o.AddPoint(m)
282+
o.AddMetric(m)
282283
}
283284
}
284285
}
@@ -318,8 +319,8 @@ func (a *Agent) Run(shutdown chan struct{}) error {
318319
a.Config.Agent.Interval.Duration, a.Config.Agent.Debug, a.Config.Agent.Quiet,
319320
a.Config.Agent.Hostname, a.Config.Agent.FlushInterval.Duration)
320321

321-
// channel shared between all input threads for accumulating points
322-
metricC := make(chan telegraf.Metric, 1000)
322+
// channel shared between all input threads for accumulating metrics
323+
metricC := make(chan telegraf.Metric, 10000)
323324

324325
// Round collection to nearest interval by sleeping
325326
if a.Config.Agent.RoundInterval {
@@ -342,7 +343,10 @@ func (a *Agent) Run(shutdown chan struct{}) error {
342343
// Start service of any ServicePlugins
343344
switch p := input.Input.(type) {
344345
case telegraf.ServiceInput:
345-
if err := p.Start(); err != nil {
346+
acc := NewAccumulator(input.Config, metricC)
347+
acc.SetDebug(a.Config.Agent.Debug)
348+
acc.setDefaultTags(a.Config.Tags)
349+
if err := p.Start(acc); err != nil {
346350
log.Printf("Service for input %s failed to start, exiting\n%s\n",
347351
input.Name, err.Error())
348352
return err

etc/telegraf.conf

+24-10
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,37 @@
1616

1717
# Configuration for telegraf agent
1818
[agent]
19-
# Default data collection interval for all plugins
19+
### Default data collection interval for all inputs
2020
interval = "10s"
21-
# Rounds collection interval to 'interval'
22-
# ie, if interval="10s" then always collect on :00, :10, :20, etc.
21+
### Rounds collection interval to 'interval'
22+
### ie, if interval="10s" then always collect on :00, :10, :20, etc.
2323
round_interval = true
2424

25-
# Default data flushing interval for all outputs. You should not set this below
26-
# interval. Maximum flush_interval will be flush_interval + flush_jitter
25+
### Telegraf will cache metric_buffer_limit metrics for each output, and will
26+
### flush this buffer on a successful write.
27+
metric_buffer_limit = 10000
28+
### Flush the buffer whenever full, regardless of flush_interval.
29+
flush_buffer_when_full = true
30+
31+
### Collection jitter is used to jitter the collection by a random amount.
32+
### Each plugin will sleep for a random time within jitter before collecting.
33+
### This can be used to avoid many plugins querying things like sysfs at the
34+
### same time, which can have a measurable effect on the system.
35+
collection_jitter = "0s"
36+
37+
### Default flushing interval for all outputs. You shouldn't set this below
38+
### interval. Maximum flush_interval will be flush_interval + flush_jitter
2739
flush_interval = "10s"
28-
# Jitter the flush interval by a random amount. This is primarily to avoid
29-
# large write spikes for users running a large number of telegraf instances.
30-
# ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
40+
### Jitter the flush interval by a random amount. This is primarily to avoid
41+
### large write spikes for users running a large number of telegraf instances.
42+
### ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
3143
flush_jitter = "0s"
3244

33-
# Run telegraf in debug mode
45+
### Run telegraf in debug mode
3446
debug = false
35-
# Override default hostname, if empty use os.Hostname()
47+
### Run telegraf in quiet mode
48+
quiet = false
49+
### Override default hostname, if empty use os.Hostname()
3650
hostname = ""
3751

3852

input.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type ServiceInput interface {
2424
Gather(Accumulator) error
2525

2626
// Start starts the ServiceInput's service, whatever that may be
27-
Start() error
27+
Start(Accumulator) error
2828

2929
// Stop stops the services and closes any necessary channels and connections
3030
Stop()

internal/config/config.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type AgentConfig struct {
6868
// same time, which can have a measurable effect on the system.
6969
CollectionJitter internal.Duration
7070

71-
// Interval at which to flush data
71+
// FlushInterval is the Interval at which to flush data
7272
FlushInterval internal.Duration
7373

7474
// FlushJitter Jitters the flush interval by a random amount.
@@ -82,6 +82,11 @@ type AgentConfig struct {
8282
// full, the oldest metrics will be overwritten.
8383
MetricBufferLimit int
8484

85+
// FlushBufferWhenFull tells Telegraf to flush the metric buffer whenever
86+
// it fills up, regardless of FlushInterval. Setting this option to true
87+
// does _not_ deactivate FlushInterval.
88+
FlushBufferWhenFull bool
89+
8590
// TODO(cam): Remove UTC and Precision parameters, they are no longer
8691
// valid for the agent config. Leaving them here for now for backwards-
8792
// compatability
@@ -157,6 +162,8 @@ var header = `##################################################################
157162
### Telegraf will cache metric_buffer_limit metrics for each output, and will
158163
### flush this buffer on a successful write.
159164
metric_buffer_limit = 10000
165+
### Flush the buffer whenever full, regardless of flush_interval.
166+
flush_buffer_when_full = true
160167
161168
### Collection jitter is used to jitter the collection by a random amount.
162169
### Each plugin will sleep for a random time within jitter before collecting.
@@ -421,8 +428,9 @@ func (c *Config) addOutput(name string, table *ast.Table) error {
421428

422429
ro := internal_models.NewRunningOutput(name, output, outputConfig)
423430
if c.Agent.MetricBufferLimit > 0 {
424-
ro.PointBufferLimit = c.Agent.MetricBufferLimit
431+
ro.MetricBufferLimit = c.Agent.MetricBufferLimit
425432
}
433+
ro.FlushBufferWhenFull = c.Agent.FlushBufferWhenFull
426434
ro.Quiet = c.Agent.Quiet
427435
c.Outputs = append(c.Outputs, ro)
428436
return nil

internal/models/running_output.go

+87-28
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@ package internal_models
22

33
import (
44
"log"
5+
"sync"
56
"time"
67

78
"github.com/influxdata/telegraf"
89
)
910

10-
const DEFAULT_POINT_BUFFER_LIMIT = 10000
11+
const (
12+
// Default number of metrics kept between flushes.
13+
DEFAULT_METRIC_BUFFER_LIMIT = 10000
14+
15+
// Limit how many full metric buffers are kept due to failed writes.
16+
FULL_METRIC_BUFFERS_LIMIT = 100
17+
)
1118

1219
type RunningOutput struct {
13-
Name string
14-
Output telegraf.Output
15-
Config *OutputConfig
16-
Quiet bool
17-
PointBufferLimit int
20+
Name string
21+
Output telegraf.Output
22+
Config *OutputConfig
23+
Quiet bool
24+
MetricBufferLimit int
25+
FlushBufferWhenFull bool
1826

19-
metrics []telegraf.Metric
20-
overwriteCounter int
27+
metrics []telegraf.Metric
28+
tmpmetrics map[int][]telegraf.Metric
29+
overwriteI int
30+
mapI int
31+
32+
sync.Mutex
2133
}
2234

2335
func NewRunningOutput(
@@ -26,47 +38,94 @@ func NewRunningOutput(
2638
conf *OutputConfig,
2739
) *RunningOutput {
2840
ro := &RunningOutput{
29-
Name: name,
30-
metrics: make([]telegraf.Metric, 0),
31-
Output: output,
32-
Config: conf,
33-
PointBufferLimit: DEFAULT_POINT_BUFFER_LIMIT,
41+
Name: name,
42+
metrics: make([]telegraf.Metric, 0),
43+
tmpmetrics: make(map[int][]telegraf.Metric),
44+
Output: output,
45+
Config: conf,
46+
MetricBufferLimit: DEFAULT_METRIC_BUFFER_LIMIT,
3447
}
3548
return ro
3649
}
3750

38-
func (ro *RunningOutput) AddPoint(point telegraf.Metric) {
51+
// AddMetric adds a metric to the output. This function can also write cached
52+
// points if FlushBufferWhenFull is true.
53+
func (ro *RunningOutput) AddMetric(metric telegraf.Metric) {
3954
if ro.Config.Filter.IsActive {
40-
if !ro.Config.Filter.ShouldMetricPass(point) {
55+
if !ro.Config.Filter.ShouldMetricPass(metric) {
4156
return
4257
}
4358
}
59+
ro.Lock()
60+
defer ro.Unlock()
4461

45-
if len(ro.metrics) < ro.PointBufferLimit {
46-
ro.metrics = append(ro.metrics, point)
62+
if len(ro.metrics) < ro.MetricBufferLimit {
63+
ro.metrics = append(ro.metrics, metric)
4764
} else {
48-
log.Printf("WARNING: overwriting cached metrics, you may want to " +
49-
"increase the metric_buffer_limit setting in your [agent] config " +
50-
"if you do not wish to overwrite metrics.\n")
51-
if ro.overwriteCounter == len(ro.metrics) {
52-
ro.overwriteCounter = 0
65+
if ro.FlushBufferWhenFull {
66+
tmpmetrics := make([]telegraf.Metric, len(ro.metrics))
67+
copy(tmpmetrics, ro.metrics)
68+
ro.metrics = make([]telegraf.Metric, 0)
69+
err := ro.write(tmpmetrics)
70+
if err != nil {
71+
log.Printf("ERROR writing full metric buffer to output %s, %s",
72+
ro.Name, err)
73+
if len(ro.tmpmetrics) == FULL_METRIC_BUFFERS_LIMIT {
74+
ro.mapI = 0
75+
// overwrite one
76+
ro.tmpmetrics[ro.mapI] = tmpmetrics
77+
ro.mapI++
78+
} else {
79+
ro.tmpmetrics[ro.mapI] = tmpmetrics
80+
ro.mapI++
81+
}
82+
}
83+
} else {
84+
log.Printf("WARNING: overwriting cached metrics, you may want to " +
85+
"increase the metric_buffer_limit setting in your [agent] " +
86+
"config if you do not wish to overwrite metrics.\n")
87+
if ro.overwriteI == len(ro.metrics) {
88+
ro.overwriteI = 0
89+
}
90+
ro.metrics[ro.overwriteI] = metric
91+
ro.overwriteI++
5392
}
54-
ro.metrics[ro.overwriteCounter] = point
55-
ro.overwriteCounter++
5693
}
5794
}
5895

96+
// Write writes all cached points to this output.
5997
func (ro *RunningOutput) Write() error {
98+
ro.Lock()
99+
defer ro.Unlock()
100+
err := ro.write(ro.metrics)
101+
if err != nil {
102+
return err
103+
} else {
104+
ro.metrics = make([]telegraf.Metric, 0)
105+
ro.overwriteI = 0
106+
}
107+
108+
// Write any cached metric buffers that failed previously
109+
for i, tmpmetrics := range ro.tmpmetrics {
110+
if err := ro.write(tmpmetrics); err != nil {
111+
return err
112+
} else {
113+
delete(ro.tmpmetrics, i)
114+
}
115+
}
116+
117+
return nil
118+
}
119+
120+
func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
60121
start := time.Now()
61-
err := ro.Output.Write(ro.metrics)
122+
err := ro.Output.Write(metrics)
62123
elapsed := time.Since(start)
63124
if err == nil {
64125
if !ro.Quiet {
65126
log.Printf("Wrote %d metrics to output %s in %s\n",
66-
len(ro.metrics), ro.Name, elapsed)
127+
len(metrics), ro.Name, elapsed)
67128
}
68-
ro.metrics = make([]telegraf.Metric, 0)
69-
ro.overwriteCounter = 0
70129
}
71130
return err
72131
}

plugins/inputs/github_webhooks/github_webhooks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (gh *GithubWebhooks) Listen() {
6161
}
6262
}
6363

64-
func (gh *GithubWebhooks) Start() error {
64+
func (gh *GithubWebhooks) Start(_ telegraf.Accumulator) error {
6565
go gh.Listen()
6666
log.Printf("Started the github_webhooks service on %s\n", gh.ServiceAddress)
6767
return nil

0 commit comments

Comments
 (0)