Skip to content

Commit 4a973ef

Browse files
capt4ceiltoga
authored andcommitted
keep track of p2p go routine metrics (#569)
1 parent 545cf8d commit 4a973ef

File tree

3 files changed

+227
-18
lines changed

3 files changed

+227
-18
lines changed

common/monitoring/metricsMonitoring.go

Lines changed: 172 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math"
66
"reflect"
7+
"sync"
78

89
"github.com/prometheus/client_golang/prometheus"
910
"github.com/zoobc/zoobc-core/common/model"
@@ -16,20 +17,71 @@ type lastblockMetrics struct {
1617
}
1718

1819
var (
19-
isMonitoringActive bool
20-
nodePublicKey []byte
21-
receiptCounter prometheus.Counter
22-
unresolvedPeersCounter prometheus.Gauge
23-
resolvedPeersCounter prometheus.Gauge
24-
unresolvedPriorityPeersCounter prometheus.Gauge
25-
resolvedPriorityPeersCounter prometheus.Gauge
20+
isMonitoringActive bool
21+
nodePublicKey []byte
22+
23+
receiptCounter prometheus.Counter
24+
receiptCounterSync sync.Mutex
25+
26+
unresolvedPeersCounter prometheus.Gauge
27+
unresolvedPeersCounterSync sync.Mutex
28+
29+
resolvedPeersCounter prometheus.Gauge
30+
resolvedPeersCounterSync sync.Mutex
31+
32+
unresolvedPriorityPeersCounter prometheus.Gauge
33+
unresolvedPriorityPeersCounterSync sync.Mutex
34+
35+
resolvedPriorityPeersCounter prometheus.Gauge
36+
resolvedPriorityPeersCounterSync sync.Mutex
37+
2638
activeRegisteredNodesGauge prometheus.Gauge
27-
nodeScore prometheus.Gauge
28-
blockerCounter = make(map[string]prometheus.Counter)
29-
statusLockCounter = make(map[int]prometheus.Gauge)
30-
blockchainStatus = make(map[int32]prometheus.Gauge)
31-
blockchainSmithTime = make(map[int32]prometheus.Gauge)
32-
blockchainHeight = make(map[int32]*lastblockMetrics)
39+
activeRegisteredNodesGaugeSync sync.Mutex
40+
41+
nodeScore prometheus.Gauge
42+
nodeScoreSync sync.Mutex
43+
44+
blockerCounter = make(map[string]prometheus.Counter)
45+
blockerCounterSync sync.Mutex
46+
47+
statusLockCounter = make(map[int]prometheus.Gauge)
48+
statusLockCounterSync sync.Mutex
49+
50+
blockchainStatus = make(map[int32]prometheus.Gauge)
51+
blockchainStatusSync sync.Mutex
52+
53+
blockchainSmithTime = make(map[int32]prometheus.Gauge)
54+
blockchainSmithTimeSync sync.Mutex
55+
56+
blockchainHeight = make(map[int32]*lastblockMetrics)
57+
blockchainHeightSync sync.Mutex
58+
59+
goRoutineActivityCounters = make(map[string]prometheus.Gauge)
60+
goRoutineActivityCountersSync sync.Mutex
61+
)
62+
63+
const (
64+
P2pGetPeerInfoServer = "P2pGetPeerInfoServer"
65+
P2pGetMorePeersServer = "P2pGetMorePeersServer"
66+
P2pSendPeersServer = "P2pSendPeersServer"
67+
P2pSendBlockServer = "P2pSendBlockServer"
68+
P2pSendTransactionServer = "P2pSendTransactionServer"
69+
P2pRequestBlockTransactionsServer = "P2pRequestBlockTransactionsServer"
70+
P2pGetCumulativeDifficultyServer = "P2pGetCumulativeDifficultyServer"
71+
P2pGetCommonMilestoneBlockIDsServer = "P2pGetCommonMilestoneBlockIDsServer"
72+
P2pGetNextBlockIDsServer = "P2pGetNextBlockIDsServer"
73+
P2pGetNextBlocksServer = "P2pGetNextBlocksServer"
74+
75+
P2pGetPeerInfoClient = "P2pGetPeerInfoClient"
76+
P2pGetMorePeersClient = "P2pGetMorePeersClient"
77+
P2pSendPeersClient = "P2pSendPeersClient"
78+
P2pSendBlockClient = "P2pSendBlockClient"
79+
P2pSendTransactionClient = "P2pSendTransactionClient"
80+
P2pRequestBlockTransactionsClient = "P2pRequestBlockTransactionsClient"
81+
P2pGetCumulativeDifficultyClient = "P2pGetCumulativeDifficultyClient"
82+
P2pGetCommonMilestoneBlockIDsClient = "P2pGetCommonMilestoneBlockIDsClient"
83+
P2pGetNextBlockIDsClient = "P2pGetNextBlockIDsClient"
84+
P2pGetNextBlocksClient = "P2pGetNextBlocksClient"
3385
)
3486

3587
func SetMonitoringActive(isActive bool) {
@@ -45,6 +97,12 @@ func IsMonitoringActive() bool {
4597
}
4698

4799
func IncrementReceiptCounter() {
100+
if !isMonitoringActive {
101+
return
102+
}
103+
104+
receiptCounterSync.Lock()
105+
defer receiptCounterSync.Unlock()
48106
if receiptCounter == nil {
49107
receiptCounter = prometheus.NewCounter(prometheus.CounterOpts{
50108
Name: fmt.Sprintf("zoobc_receipts"),
@@ -57,6 +115,13 @@ func IncrementReceiptCounter() {
57115
}
58116

59117
func SetUnresolvedPeersCount(count int) {
118+
if !isMonitoringActive {
119+
return
120+
}
121+
122+
unresolvedPeersCounterSync.Lock()
123+
defer unresolvedPeersCounterSync.Unlock()
124+
60125
if unresolvedPeersCounter == nil {
61126
unresolvedPeersCounter = prometheus.NewGauge(prometheus.GaugeOpts{
62127
Name: fmt.Sprintf("zoobc_unresolved_peers"),
@@ -69,6 +134,13 @@ func SetUnresolvedPeersCount(count int) {
69134
}
70135

71136
func SetResolvedPeersCount(count int) {
137+
if !isMonitoringActive {
138+
return
139+
}
140+
141+
resolvedPeersCounterSync.Lock()
142+
defer resolvedPeersCounterSync.Unlock()
143+
72144
if resolvedPeersCounter == nil {
73145
resolvedPeersCounter = prometheus.NewGauge(prometheus.GaugeOpts{
74146
Name: fmt.Sprintf("zoobc_resolved_peers"),
@@ -81,6 +153,13 @@ func SetResolvedPeersCount(count int) {
81153
}
82154

83155
func SetResolvedPriorityPeersCount(count int) {
156+
if !isMonitoringActive {
157+
return
158+
}
159+
160+
resolvedPriorityPeersCounterSync.Lock()
161+
defer resolvedPriorityPeersCounterSync.Unlock()
162+
84163
if resolvedPriorityPeersCounter == nil {
85164
resolvedPriorityPeersCounter = prometheus.NewGauge(prometheus.GaugeOpts{
86165
Name: fmt.Sprintf("zoobc_resolved_priority_peers"),
@@ -93,6 +172,13 @@ func SetResolvedPriorityPeersCount(count int) {
93172
}
94173

95174
func SetUnresolvedPriorityPeersCount(count int) {
175+
if !isMonitoringActive {
176+
return
177+
}
178+
179+
unresolvedPriorityPeersCounterSync.Lock()
180+
defer unresolvedPriorityPeersCounterSync.Unlock()
181+
96182
if unresolvedPriorityPeersCounter == nil {
97183
unresolvedPriorityPeersCounter = prometheus.NewGauge(prometheus.GaugeOpts{
98184
Name: fmt.Sprintf("zoobc_unresolved_priority_peers"),
@@ -105,6 +191,13 @@ func SetUnresolvedPriorityPeersCount(count int) {
105191
}
106192

107193
func SetActiveRegisteredNodesCount(count int) {
194+
if !isMonitoringActive {
195+
return
196+
}
197+
198+
activeRegisteredNodesGaugeSync.Lock()
199+
defer activeRegisteredNodesGaugeSync.Unlock()
200+
108201
if activeRegisteredNodesGauge == nil {
109202
activeRegisteredNodesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
110203
Name: fmt.Sprintf("zoobc_active_registered_nodes"),
@@ -121,6 +214,9 @@ func IncrementBlockerMetrics(typeBlocker string) {
121214
return
122215
}
123216

217+
blockerCounterSync.Lock()
218+
defer blockerCounterSync.Unlock()
219+
124220
if blockerCounter[typeBlocker] == nil {
125221
blockerCounter[typeBlocker] = prometheus.NewCounter(prometheus.CounterOpts{
126222
Name: fmt.Sprintf("zoobc_err_%s", typeBlocker),
@@ -136,6 +232,9 @@ func IncrementStatusLockCounter(typeStatusLock int) {
136232
return
137233
}
138234

235+
statusLockCounterSync.Lock()
236+
defer statusLockCounterSync.Unlock()
237+
139238
if statusLockCounter[typeStatusLock] == nil {
140239
statusLockCounter[typeStatusLock] = prometheus.NewGauge(prometheus.GaugeOpts{
141240
Name: fmt.Sprintf("zoobc_status_lock_%d", typeStatusLock),
@@ -154,25 +253,30 @@ func DecrementStatusLockCounter(typeStatusLock int) {
154253
return
155254
}
156255

157-
if !isMonitoringActive {
158-
return
159-
}
256+
statusLockCounterSync.Lock()
257+
defer statusLockCounterSync.Unlock()
160258

161259
if statusLockCounter[typeStatusLock] == nil {
162260
statusLockCounter[typeStatusLock] = prometheus.NewGauge(prometheus.GaugeOpts{
163261
Name: fmt.Sprintf("zoobc_status_lock_%d", typeStatusLock),
164262
Help: fmt.Sprintf("Status lock %d counter", typeStatusLock),
165263
})
166264
prometheus.MustRegister(statusLockCounter[typeStatusLock])
167-
} else {
168-
statusLockCounter[typeStatusLock].Dec()
265+
266+
// to avoid below as the initial value, on creation on decrement, we exit
267+
return
169268
}
269+
statusLockCounter[typeStatusLock].Dec()
170270
}
171271

172272
func SetBlockchainStatus(chainType int32, newStatus int) {
173273
if !isMonitoringActive {
174274
return
175275
}
276+
277+
blockchainStatusSync.Lock()
278+
defer blockchainStatusSync.Unlock()
279+
176280
if blockchainStatus[chainType] == nil {
177281
blockchainStatus[chainType] = prometheus.NewGauge(prometheus.GaugeOpts{
178282
Name: fmt.Sprintf("zoobc_blockchain_status_%d", chainType),
@@ -187,6 +291,10 @@ func SetBlockchainSmithTime(chainType int32, newTime int64) {
187291
if !isMonitoringActive {
188292
return
189293
}
294+
295+
blockchainSmithTimeSync.Lock()
296+
defer blockchainSmithTimeSync.Unlock()
297+
190298
if blockchainSmithTime[chainType] == nil {
191299
blockchainSmithTime[chainType] = prometheus.NewGauge(prometheus.GaugeOpts{
192300
Name: fmt.Sprintf("zoobc_blockchain_%d_smith_time", chainType),
@@ -201,6 +309,10 @@ func SetNodeScore(activeBlocksmiths []*model.Blocksmith) {
201309
if !isMonitoringActive {
202310
return
203311
}
312+
313+
nodeScoreSync.Lock()
314+
defer nodeScoreSync.Unlock()
315+
204316
if nodeScore == nil {
205317
nodeScore = prometheus.NewGauge(prometheus.GaugeOpts{
206318
Name: "zoobc_node_score",
@@ -225,6 +337,9 @@ func SetLastBlock(chainType int32, block *model.Block) {
225337
return
226338
}
227339

340+
blockchainHeightSync.Lock()
341+
defer blockchainHeightSync.Unlock()
342+
228343
if blockchainHeight[chainType] == nil {
229344
idMsbMetrics := prometheus.NewGauge(prometheus.GaugeOpts{
230345
Name: fmt.Sprintf("zoobc_blockchain_id_%d_msb", chainType),
@@ -252,3 +367,42 @@ func SetLastBlock(chainType int32, block *model.Block) {
252367
blockchainHeight[chainType].IDLsb.Set(math.Abs(float64(block.GetID() % int64(1000000000))))
253368
blockchainHeight[chainType].Height.Set(float64(block.GetHeight()))
254369
}
370+
371+
func IncrementGoRoutineActivity(activityName string) {
372+
if !isMonitoringActive {
373+
return
374+
}
375+
376+
goRoutineActivityCountersSync.Lock()
377+
defer goRoutineActivityCountersSync.Unlock()
378+
379+
if goRoutineActivityCounters[activityName] == nil {
380+
goRoutineActivityCounters[activityName] = prometheus.NewGauge(prometheus.GaugeOpts{
381+
Name: fmt.Sprintf("zoobc_routines_counter_%s", activityName),
382+
Help: fmt.Sprintf("Go routine counter for %s", activityName),
383+
})
384+
prometheus.MustRegister(goRoutineActivityCounters[activityName])
385+
}
386+
goRoutineActivityCounters[activityName].Inc()
387+
}
388+
389+
func DecrementGoRoutineActivity(activityName string) {
390+
if !isMonitoringActive {
391+
return
392+
}
393+
394+
goRoutineActivityCountersSync.Lock()
395+
defer goRoutineActivityCountersSync.Unlock()
396+
397+
if goRoutineActivityCounters[activityName] == nil {
398+
goRoutineActivityCounters[activityName] = prometheus.NewGauge(prometheus.GaugeOpts{
399+
Name: fmt.Sprintf("zoobc_routines_counter_%s", activityName),
400+
Help: fmt.Sprintf("Go routine counter for %s", activityName),
401+
})
402+
prometheus.MustRegister(goRoutineActivityCounters[activityName])
403+
404+
// to avoid below as the initial value, on creation on decrement, we exit
405+
return
406+
}
407+
goRoutineActivityCounters[activityName].Dec()
408+
}

p2p/client/peerServiceClient.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ func (psc *PeerServiceClient) getDefaultContext(requestTimeOut time.Duration) (c
174174

175175
// GetPeerInfo to get Peer info
176176
func (psc *PeerServiceClient) GetPeerInfo(destPeer *model.Peer) (*model.Node, error) {
177+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetPeerInfoClient)
178+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetPeerInfoClient)
179+
177180
// add a copy to avoid pointer delete
178181
connection, err := psc.GetConnection(destPeer)
179182
if err != nil {
@@ -202,6 +205,9 @@ func (psc *PeerServiceClient) GetPeerInfo(destPeer *model.Peer) (*model.Node, er
202205

203206
// GetMorePeers to collect more peers available
204207
func (psc *PeerServiceClient) GetMorePeers(destPeer *model.Peer) (*model.GetMorePeersResponse, error) {
208+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetMorePeersClient)
209+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetMorePeersClient)
210+
205211
connection, err := psc.GetConnection(destPeer)
206212
if err != nil {
207213
return nil, err
@@ -224,6 +230,9 @@ func (psc *PeerServiceClient) GetMorePeers(destPeer *model.Peer) (*model.GetMore
224230

225231
// SendPeers sends set of peers to other node (to populate the network)
226232
func (psc *PeerServiceClient) SendPeers(destPeer *model.Peer, peersInfo []*model.Node) (*model.Empty, error) {
233+
monitoring.IncrementGoRoutineActivity(monitoring.P2pSendPeersClient)
234+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pSendPeersClient)
235+
227236
connection, err := psc.GetConnection(destPeer)
228237
if err != nil {
229238
return nil, err
@@ -250,6 +259,9 @@ func (psc *PeerServiceClient) SendBlock(
250259
block *model.Block,
251260
chainType chaintype.ChainType,
252261
) error {
262+
monitoring.IncrementGoRoutineActivity(monitoring.P2pSendBlockClient)
263+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pSendBlockClient)
264+
253265
connection, err := psc.GetConnection(destPeer)
254266
if err != nil {
255267
return err
@@ -288,6 +300,9 @@ func (psc *PeerServiceClient) SendTransaction(
288300
transactionBytes []byte,
289301
chainType chaintype.ChainType,
290302
) error {
303+
monitoring.IncrementGoRoutineActivity(monitoring.P2pSendTransactionClient)
304+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pSendTransactionClient)
305+
291306
connection, err := psc.GetConnection(destPeer)
292307
if err != nil {
293308
return err
@@ -325,6 +340,9 @@ func (psc *PeerServiceClient) GetCumulativeDifficulty(
325340
destPeer *model.Peer,
326341
chaintype chaintype.ChainType,
327342
) (*model.GetCumulativeDifficultyResponse, error) {
343+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetCumulativeDifficultyClient)
344+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetCumulativeDifficultyClient)
345+
328346
connection, err := psc.GetConnection(destPeer)
329347
if err != nil {
330348
return nil, err
@@ -353,6 +371,9 @@ func (psc *PeerServiceClient) GetCommonMilestoneBlockIDs(
353371
chaintype chaintype.ChainType,
354372
lastBlockID, lastMilestoneBlockID int64,
355373
) (*model.GetCommonMilestoneBlockIdsResponse, error) {
374+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetCommonMilestoneBlockIDsClient)
375+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetCommonMilestoneBlockIDsClient)
376+
356377
connection, err := psc.GetConnection(destPeer)
357378
if err != nil {
358379
return nil, err
@@ -384,6 +405,9 @@ func (psc *PeerServiceClient) GetNextBlockIDs(
384405
blockID int64,
385406
limit uint32,
386407
) (*model.BlockIdsResponse, error) {
408+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetNextBlockIDsClient)
409+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetNextBlockIDsClient)
410+
387411
connection, err := psc.GetConnection(destPeer)
388412
if err != nil {
389413
return nil, err
@@ -415,6 +439,9 @@ func (psc *PeerServiceClient) GetNextBlocks(
415439
blockIds []int64,
416440
blockID int64,
417441
) (*model.BlocksData, error) {
442+
monitoring.IncrementGoRoutineActivity(monitoring.P2pGetNextBlocksClient)
443+
defer monitoring.DecrementGoRoutineActivity(monitoring.P2pGetNextBlocksClient)
444+
418445
connection, err := psc.GetConnection(destPeer)
419446
if err != nil {
420447
return nil, err

0 commit comments

Comments
 (0)