status-go/metrics/node/metrics.go
Jakub Sokołowski 714eb416d2 metrics: fix p2p_peers_count metrics labels
Because `p2p.Server.Name()` function started returning an abbreviated
name of the node this broke the parsing of the name in order to fill in
the labels for `p2p_peers_count` metric, resulting in metrics like this:
```sh
 > curl -sS localhost:9090/metrics | grep '^p2p_peers_count'
p2p_peers_count{platform="v0.79.0",type="Statusd",version="unknown"} 3
```
Caused by value returned from `Name()` to look like this:
```
Statusd/v0.79.0/linu...
```
By using `Fullname()` we are sure we are pasing all the segments.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
2021-08-24 11:07:39 +02:00

80 lines
1.8 KiB
Go

package node
import (
"errors"
"strings"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
prom "github.com/prometheus/client_golang/prometheus"
)
var (
nodePeersGauge = prom.NewGaugeVec(prom.GaugeOpts{
Name: "p2p_peers_count",
Help: "Current numbers of peers split by name.",
}, []string{"type", "version", "platform"})
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.",
})
)
func init() {
prom.MustRegister(nodePeersGauge)
prom.MustRegister(nodePeersAbsolute)
prom.MustRegister(nodeMaxPeersGauge)
}
func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
server := node.Server()
if server == nil {
return errors.New("p2p server is unavailable")
}
calculatePeerCounts(server)
nodePeersAbsolute.Set(float64(server.PeerCount()))
nodeMaxPeersGauge.Set(float64(server.MaxPeers))
return nil
}
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")
}
}
func calculatePeerCounts(server *p2p.Server) {
peers := server.Peers()
/* necessary to count all peers anew */
nodePeersGauge.Reset()
for _, p := range peers {
labels, err := labelsFromNodeName(p.Fullname())
if err != nil {
logger.Warn("failed parsing peer name", "error", err, "name", p.Name())
continue
}
nodePeersGauge.With(labels).Inc()
}
}