Commit f0146103 authored by Jeromy Johnson's avatar Jeromy Johnson

Merge pull request #2728 from ipfs/peers-total-by-transport

metrics: add transport label to p2p_peers_total
parents cc822af1 05cb7a1b
......@@ -29,7 +29,7 @@ func MetricsCollectionOption(handlerName string) ServeOption {
var (
peersTotalMetric = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
"Number of connected peers", nil, nil)
"Number of connected peers", []string{"transport"}, nil)
)
type IpfsNodeCollector struct {
......@@ -41,13 +41,24 @@ func (_ IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) {
}
func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
peersTotalMetric,
prometheus.GaugeValue,
c.PeersTotalValue(),
)
for tr, val := range c.PeersTotalValues() {
ch <- prometheus.MustNewConstMetric(
peersTotalMetric,
prometheus.GaugeValue,
val,
tr,
)
}
}
func (c IpfsNodeCollector) PeersTotalValue() float64 {
return float64(len(c.Node.PeerHost.Network().Conns()))
func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 {
vals := make(map[string]float64)
for _, conn := range c.Node.PeerHost.Network().Conns() {
tr := ""
for _, proto := range conn.RemoteMultiaddr().Protocols() {
tr = tr + "/" + proto.Name
}
vals[tr] = vals[tr] + 1
}
return vals
}
......@@ -39,8 +39,11 @@ func TestPeersTotal(t *testing.T) {
node := &core.IpfsNode{PeerHost: hosts[0]}
collector := IpfsNodeCollector{Node: node}
actual := collector.PeersTotalValue()
if actual != 3 {
t.Fatalf("expected 3 peers, got %d", int(actual))
actual := collector.PeersTotalValues()
if len(actual) != 1 {
t.Fatalf("expected 1 peers transport, got %d", len(actual))
}
if actual["/ip4/tcp"] != float64(3) {
t.Fatalf("expected 3 peers, got %s", actual["/ip4/tcp"])
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment