Skip to content

Commit

Permalink
fix: fix metrics race condition (#1194)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- Fixes race condition described in #1156, which was already fixed on
the 2.x branch.

## Short description of the changes

- Copy fix from that branch.

Fixes #1156.
  • Loading branch information
kentquirk authored Jun 11, 2024
1 parent 9b36da6 commit 1d3e19a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions metrics/otel_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,15 @@ func (o *OTelMetrics) Register(name string, metricType string) {
o.counters[name] = ctr
case "gauge":
var f metric.Float64Callback = func(_ context.Context, result metric.Float64Observer) error {
o.lock.Lock()
defer o.lock.Unlock()
result.Observe(o.values[name])
// this callback is invoked from outside this function call, so we
// need to Rlock when we read the values map. We don't know how long
// Observe() takes, so we make a copy of the value and unlock before
// calling Observe.
o.lock.RLock()
v := o.values[name]
o.lock.RUnlock()

result.Observe(v)
return nil
}
g, err := o.meter.Float64ObservableGauge(name,
Expand Down

0 comments on commit 1d3e19a

Please sign in to comment.