Browse Source

Wrap conn with metrics (#2131)

release-v0260
Marco Munizaga 2 years ago
committed by GitHub
parent
commit
90aedbddaa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      p2p/net/swarm/swarm.go
  2. 4
      p2p/net/swarm/swarm_conn.go
  3. 6
      p2p/net/swarm/swarm_dial.go
  4. 2
      p2p/net/swarm/swarm_listen.go

22
p2p/net/swarm/swarm.go

@ -641,3 +641,25 @@ func (s *Swarm) ResourceManager() network.ResourceManager {
// Swarm is a Network.
var _ network.Network = (*Swarm)(nil)
var _ transport.TransportNetwork = (*Swarm)(nil)
type connWithMetrics struct {
transport.CapableConn
opened time.Time
dir network.Direction
metricsTracer MetricsTracer
}
func wrapWithMetrics(capableConn transport.CapableConn, metricsTracer MetricsTracer, opened time.Time, dir network.Direction) connWithMetrics {
c := connWithMetrics{CapableConn: capableConn, opened: opened, dir: dir, metricsTracer: metricsTracer}
c.metricsTracer.OpenedConnection(c.dir, capableConn.RemotePublicKey(), capableConn.ConnState(), capableConn.LocalMultiaddr())
return c
}
func (c connWithMetrics) completedHandshake() {
c.metricsTracer.CompletedHandshake(time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
}
func (c connWithMetrics) Close() error {
c.metricsTracer.ClosedConnection(c.dir, time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
return c.CapableConn.Close()
}

4
p2p/net/swarm/swarm_conn.go

@ -60,10 +60,6 @@ func (c *Conn) Close() error {
}
func (c *Conn) doClose() {
if c.swarm.metricsTracer != nil {
c.swarm.metricsTracer.ClosedConnection(c.stat.Direction, time.Since(c.stat.Stats.Opened), c.ConnState(), c.LocalMultiaddr())
}
c.swarm.removeConn(c)
// Prevent new streams from opening.

6
p2p/net/swarm/swarm_dial.go

@ -499,9 +499,9 @@ func (s *Swarm) dialAddr(ctx context.Context, p peer.ID, addr ma.Multiaddr) (tra
}
canonicallog.LogPeerStatus(100, connC.RemotePeer(), connC.RemoteMultiaddr(), "connection_status", "established", "dir", "outbound")
if s.metricsTracer != nil {
connState := connC.ConnState()
s.metricsTracer.OpenedConnection(network.DirOutbound, connC.RemotePublicKey(), connState, connC.LocalMultiaddr())
s.metricsTracer.CompletedHandshake(time.Since(start), connState, connC.LocalMultiaddr())
connWithMetrics := wrapWithMetrics(connC, s.metricsTracer, start, network.DirOutbound)
connWithMetrics.completedHandshake()
connC = connWithMetrics
}
// Trust the transport? Yeah... right.

2
p2p/net/swarm/swarm_listen.go

@ -131,7 +131,7 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
}
canonicallog.LogPeerStatus(100, c.RemotePeer(), c.RemoteMultiaddr(), "connection_status", "established", "dir", "inbound")
if s.metricsTracer != nil {
s.metricsTracer.OpenedConnection(network.DirInbound, c.RemotePublicKey(), c.ConnState(), c.LocalMultiaddr())
c = wrapWithMetrics(c, s.metricsTracer, time.Now(), network.DirOutbound)
}
log.Debugf("swarm listener accepted connection: %s <-> %s", c.LocalMultiaddr(), c.RemoteMultiaddr())

Loading…
Cancel
Save