Skip to content

Commit

Permalink
Fix locking if output is an AggregatingOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and Mathieu Lecarme committed Apr 17, 2020
1 parent cd046e7 commit 80eaf0f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internal/models/running_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func NewRunningOutput(
// AddMetric adds a metric to the output. This function can also write cached
// points if FlushBufferWhenFull is true.
func (ro *RunningOutput) AddMetric(m telegraf.Metric) {
ro.Lock()
defer ro.Unlock()

if m == nil {
return
}
Expand All @@ -115,8 +118,6 @@ func (ro *RunningOutput) AddMetric(m telegraf.Metric) {
}

if output, ok := ro.Output.(telegraf.AggregatingOutput); ok {
ro.Lock()
defer ro.Unlock()
output.Add(m)
return
}
Expand All @@ -134,6 +135,9 @@ func (ro *RunningOutput) AddMetric(m telegraf.Metric) {

// Write writes all cached points to this output.
func (ro *RunningOutput) Write() error {
ro.Lock()
defer ro.Unlock()

if output, ok := ro.Output.(telegraf.AggregatingOutput); ok {
metrics := output.Push()
ro.metrics.Add(metrics...)
Expand Down Expand Up @@ -188,8 +192,6 @@ func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
if nMetrics == 0 {
return nil
}
ro.Lock()
defer ro.Unlock()
start := time.Now()
err := ro.Output.Write(metrics)
elapsed := time.Since(start)
Expand Down

0 comments on commit 80eaf0f

Please sign in to comment.