Skip to content

Commit c9ce45c

Browse files
committed
metrics tracker: deregister on close
1 parent bc5f22c commit c9ce45c

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

ledger/metrics.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ func (mt *metricsTracker) loadFromDisk(l ledgerForTracker, _ basics.Round) error
4040
}
4141

4242
func (mt *metricsTracker) close() {
43+
if mt.ledgerTransactionsTotal != nil {
44+
mt.ledgerTransactionsTotal.Deregister(nil)
45+
mt.ledgerTransactionsTotal = nil
46+
}
47+
if mt.ledgerRewardClaimsTotal != nil {
48+
mt.ledgerRewardClaimsTotal.Deregister(nil)
49+
mt.ledgerRewardClaimsTotal = nil
50+
}
51+
if mt.ledgerRound != nil {
52+
mt.ledgerRound.Deregister(nil)
53+
mt.ledgerRound = nil
54+
}
4355
}
4456

4557
func (mt *metricsTracker) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {

ledger/metrics_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (C) 2019-2022 Algorand, Inc.
2+
// This file is part of go-algorand
3+
//
4+
// go-algorand is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// go-algorand is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package ledger
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/algorand/go-algorand/data/basics"
26+
"github.com/algorand/go-algorand/data/bookkeeping"
27+
"github.com/algorand/go-algorand/ledger/ledgercore"
28+
ledgertesting "github.com/algorand/go-algorand/ledger/testing"
29+
"github.com/algorand/go-algorand/protocol"
30+
"github.com/algorand/go-algorand/util/metrics"
31+
)
32+
33+
func TestMetricsReload(t *testing.T) {
34+
mt := metricsTracker{}
35+
accts := ledgertesting.RandomAccounts(1, true)
36+
ml := makeMockLedgerForTracker(t, true, 1, protocol.ConsensusCurrentVersion, []map[basics.Address]basics.AccountData{accts})
37+
38+
mt.loadFromDisk(ml, 0)
39+
blk := bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: 1}}
40+
mt.newBlock(blk, ledgercore.StateDelta{})
41+
mt.close()
42+
43+
mt.loadFromDisk(ml, 0)
44+
blk = bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: 2}}
45+
mt.newBlock(blk, ledgercore.StateDelta{})
46+
47+
var buf strings.Builder
48+
metrics.DefaultRegistry().WriteMetrics(&buf, "")
49+
lines := strings.Split(buf.String(), "\n")
50+
txCount := 0
51+
rcCount := 0
52+
rCount := 0
53+
for _, line := range lines {
54+
if strings.HasPrefix(line, "# HELP") || strings.HasPrefix(line, "# TYPE") {
55+
// ignore comments
56+
continue
57+
}
58+
if strings.HasPrefix(line, metrics.LedgerTransactionsTotal.Name) {
59+
txCount++
60+
}
61+
if strings.HasPrefix(line, metrics.LedgerRewardClaimsTotal.Name) {
62+
rcCount++
63+
}
64+
if strings.HasPrefix(line, metrics.LedgerRound.Name) {
65+
rCount++
66+
}
67+
}
68+
require.Equal(t, 1, txCount)
69+
require.Equal(t, 1, rcCount)
70+
require.Equal(t, 1, rCount)
71+
72+
mt.close()
73+
}

0 commit comments

Comments
 (0)