danielqsj / kafka_exporter

Kafka exporter for Prometheus
Apache License 2.0
2.18k stars 610 forks source link

fix:fix data race bug when collecting metrics #402

Open Sniper91 opened 1 year ago

Sniper91 commented 1 year ago

reproduce procedure

  1. run kafka exporter with race detector
    go run -race ./  --kafka.server localhost:9092
  2. request kafka metrics frequently
    curl http://localhost:9308

    output

    
    ==================
    WARNING: DATA RACE
    Read at 0x00c0001ab170 by goroutine 27:
    main.(*Exporter).collectChans()
      /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:355 +0x366
    main.(*Exporter).Collect.func1()
      /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:330 +0x5b

Previous write at 0x00c0001ab170 by goroutine 28: main.(*Exporter).collectChans.func1() /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:347 +0x267

Goroutine 27 (running) created at: main.(Exporter).Collect() /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:330 +0x517 github.com/prometheus/client_golang/prometheus.(Registry).Gather.func1() /Users/bytedance/opensource/kafka_exporter/vendor/github.com/prometheus/client_golang/prometheus/registry.go:455 +0x245

Goroutine 28 (finished) created at: main.(Exporter).collectChans() /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:345 +0x253 main.(Exporter).Collect.func1() /Users/bytedance/opensource/kafka_exporter/kafka_exporter.go:330 +0x5b