set.go: fix data race in Set.ListMetricNames when accessing Set.m map

The Set.m map can be updated by concurrent goroutines when ListMetricNames is called
This commit is contained in:
Aliaksandr Valialkin 2021-11-01 14:51:25 +02:00
parent 2ec4485cad
commit 0ee99fd214
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

2
set.go
View File

@ -511,6 +511,8 @@ func (s *Set) UnregisterMetric(name string) bool {
// ListMetricNames returns a list of all the metrics in s.
func (s *Set) ListMetricNames() []string {
s.mu.Lock()
defer s.mu.Unlock()
var list []string
for name := range s.m {
list = append(list, name)