48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
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
|
|
}
|