Browse Source

swarm: export ipfs_p2p_peers_total metric

License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
pull/2/head
Lars Gierth 10 years ago
parent
commit
910ebfe9a5
  1. 31
      net/swarm/swarm.go

31
net/swarm/swarm.go

@ -19,6 +19,7 @@ import (
ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
@ -26,6 +27,13 @@ var log = eventlog.Logger("swarm2")
var PSTransport pst.Transport
var peersTotal = prom.NewGaugeVec(prom.GaugeOpts{
Namespace: "ipfs",
Subsystem: "p2p",
Name: "peers_total",
Help: "Number of connected peers",
}, []string{"peer_id"})
func init() {
tpt := *psy.DefaultTransport
tpt.MaxStreamWindowSize = 512 * 1024
@ -82,6 +90,10 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
s.cg.SetTeardown(s.teardown)
s.SetConnHandler(nil) // make sure to setup our own conn handler.
// setup swarm metrics
prom.MustRegisterOrGet(peersTotal)
s.Notify((*metricsNotifiee)(s))
return s, s.listen(listenAddrs)
}
@ -273,3 +285,22 @@ func (n *ps2netNotifee) OpenedStream(s *ps.Stream) {
func (n *ps2netNotifee) ClosedStream(s *ps.Stream) {
n.not.ClosedStream(n.net, inet.Stream((*Stream)(s)))
}
type metricsNotifiee Swarm
func (nn *metricsNotifiee) Connected(n inet.Network, v inet.Conn) {
peersTotalGauge(n.LocalPeer()).Inc()
}
func (nn *metricsNotifiee) Disconnected(n inet.Network, v inet.Conn) {
peersTotalGauge(n.LocalPeer()).Dec()
}
func (nn *metricsNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
func (nn *metricsNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
func (nn *metricsNotifiee) Listen(n inet.Network, a ma.Multiaddr) {}
func (nn *metricsNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}
func peersTotalGauge(id peer.ID) prom.Gauge {
return peersTotal.With(prom.Labels{"peer_id": id.Pretty()})
}

Loading…
Cancel
Save