-
Notifications
You must be signed in to change notification settings - Fork 199
/
metricsUpdater.go
191 lines (158 loc) · 6.05 KB
/
metricsUpdater.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package status
import (
"context"
"fmt"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/atomic"
"github.com/ElrondNetwork/elrond-go-core/core/check"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/heartbeat"
"github.com/ElrondNetwork/elrond-go/storage"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
const minDuration = time.Second
var log = logger.GetOrCreate("heartbeat/status")
// ArgsMetricsUpdater represents the arguments for the metricsUpdater constructor
type ArgsMetricsUpdater struct {
PeerAuthenticationCacher storage.Cacher
HeartbeatMonitor HeartbeatMonitor
HeartbeatSenderInfoProvider HeartbeatSenderInfoProvider
AppStatusHandler core.AppStatusHandler
TimeBetweenConnectionsMetricsUpdate time.Duration
EpochNotifier vmcommon.EpochNotifier
HeartbeatV1DisableEpoch uint32
}
type metricsUpdater struct {
peerAuthenticationCacher storage.Cacher
heartbeatMonitor HeartbeatMonitor
heartbeatSenderInfoProvider HeartbeatSenderInfoProvider
appStatusHandler core.AppStatusHandler
timeBetweenConnectionsMetricsUpdate time.Duration
cancelFunc func()
flagHeartbeatV1DisableEpoch atomic.Flag
heartbeatV1DisableEpoch uint32
}
// NewMetricsUpdater creates a new instance of type metricsUpdater
func NewMetricsUpdater(args ArgsMetricsUpdater) (*metricsUpdater, error) {
err := checkArgs(args)
if err != nil {
return nil, err
}
updater := &metricsUpdater{
peerAuthenticationCacher: args.PeerAuthenticationCacher,
heartbeatMonitor: args.HeartbeatMonitor,
heartbeatSenderInfoProvider: args.HeartbeatSenderInfoProvider,
appStatusHandler: args.AppStatusHandler,
timeBetweenConnectionsMetricsUpdate: args.TimeBetweenConnectionsMetricsUpdate,
heartbeatV1DisableEpoch: args.HeartbeatV1DisableEpoch,
}
args.EpochNotifier.RegisterNotifyHandler(updater)
args.PeerAuthenticationCacher.RegisterHandler(updater.onAddedPeerAuthenticationMessage, "metricsUpdater")
var ctx context.Context
ctx, updater.cancelFunc = context.WithCancel(context.Background())
go updater.processMetricsUpdate(ctx)
return updater, nil
}
func checkArgs(args ArgsMetricsUpdater) error {
if check.IfNil(args.PeerAuthenticationCacher) {
return heartbeat.ErrNilCacher
}
if check.IfNil(args.AppStatusHandler) {
return heartbeat.ErrNilAppStatusHandler
}
if check.IfNil(args.HeartbeatMonitor) {
return heartbeat.ErrNilHeartbeatMonitor
}
if check.IfNil(args.HeartbeatSenderInfoProvider) {
return heartbeat.ErrNilHeartbeatSenderInfoProvider
}
if args.TimeBetweenConnectionsMetricsUpdate < minDuration {
return fmt.Errorf("%w on TimeBetweenConnectionsMetricsUpdate, provided %d, min expected %d",
heartbeat.ErrInvalidTimeDuration, args.TimeBetweenConnectionsMetricsUpdate, minDuration)
}
if check.IfNil(args.EpochNotifier) {
return heartbeat.ErrNilEpochNotifier
}
return nil
}
func (updater *metricsUpdater) processMetricsUpdate(ctx context.Context) {
timer := time.NewTimer(updater.timeBetweenConnectionsMetricsUpdate)
defer timer.Stop()
for {
timer.Reset(updater.timeBetweenConnectionsMetricsUpdate)
select {
case <-timer.C:
updater.updateMetrics()
case <-ctx.Done():
log.Debug("closing heartbeat v2 metricsUpdater go routine")
return
}
}
}
func (updater *metricsUpdater) updateMetrics() {
if updater.shouldSkipUpdateMetrics() {
return
}
updater.updateConnectionsMetrics()
updater.updateSenderMetrics()
}
func (updater *metricsUpdater) updateConnectionsMetrics() {
heartbeats := updater.heartbeatMonitor.GetHeartbeats()
counterActiveValidators := 0
counterConnectedNodes := 0
for _, heartbeatMessage := range heartbeats {
if heartbeatMessage.IsActive {
counterConnectedNodes++
if isValidator(heartbeatMessage.PeerType) {
counterActiveValidators++
}
}
}
updater.appStatusHandler.SetUInt64Value(common.MetricNumIntraShardValidatorNodes, uint64(counterActiveValidators))
updater.appStatusHandler.SetUInt64Value(common.MetricConnectedNodes, uint64(counterConnectedNodes))
}
func (updater *metricsUpdater) updateSenderMetrics() {
result, subType, err := updater.heartbeatSenderInfoProvider.GetSenderInfo()
if err != nil {
log.Warn("error while updating metrics in heartbeat v2 metricsUpdater", "error", err)
return
}
nodeType := ""
if result == string(common.ObserverList) {
nodeType = string(core.NodeTypeObserver)
} else {
nodeType = string(core.NodeTypeValidator)
}
updater.appStatusHandler.SetStringValue(common.MetricNodeType, nodeType)
updater.appStatusHandler.SetStringValue(common.MetricPeerType, result)
updater.appStatusHandler.SetStringValue(common.MetricPeerSubType, subType.String())
}
func isValidator(peerType string) bool {
return peerType == string(common.EligibleList) || peerType == string(common.WaitingList)
}
// Close closes the internal go routine
func (updater *metricsUpdater) Close() error {
updater.cancelFunc()
return nil
}
// EpochConfirmed is called whenever an epoch is confirmed
func (updater *metricsUpdater) EpochConfirmed(epoch uint32, _ uint64) {
updater.flagHeartbeatV1DisableEpoch.SetValue(epoch >= updater.heartbeatV1DisableEpoch)
log.Debug("heartbeat v1 subsystem", "enabled", !updater.flagHeartbeatV1DisableEpoch.IsSet())
}
func (updater *metricsUpdater) onAddedPeerAuthenticationMessage(_ []byte, _ interface{}) {
if updater.shouldSkipUpdateMetrics() {
return
}
updater.appStatusHandler.SetUInt64Value(common.MetricLiveValidatorNodes, uint64(updater.peerAuthenticationCacher.Len()))
}
func (updater *metricsUpdater) shouldSkipUpdateMetrics() bool {
heartbeatV1IsStillActive := !updater.flagHeartbeatV1DisableEpoch.IsSet()
return heartbeatV1IsStillActive
}
// IsInterfaceNil returns true if there is no value under the interface
func (updater *metricsUpdater) IsInterfaceNil() bool {
return updater == nil
}