Skip to content

Commit

Permalink
Merge pull request #285 from form3tech-oss/nvloff-metrics-init-race
Browse files Browse the repository at this point in the history
fix(metrics): fix race condition in metrics.Init()
  • Loading branch information
nvloff-f3 authored Sep 11, 2024
2 parents f016ea2 + d2db6af commit b125e2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func Init(iterationMetricsEnabled bool) {
}
m = NewInstance(defaultRegistry, iterationMetricsEnabled)
})
m.IterationMetricsEnabled = iterationMetricsEnabled
}

func Instance() *Metrics {
Expand Down
24 changes: 24 additions & 0 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package metrics_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/form3tech-oss/f1/v2/internal/metrics"
)

func TestMetrics_Init_IsSafe(t *testing.T) {
t.Parallel()

metrics.Init(true)

// race detector assertion
for range 10 {
go func() {
metrics.Init(false)
}()
}

assert.True(t, metrics.Instance().IterationMetricsEnabled)
}

0 comments on commit b125e2e

Please sign in to comment.