Skip to content

Commit 0fff86d

Browse files
committed
Make metrics.Registry.Deregister thread safe
1 parent c9ce45c commit 0fff86d

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

ledger/metrics_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ import (
2727
"github.com/algorand/go-algorand/ledger/ledgercore"
2828
ledgertesting "github.com/algorand/go-algorand/ledger/testing"
2929
"github.com/algorand/go-algorand/protocol"
30+
"github.com/algorand/go-algorand/test/partitiontest"
3031
"github.com/algorand/go-algorand/util/metrics"
3132
)
3233

3334
func TestMetricsReload(t *testing.T) {
35+
partitiontest.PartitionTest(t)
36+
3437
mt := metricsTracker{}
3538
accts := ledgertesting.RandomAccounts(1, true)
3639
ml := makeMockLedgerForTracker(t, true, 1, protocol.ConsensusCurrentVersion, []map[basics.Address]basics.AccountData{accts})

util/metrics/counter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (counter *Counter) Deregister(reg *Registry) {
6363
// Inc increases counter by 1
6464
// Much faster if labels is nil or empty.
6565
func (counter *Counter) Inc(labels map[string]string) {
66-
if labels == nil || len(labels) == 0 {
66+
if len(labels) == 0 {
6767
counter.fastAddUint64(1)
6868
} else {
6969
counter.Add(1.0, labels)
@@ -98,7 +98,7 @@ func (counter *Counter) Add(x float64, labels map[string]string) {
9898
// If labels is nil this is much faster than Add()
9999
// Calls through to Add() if labels is not nil.
100100
func (counter *Counter) AddUint64(x uint64, labels map[string]string) {
101-
if labels == nil || len(labels) == 0 {
101+
if len(labels) == 0 {
102102
counter.fastAddUint64(x)
103103
} else {
104104
counter.Add(float64(x), labels)
@@ -108,7 +108,7 @@ func (counter *Counter) AddUint64(x uint64, labels map[string]string) {
108108
// AddMicrosecondsSince increases counter by microseconds between Time t and now.
109109
// Fastest if labels is nil
110110
func (counter *Counter) AddMicrosecondsSince(t time.Time, labels map[string]string) {
111-
counter.AddUint64(uint64(time.Now().Sub(t).Microseconds()), labels)
111+
counter.AddUint64(uint64(time.Since(t).Microseconds()), labels)
112112
}
113113

114114
func (counter *Counter) fastAddUint64(x uint64) {

util/metrics/registry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (r *Registry) Register(metric Metric) {
5050

5151
// Deregister removes the given metric to the registry
5252
func (r *Registry) Deregister(metric Metric) {
53+
r.metricsMu.Lock()
54+
defer r.metricsMu.Unlock()
5355
for i, m := range r.metrics {
5456
if m == metric {
5557
r.metrics = append(r.metrics[:i], r.metrics[i+1:]...)

util/metrics/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
var (
3838
// the duration of which we'll keep a metric in-memory and keep reporting it.
3939
// when a metric time expires, it would get removed.
40+
// TODO: implement or remove
4041
maxMetricRetensionDuration = time.Duration(5) * time.Minute
4142
)
4243

0 commit comments

Comments
 (0)