Skip to content

Commit

Permalink
Merge pull request #70171 from adityamaru/backport20.2-69538
Browse files Browse the repository at this point in the history
release-20.2: metric: fix missing mutex handling in metrics registry
  • Loading branch information
adityamaru authored Sep 15, 2021
2 parents 15b962c + f115dd2 commit ce1977c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/util/metric/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (r *Registry) addMetricValue(
// WriteMetricsMetadata writes metadata from all tracked metrics to the
// parameter map.
func (r *Registry) WriteMetricsMetadata(dest map[string]Metadata) {
r.Lock()
defer r.Unlock()
for _, v := range r.tracked {
dest[v.GetName()] = v.GetMetadata()
}
Expand All @@ -157,6 +159,8 @@ func (r *Registry) Each(f func(name string, val interface{})) {

// MarshalJSON marshals to JSON.
func (r *Registry) MarshalJSON() ([]byte, error) {
r.Lock()
defer r.Unlock()
m := make(map[string]interface{})
for _, metric := range r.tracked {
metric.Inspect(func(v interface{}) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/util/metric/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

func (r *Registry) findMetricByName(name string) Iterable {
r.Lock()
defer r.Unlock()
for _, metric := range r.tracked {
if metric.GetName() == name {
return metric
Expand All @@ -28,8 +30,6 @@ func (r *Registry) findMetricByName(name string) Iterable {
// Counter with this name is not present (including if a non-Counter Iterable is
// registered with the name), nil is returned.
func (r *Registry) getCounter(name string) *Counter {
r.Lock()
defer r.Unlock()
iterable := r.findMetricByName(name)
if iterable == nil {
return nil
Expand All @@ -47,8 +47,6 @@ func (r *Registry) getCounter(name string) *Counter {
// with this name is not present (including if a non-Gauge Iterable is
// registered with the name), nil is returned.
func (r *Registry) getGauge(name string) *Gauge {
r.Lock()
defer r.Unlock()
iterable := r.findMetricByName(name)
if iterable == nil {
return nil
Expand Down

0 comments on commit ce1977c

Please sign in to comment.