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 { ...@@ -29,7 +29,7 @@ func MetricsCollectionOption(handlerName string) ServeOption {
var ( var (
peersTotalMetric = prometheus.NewDesc( peersTotalMetric = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "p2p", "peers_total"), prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
"Number of connected peers", nil, nil) "Number of connected peers", []string{"transport"}, nil)
) )
type IpfsNodeCollector struct { type IpfsNodeCollector struct {
...@@ -41,13 +41,24 @@ func (_ IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) { ...@@ -41,13 +41,24 @@ func (_ IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) {
} }
func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) { func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) {
for tr, val := range c.PeersTotalValues() {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
peersTotalMetric, peersTotalMetric,
prometheus.GaugeValue, prometheus.GaugeValue,
c.PeersTotalValue(), val,
tr,
) )
}
} }
func (c IpfsNodeCollector) PeersTotalValue() float64 { func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 {
return float64(len(c.Node.PeerHost.Network().Conns())) 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) { ...@@ -39,8 +39,11 @@ func TestPeersTotal(t *testing.T) {
node := &core.IpfsNode{PeerHost: hosts[0]} node := &core.IpfsNode{PeerHost: hosts[0]}
collector := IpfsNodeCollector{Node: node} collector := IpfsNodeCollector{Node: node}
actual := collector.PeersTotalValue() actual := collector.PeersTotalValues()
if actual != 3 { if len(actual) != 1 {
t.Fatalf("expected 3 peers, got %d", int(actual)) 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