4
4
"fmt"
5
5
"math"
6
6
"reflect"
7
+ "sync"
7
8
8
9
"github.com/prometheus/client_golang/prometheus"
9
10
"github.com/zoobc/zoobc-core/common/model"
@@ -16,20 +17,71 @@ type lastblockMetrics struct {
16
17
}
17
18
18
19
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
+
26
38
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"
33
85
)
34
86
35
87
func SetMonitoringActive (isActive bool ) {
@@ -45,6 +97,12 @@ func IsMonitoringActive() bool {
45
97
}
46
98
47
99
func IncrementReceiptCounter () {
100
+ if ! isMonitoringActive {
101
+ return
102
+ }
103
+
104
+ receiptCounterSync .Lock ()
105
+ defer receiptCounterSync .Unlock ()
48
106
if receiptCounter == nil {
49
107
receiptCounter = prometheus .NewCounter (prometheus.CounterOpts {
50
108
Name : fmt .Sprintf ("zoobc_receipts" ),
@@ -57,6 +115,13 @@ func IncrementReceiptCounter() {
57
115
}
58
116
59
117
func SetUnresolvedPeersCount (count int ) {
118
+ if ! isMonitoringActive {
119
+ return
120
+ }
121
+
122
+ unresolvedPeersCounterSync .Lock ()
123
+ defer unresolvedPeersCounterSync .Unlock ()
124
+
60
125
if unresolvedPeersCounter == nil {
61
126
unresolvedPeersCounter = prometheus .NewGauge (prometheus.GaugeOpts {
62
127
Name : fmt .Sprintf ("zoobc_unresolved_peers" ),
@@ -69,6 +134,13 @@ func SetUnresolvedPeersCount(count int) {
69
134
}
70
135
71
136
func SetResolvedPeersCount (count int ) {
137
+ if ! isMonitoringActive {
138
+ return
139
+ }
140
+
141
+ resolvedPeersCounterSync .Lock ()
142
+ defer resolvedPeersCounterSync .Unlock ()
143
+
72
144
if resolvedPeersCounter == nil {
73
145
resolvedPeersCounter = prometheus .NewGauge (prometheus.GaugeOpts {
74
146
Name : fmt .Sprintf ("zoobc_resolved_peers" ),
@@ -81,6 +153,13 @@ func SetResolvedPeersCount(count int) {
81
153
}
82
154
83
155
func SetResolvedPriorityPeersCount (count int ) {
156
+ if ! isMonitoringActive {
157
+ return
158
+ }
159
+
160
+ resolvedPriorityPeersCounterSync .Lock ()
161
+ defer resolvedPriorityPeersCounterSync .Unlock ()
162
+
84
163
if resolvedPriorityPeersCounter == nil {
85
164
resolvedPriorityPeersCounter = prometheus .NewGauge (prometheus.GaugeOpts {
86
165
Name : fmt .Sprintf ("zoobc_resolved_priority_peers" ),
@@ -93,6 +172,13 @@ func SetResolvedPriorityPeersCount(count int) {
93
172
}
94
173
95
174
func SetUnresolvedPriorityPeersCount (count int ) {
175
+ if ! isMonitoringActive {
176
+ return
177
+ }
178
+
179
+ unresolvedPriorityPeersCounterSync .Lock ()
180
+ defer unresolvedPriorityPeersCounterSync .Unlock ()
181
+
96
182
if unresolvedPriorityPeersCounter == nil {
97
183
unresolvedPriorityPeersCounter = prometheus .NewGauge (prometheus.GaugeOpts {
98
184
Name : fmt .Sprintf ("zoobc_unresolved_priority_peers" ),
@@ -105,6 +191,13 @@ func SetUnresolvedPriorityPeersCount(count int) {
105
191
}
106
192
107
193
func SetActiveRegisteredNodesCount (count int ) {
194
+ if ! isMonitoringActive {
195
+ return
196
+ }
197
+
198
+ activeRegisteredNodesGaugeSync .Lock ()
199
+ defer activeRegisteredNodesGaugeSync .Unlock ()
200
+
108
201
if activeRegisteredNodesGauge == nil {
109
202
activeRegisteredNodesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
110
203
Name : fmt .Sprintf ("zoobc_active_registered_nodes" ),
@@ -121,6 +214,9 @@ func IncrementBlockerMetrics(typeBlocker string) {
121
214
return
122
215
}
123
216
217
+ blockerCounterSync .Lock ()
218
+ defer blockerCounterSync .Unlock ()
219
+
124
220
if blockerCounter [typeBlocker ] == nil {
125
221
blockerCounter [typeBlocker ] = prometheus .NewCounter (prometheus.CounterOpts {
126
222
Name : fmt .Sprintf ("zoobc_err_%s" , typeBlocker ),
@@ -136,6 +232,9 @@ func IncrementStatusLockCounter(typeStatusLock int) {
136
232
return
137
233
}
138
234
235
+ statusLockCounterSync .Lock ()
236
+ defer statusLockCounterSync .Unlock ()
237
+
139
238
if statusLockCounter [typeStatusLock ] == nil {
140
239
statusLockCounter [typeStatusLock ] = prometheus .NewGauge (prometheus.GaugeOpts {
141
240
Name : fmt .Sprintf ("zoobc_status_lock_%d" , typeStatusLock ),
@@ -154,25 +253,30 @@ func DecrementStatusLockCounter(typeStatusLock int) {
154
253
return
155
254
}
156
255
157
- if ! isMonitoringActive {
158
- return
159
- }
256
+ statusLockCounterSync .Lock ()
257
+ defer statusLockCounterSync .Unlock ()
160
258
161
259
if statusLockCounter [typeStatusLock ] == nil {
162
260
statusLockCounter [typeStatusLock ] = prometheus .NewGauge (prometheus.GaugeOpts {
163
261
Name : fmt .Sprintf ("zoobc_status_lock_%d" , typeStatusLock ),
164
262
Help : fmt .Sprintf ("Status lock %d counter" , typeStatusLock ),
165
263
})
166
264
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
169
268
}
269
+ statusLockCounter [typeStatusLock ].Dec ()
170
270
}
171
271
172
272
func SetBlockchainStatus (chainType int32 , newStatus int ) {
173
273
if ! isMonitoringActive {
174
274
return
175
275
}
276
+
277
+ blockchainStatusSync .Lock ()
278
+ defer blockchainStatusSync .Unlock ()
279
+
176
280
if blockchainStatus [chainType ] == nil {
177
281
blockchainStatus [chainType ] = prometheus .NewGauge (prometheus.GaugeOpts {
178
282
Name : fmt .Sprintf ("zoobc_blockchain_status_%d" , chainType ),
@@ -187,6 +291,10 @@ func SetBlockchainSmithTime(chainType int32, newTime int64) {
187
291
if ! isMonitoringActive {
188
292
return
189
293
}
294
+
295
+ blockchainSmithTimeSync .Lock ()
296
+ defer blockchainSmithTimeSync .Unlock ()
297
+
190
298
if blockchainSmithTime [chainType ] == nil {
191
299
blockchainSmithTime [chainType ] = prometheus .NewGauge (prometheus.GaugeOpts {
192
300
Name : fmt .Sprintf ("zoobc_blockchain_%d_smith_time" , chainType ),
@@ -201,6 +309,10 @@ func SetNodeScore(activeBlocksmiths []*model.Blocksmith) {
201
309
if ! isMonitoringActive {
202
310
return
203
311
}
312
+
313
+ nodeScoreSync .Lock ()
314
+ defer nodeScoreSync .Unlock ()
315
+
204
316
if nodeScore == nil {
205
317
nodeScore = prometheus .NewGauge (prometheus.GaugeOpts {
206
318
Name : "zoobc_node_score" ,
@@ -225,6 +337,9 @@ func SetLastBlock(chainType int32, block *model.Block) {
225
337
return
226
338
}
227
339
340
+ blockchainHeightSync .Lock ()
341
+ defer blockchainHeightSync .Unlock ()
342
+
228
343
if blockchainHeight [chainType ] == nil {
229
344
idMsbMetrics := prometheus .NewGauge (prometheus.GaugeOpts {
230
345
Name : fmt .Sprintf ("zoobc_blockchain_id_%d_msb" , chainType ),
@@ -252,3 +367,42 @@ func SetLastBlock(chainType int32, block *model.Block) {
252
367
blockchainHeight [chainType ].IDLsb .Set (math .Abs (float64 (block .GetID () % int64 (1000000000 ))))
253
368
blockchainHeight [chainType ].Height .Set (float64 (block .GetHeight ()))
254
369
}
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
+ }
0 commit comments