Skip to content

Commit

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

- Fixes #1156 

## Short description of the changes

- Add an RLock in the callback 
- Unlock before calling Observe()

Should be cherry-picked to Refinery 3.
  • Loading branch information
kentquirk authored May 22, 2024
1 parent 4bb5695 commit 636f03b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion metrics/otel_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +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 {
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 636f03b

Please sign in to comment.