metrics: add mutex to the metric writer

It was possible for multiple status messages to be written at the same
time which caused some of the metric writer code to have a race
condition.

This code should be fast enough that it doesn't interrupt the display,
but some further work might be needed here.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg 2024-08-14 10:57:04 -05:00
parent 0e64eb4f8b
commit 6c5279da54
No known key found for this signature in database
GPG Key ID: 6603D4B96394F6B1
1 changed files with 4 additions and 0 deletions

View File

@ -45,6 +45,7 @@ var re = sync.OnceValue(func() *rePatterns {
type metricWriter struct {
recorders []metricRecorder
attrs attribute.Set
mu sync.Mutex
}
func newMetrics(mp metric.MeterProvider, attrs attribute.Set) *metricWriter {
@ -63,6 +64,9 @@ func newMetrics(mp metric.MeterProvider, attrs attribute.Set) *metricWriter {
}
func (mw *metricWriter) Write(ss *client.SolveStatus) {
mw.mu.Lock()
defer mw.mu.Unlock()
for _, recorder := range mw.recorders {
recorder.Record(ss)
}