From 62a4188c94ebb49f25f8cf1ae7e25e8d1516c49c Mon Sep 17 00:00:00 2001 From: sniper91 Date: Tue, 29 Aug 2023 11:47:47 +0800 Subject: [PATCH] fix data race bug when collecting metrics --- kafka_exporter.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kafka_exporter.go b/kafka_exporter.go index 36021149..74af71ad 100644 --- a/kafka_exporter.go +++ b/kafka_exporter.go @@ -16,7 +16,7 @@ import ( "time" "github.com/Shopify/sarama" - kingpin "github.com/alecthomas/kingpin/v2" + "github.com/alecthomas/kingpin/v2" "github.com/krallistic/kazoo-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" @@ -342,13 +342,16 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { func (e *Exporter) collectChans(quit chan struct{}) { original := make(chan prometheus.Metric) container := make([]prometheus.Metric, 0, 100) + done := make(chan struct{}) go func() { for metric := range original { container = append(container, metric) } + close(done) }() e.collect(original) close(original) + <-done // Lock to avoid modification on the channel slice e.sgMutex.Lock() for _, ch := range e.sgChans {