2018-05-16 15:36:59 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-22 10:26:03 +00:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
|
2018-05-16 15:36:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2018-05-22 10:26:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-05-16 15:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-22 10:26:03 +00:00
|
|
|
nodePeersCounter metrics.Counter
|
|
|
|
nodeMaxPeersGauge metrics.Gauge
|
2018-05-16 15:36:59 +00:00
|
|
|
)
|
|
|
|
|
2018-05-22 10:26:03 +00:00
|
|
|
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)
|
|
|
|
nodeMaxPeersGauge = metrics.NewRegisteredGauge("p2p/MaxPeers", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
|
2018-05-16 15:36:59 +00:00
|
|
|
server := node.Server()
|
|
|
|
if server == nil {
|
2018-05-22 10:26:03 +00:00
|
|
|
return errors.New("p2p server is unavailable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if evType == p2p.PeerEventTypeAdd {
|
|
|
|
nodePeersCounter.Inc(1)
|
|
|
|
} else if evType == p2p.PeerEventTypeDrop {
|
|
|
|
nodePeersCounter.Dec(1)
|
2018-05-16 15:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeMaxPeersGauge.Update(int64(server.MaxPeers))
|
2018-05-22 10:26:03 +00:00
|
|
|
|
|
|
|
return nil
|
2018-05-16 15:36:59 +00:00
|
|
|
}
|