From 80eaf0f91535ae21014d2ae126ad6ad2e92dc0cd Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Mon, 10 Sep 2018 15:14:14 -0700 Subject: [PATCH] Fix locking if output is an AggregatingOutput --- internal/models/running_output.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/models/running_output.go b/internal/models/running_output.go index c926917d60b13..014202454e15d 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -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 } @@ -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 } @@ -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...) @@ -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)