Skip to content

Commit

Permalink
metrics: add transport label to p2p_peers_total
Browse files Browse the repository at this point in the history
Gives us per-transport peers counts:

	ipfs_p2p_peers_total{transport="/ip4/tcp"} 25
	ipfs_p2p_peers_total{transport="/ip6/tcp"} 13
	ipfs_p2p_peers_total{transport="/ip6/udp/utp"} 17

License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed May 18, 2016
1 parent 41c5e11 commit 91e141f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 19 additions & 8 deletions core/corehttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 := 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
}
9 changes: 6 additions & 3 deletions core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
}

0 comments on commit 91e141f

Please sign in to comment.