Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ func (s *Service) Connect(ctx context.Context, addrs []ma.Multiaddr) (address *b
return nil, fmt.Errorf("libp2p connect: peer %s does not exist %w", overlay, p2p.ErrPeerNotFound)
}

s.metrics.CreatedConnectionCount.Inc()
s.metrics.incCreatedConnection(stream.Conn().RemoteMultiaddr())

if len(peerAddrs) > 0 {
s.notifyReacherConnected(overlay, peerAddrs)
Expand Down Expand Up @@ -1572,8 +1572,8 @@ type connectionNotifier struct {
network.Notifiee
}

func (c *connectionNotifier) Connected(_ network.Network, _ network.Conn) {
c.metrics.HandledConnectionCount.Inc()
func (c *connectionNotifier) Connected(_ network.Network, conn network.Conn) {
c.metrics.observeHandledConnection(conn.RemoteMultiaddr())
}

// isNetworkOrHostUnreachableError determines based on the
Expand Down
98 changes: 84 additions & 14 deletions pkg/p2p/libp2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
package libp2p

import (
"github.com/ethersphere/bee/v2/pkg/bzz"
m "github.com/ethersphere/bee/v2/pkg/metrics"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/prometheus/client_golang/prometheus"
)

const (
connectionTransportLabelName = "transport"
connectionTransportHelp = "The 'transport' label is one of: tcp, ws, wss, quic-v1, quic, unknown."
)

type metrics struct {
// all metrics fields must be exported
// to be able to return them by Metrics()
// using reflection
CreatedConnectionCount prometheus.Counter
HandledConnectionCount prometheus.Counter
CreatedConnectionCount *prometheus.CounterVec
HandledConnectionCount *prometheus.CounterVec
PublicAddressConnections *prometheus.CounterVec
PrivateAddressConnections *prometheus.CounterVec
CreatedStreamCount prometheus.Counter
ClosedStreamCount prometheus.Counter
StreamResetCount prometheus.Counter
Expand All @@ -33,18 +43,42 @@ func newMetrics() metrics {
subsystem := "libp2p"

return metrics{
CreatedConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "created_connection_count",
Help: "Number of initiated outgoing libp2p connections.",
}),
HandledConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "handled_connection_count",
Help: "Number of handled incoming libp2p connections.",
}),
CreatedConnectionCount: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "created_connection_count",
Help: "Number of initiated outgoing libp2p connections. " + connectionTransportHelp,
},
[]string{connectionTransportLabelName},
),
HandledConnectionCount: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "handled_connection_count",
Help: "Number of handled incoming libp2p connections. " + connectionTransportHelp,
},
[]string{connectionTransportLabelName},
),
PublicAddressConnections: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "public_address_connections_total",
Help: "Number of libp2p connections whose remote multiaddr is a public address. " + connectionTransportHelp,
},
[]string{connectionTransportLabelName},
),
PrivateAddressConnections: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "private_address_connections_total",
Help: "Number of libp2p connections whose remote multiaddr is a private address. " + connectionTransportHelp,
},
[]string{connectionTransportLabelName},
),
CreatedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Expand Down Expand Up @@ -120,6 +154,42 @@ func newMetrics() metrics {
}
}

func (m metrics) incCreatedConnection(addr ma.Multiaddr) {
m.CreatedConnectionCount.WithLabelValues(connectionTransportLabel(addr)).Inc()
}

func (m metrics) observeHandledConnection(addr ma.Multiaddr) {
transport := connectionTransportLabel(addr)
m.HandledConnectionCount.WithLabelValues(transport).Inc()
if manet.IsPublicAddr(addr) {
m.PublicAddressConnections.WithLabelValues(transport).Inc()
return
}
m.PrivateAddressConnections.WithLabelValues(transport).Inc()
}

// connectionTransportLabel returns the Prometheus transport label for a connection
// multiaddr. Live WSS connections are often encoded with the deprecated /wss
// component rather than the /tls/.../ws form used in advertised AutoTLS addresses.
func connectionTransportLabel(addr ma.Multiaddr) string {
if addr == nil {
return bzz.TransportUnknown.String()
}
if _, err := addr.ValueForProtocol(ma.P_WSS); err == nil {
return bzz.TransportWSS.String()
}
if t := bzz.ClassifyTransport(addr); t != bzz.TransportUnknown {
return t.String()
}
if _, err := addr.ValueForProtocol(ma.P_QUIC_V1); err == nil {
return "quic-v1"
}
if _, err := addr.ValueForProtocol(ma.P_QUIC); err == nil {
return "quic"
}
return bzz.TransportUnknown.String()
}

func (s *Service) Metrics() []prometheus.Collector {
collectors := append(m.PrometheusCollectorsFromFields(s.metrics), s.handshakeService.Metrics()...)
if mc, ok := s.reacher.(interface{ Metrics() []prometheus.Collector }); ok {
Expand Down
Loading