@cryptotaxi247 / kubo / commits / 8b164f9e3

swarm: export ipfs_p2p_peers_total metric

License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>

Lars Gierth committed Jun 23, 2015 at 02:11 UTC 8b164f9e39d31085725d55dc7686a7a44bebfe00
1 file changed +31
p2p/net/swarm/swarm.go
+31
@@ -19,6 +19,7 @@ import (
19 ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
20 pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
21 psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
22 + prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
23 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
24 )
25
@@ -26,6 +27,13 @@ var log = eventlog.Logger("swarm2")
27
28 var PSTransport pst.Transport
29
30 +var peersTotal = prom.NewGaugeVec(prom.GaugeOpts{
31 + Namespace: "ipfs",
32 + Subsystem: "p2p",
33 + Name: "peers_total",
34 + Help: "Number of connected peers",
35 +}, []string{"peer_id"})
36 +
37 func init() {
38 tpt := *psy.DefaultTransport
39 tpt.MaxStreamWindowSize = 512 * 1024
@@ -82,6 +90,10 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
90 s.cg.SetTeardown(s.teardown)
91 s.SetConnHandler(nil) // make sure to setup our own conn handler.
92
93 + // setup swarm metrics
94 + prom.MustRegisterOrGet(peersTotal)
95 + s.Notify((*metricsNotifiee)(s))
96 +
97 return s, s.listen(listenAddrs)
98 }
99
@@ -273,3 +285,22 @@ func (n *ps2netNotifee) OpenedStream(s *ps.Stream) {
285 func (n *ps2netNotifee) ClosedStream(s *ps.Stream) {
286 n.not.ClosedStream(n.net, inet.Stream((*Stream)(s)))
287 }
288 +
289 +type metricsNotifiee Swarm
290 +
291 +func (nn *metricsNotifiee) Connected(n inet.Network, v inet.Conn) {
292 + peersTotalGauge(n.LocalPeer()).Inc()
293 +}
294 +
295 +func (nn *metricsNotifiee) Disconnected(n inet.Network, v inet.Conn) {
296 + peersTotalGauge(n.LocalPeer()).Dec()
297 +}
298 +
299 +func (nn *metricsNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
300 +func (nn *metricsNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
301 +func (nn *metricsNotifiee) Listen(n inet.Network, a ma.Multiaddr) {}
302 +func (nn *metricsNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}
303 +
304 +func peersTotalGauge(id peer.ID) prom.Gauge {
305 + return peersTotal.With(prom.Labels{"peer_id": id.Pretty()})
306 +}