-
Notifications
You must be signed in to change notification settings - Fork 147
/
chain.go
120 lines (104 loc) · 3.46 KB
/
chain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package metrics
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/samber/lo"
"github.com/iotaledger/wasp/packages/isc"
)
// ChainMetrics holds all metrics for a single chain
type ChainMetrics struct {
Pipe *ChainPipeMetrics
BlockWAL *ChainBlockWALMetrics
CmtLog *ChainCmtLogMetrics
Consensus *ChainConsensusMetrics
Mempool *ChainMempoolMetrics
Message *ChainMessageMetrics
StateManager *ChainStateManagerMetrics
Snapshots *ChainSnapshotsMetrics
NodeConn *ChainNodeConnMetrics
WebAPI *ChainWebAPIMetrics
State *ChainStateMetrics
}
// ChainMetricsProvider holds all metrics for all chains per chain
type ChainMetricsProvider struct {
mu sync.RWMutex
chains map[isc.ChainID]*ChainMetrics
Pipe *ChainPipeMetricsProvider
BlockWAL *ChainBlockWALMetricsProvider
CmtLog *ChainCmtLogMetricsProvider
Consensus *ChainConsensusMetricsProvider
Mempool *ChainMempoolMetricsProvider
Message *ChainMessageMetricsProvider
StateManager *ChainStateManagerMetricsProvider
Snapshots *ChainSnapshotsMetricsProvider
NodeConn *ChainNodeConnMetricsProvider
WebAPI *ChainWebAPIMetricsProvider
State *ChainStateMetricsProvider
}
func NewChainMetricsProvider() *ChainMetricsProvider {
return &ChainMetricsProvider{
chains: make(map[isc.ChainID]*ChainMetrics),
Pipe: newChainPipeMetricsProvider(),
BlockWAL: newChainBlockWALMetricsProvider(),
CmtLog: newChainCmtLogMetricsProvider(),
Consensus: newChainConsensusMetricsProvider(),
Mempool: newChainMempoolMetricsProvider(),
Message: newChainMessageMetricsProvider(),
StateManager: newChainStateManagerMetricsProvider(),
Snapshots: newChainSnapshotsMetricsProvider(),
NodeConn: newChainNodeConnMetricsProvider(),
WebAPI: NewChainWebAPIMetricsProvider(),
State: newChainStateMetricsProvider(),
}
}
func (m *ChainMetricsProvider) Register(reg prometheus.Registerer) {
m.Pipe.register(reg)
m.BlockWAL.register(reg)
m.CmtLog.register(reg)
m.Consensus.register(reg)
m.Mempool.register(reg)
m.Message.register(reg)
m.StateManager.register(reg)
m.Snapshots.register(reg)
m.NodeConn.register(reg)
m.WebAPI.register(reg)
m.State.register(reg)
}
func (m *ChainMetricsProvider) GetChainMetrics(chainID isc.ChainID) *ChainMetrics {
m.mu.Lock()
defer m.mu.Unlock()
if cm, ok := m.chains[chainID]; ok {
return cm
}
cm := &ChainMetrics{
Pipe: m.Pipe.createForChain(chainID),
BlockWAL: m.BlockWAL.createForChain(chainID),
CmtLog: m.CmtLog.createForChain(chainID),
Consensus: m.Consensus.createForChain(chainID),
Mempool: m.Mempool.createForChain(chainID),
Message: m.Message.createForChain(chainID),
StateManager: m.StateManager.createForChain(chainID),
Snapshots: m.Snapshots.createForChain(chainID),
NodeConn: m.NodeConn.createForChain(chainID),
WebAPI: m.WebAPI.CreateForChain(chainID),
State: m.State.createForChain(chainID),
}
m.chains[chainID] = cm
return cm
}
func (m *ChainMetricsProvider) RegisterChain(chainID isc.ChainID) {
m.GetChainMetrics(chainID)
}
func (m *ChainMetricsProvider) UnregisterChain(chainID isc.ChainID) {
m.mu.Lock()
defer m.mu.Unlock()
if cm, ok := m.chains[chainID]; ok {
cm.Pipe.cleanup()
delete(m.chains, chainID)
}
}
func (m *ChainMetricsProvider) RegisteredChains() []isc.ChainID {
m.mu.RLock()
defer m.mu.RUnlock()
return lo.Keys(m.chains)
}