2018-05-16 15:36:59 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-22 10:26:03 +00:00
|
|
|
"errors"
|
2019-10-22 17:32:05 +00:00
|
|
|
"strings"
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2018-05-16 15:36:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2018-05-22 10:26:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-10-22 17:32:05 +00:00
|
|
|
|
|
|
|
prom "github.com/prometheus/client_golang/prometheus"
|
2018-05-16 15:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-10-22 17:32:05 +00:00
|
|
|
nodePeersGauge = prom.NewGaugeVec(prom.GaugeOpts{
|
|
|
|
Name: "p2p_peers_count",
|
|
|
|
Help: "Current numbers of peers split by name.",
|
|
|
|
}, []string{"type", "version", "platform"})
|
|
|
|
nodePeersChanges = prom.NewGauge(prom.GaugeOpts{
|
|
|
|
Name: "p2p_peers_changes",
|
|
|
|
Help: "Current changes to connected peers.",
|
|
|
|
})
|
|
|
|
nodePeersAbsolute = prom.NewGauge(prom.GaugeOpts{
|
|
|
|
Name: "p2p_peers_absolute",
|
|
|
|
Help: "Absolute number of connected peers.",
|
|
|
|
})
|
|
|
|
nodeMaxPeersGauge = prom.NewGauge(prom.GaugeOpts{
|
|
|
|
Name: "p2p_peers_max",
|
|
|
|
Help: "Maximum number of peers that can connect.",
|
|
|
|
})
|
2018-05-16 15:36:59 +00:00
|
|
|
)
|
|
|
|
|
2018-05-22 10:26:03 +00:00
|
|
|
func init() {
|
2019-10-22 17:32:05 +00:00
|
|
|
prom.MustRegister(nodePeersGauge)
|
|
|
|
prom.MustRegister(nodePeersChanges)
|
|
|
|
prom.MustRegister(nodePeersAbsolute)
|
|
|
|
prom.MustRegister(nodeMaxPeersGauge)
|
2018-05-22 10:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
|
2019-10-22 17:32:05 +00:00
|
|
|
server := node.Server()
|
|
|
|
if server == nil {
|
|
|
|
return errors.New("p2p server is unavailable")
|
2019-10-04 15:21:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
calculatePeerCounts(server)
|
|
|
|
|
|
|
|
change, err := computeMetrics(server, evType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-04 15:21:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
nodePeersChanges.Add(change.Counter)
|
|
|
|
nodePeersAbsolute.Set(change.Absolute)
|
|
|
|
nodeMaxPeersGauge.Set(change.Max)
|
2019-10-04 15:21:24 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type peersChange struct {
|
2019-10-22 17:32:05 +00:00
|
|
|
Counter float64
|
|
|
|
Absolute float64
|
|
|
|
Max float64
|
2019-10-04 15:21:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
func labelsFromNodeName(name string) (prom.Labels, error) {
|
|
|
|
tokens := strings.Split(name, "/")
|
|
|
|
if len(tokens) == 4 {
|
|
|
|
return prom.Labels{
|
|
|
|
"type": tokens[0],
|
|
|
|
"version": tokens[1],
|
|
|
|
"platform": tokens[2],
|
|
|
|
}, nil
|
|
|
|
} else if len(tokens) == 3 {
|
|
|
|
return prom.Labels{
|
|
|
|
"type": tokens[0],
|
|
|
|
"version": "unknown",
|
|
|
|
"platform": tokens[1],
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("wrong number of segments in name")
|
2018-05-22 10:26:03 +00:00
|
|
|
}
|
2019-10-22 17:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func calculatePeerCounts(server *p2p.Server) {
|
|
|
|
peers := server.Peers()
|
|
|
|
/* necessary to count all peers anew */
|
|
|
|
nodePeersGauge.Reset()
|
|
|
|
|
|
|
|
for _, p := range peers {
|
|
|
|
labels, err := labelsFromNodeName(p.Name())
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed parsing peer name", "error", err, "name", p.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodePeersGauge.With(labels).Inc()
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 10:26:03 +00:00
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
func computeMetrics(server *p2p.Server, evType p2p.PeerEventType) (result peersChange, err error) {
|
|
|
|
switch evType {
|
|
|
|
case p2p.PeerEventTypeAdd:
|
2019-10-04 15:21:24 +00:00
|
|
|
result.Counter = 1
|
2019-10-22 17:32:05 +00:00
|
|
|
case p2p.PeerEventTypeDrop:
|
2019-10-04 15:21:24 +00:00
|
|
|
result.Counter = -1
|
2018-05-16 15:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:32:05 +00:00
|
|
|
result.Absolute = float64(server.PeerCount())
|
|
|
|
result.Max = float64(server.MaxPeers)
|
2019-10-04 15:21:24 +00:00
|
|
|
return
|
2018-05-16 15:36:59 +00:00
|
|
|
}
|