Lock buffer when adding metrics (#4514)
This function is not thread-safe but is currently used by multiple goroutines in RunningOutput
This commit is contained in:
parent
4dfb80d0fc
commit
feb75d493a
|
@ -40,18 +40,18 @@ func (b *Buffer) Len() int {
|
||||||
|
|
||||||
// Add adds metrics to the buffer.
|
// Add adds metrics to the buffer.
|
||||||
func (b *Buffer) Add(metrics ...telegraf.Metric) {
|
func (b *Buffer) Add(metrics ...telegraf.Metric) {
|
||||||
|
b.mu.Lock()
|
||||||
for i, _ := range metrics {
|
for i, _ := range metrics {
|
||||||
MetricsWritten.Incr(1)
|
MetricsWritten.Incr(1)
|
||||||
select {
|
select {
|
||||||
case b.buf <- metrics[i]:
|
case b.buf <- metrics[i]:
|
||||||
default:
|
default:
|
||||||
b.mu.Lock()
|
|
||||||
MetricsDropped.Incr(1)
|
MetricsDropped.Incr(1)
|
||||||
<-b.buf
|
<-b.buf
|
||||||
b.buf <- metrics[i]
|
b.buf <- metrics[i]
|
||||||
b.mu.Unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
b.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch returns a batch of metrics of size batchSize.
|
// Batch returns a batch of metrics of size batchSize.
|
||||||
|
|
Loading…
Reference in New Issue