-
Notifications
You must be signed in to change notification settings - Fork 244
/
metrics.go
47 lines (38 loc) · 1.11 KB
/
metrics.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
package node
import (
"errors"
"flag"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
)
var (
nodePeersCounter metrics.Counter
nodePeersGauge metrics.Gauge
nodeMaxPeersGauge metrics.Gauge
)
func init() {
// When running tests, we want metrics to be enabled.
// Having init() in metrics_test.go does not work because
// this init() is executed first.
if flag.Lookup("test.v") != nil {
metrics.Enabled = true
}
nodePeersCounter = metrics.NewRegisteredCounter("p2p/Peers", nil)
nodePeersGauge = metrics.NewRegisteredGauge("p2p/PeersAbsolute", nil)
nodeMaxPeersGauge = metrics.NewRegisteredGauge("p2p/MaxPeers", nil)
}
func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
server := node.Server()
if server == nil {
return errors.New("p2p server is unavailable")
}
if evType == p2p.PeerEventTypeAdd {
nodePeersCounter.Inc(1)
} else if evType == p2p.PeerEventTypeDrop {
nodePeersCounter.Dec(1)
}
nodePeersGauge.Update(int64(server.PeerCount()))
nodeMaxPeersGauge.Update(int64(server.MaxPeers))
return nil
}