Commit 31381401 authored by Janos Guljas's avatar Janos Guljas

add libp2p metrics for connections and streams

parent 72bd3f60
......@@ -99,6 +99,7 @@ func (c *command) initStartCmd() (err error) {
// Debug API server
debugAPIService := debugapi.New(debugapi.Options{})
// register metrics from components
debugAPIService.MustRegisterMetrics(p2ps.Metrics()...)
debugAPIService.MustRegisterMetrics(pingPong.Metrics()...)
debugAPIService.MustRegisterMetrics(apiService.Metrics()...)
......
......@@ -27,6 +27,7 @@ var _ p2p.Service = new(Service)
type Service struct {
host host.Host
metrics metrics
}
type Options struct {
......@@ -129,7 +130,10 @@ func New(ctx context.Context, o Options) (*Service, error) {
return nil, fmt.Errorf("autonat: %w", err)
}
s := &Service{host: h}
s := &Service{
host: h,
metrics: newMetrics(),
}
// TODO: be more resilient on connection errors and connect in parallel
for _, a := range o.Bootnodes {
......@@ -143,6 +147,10 @@ func New(ctx context.Context, o Options) (*Service, error) {
}
}
h.Network().SetConnHandler(func(_ network.Conn) {
s.metrics.HandledConnectionCount.Inc()
})
return s, nil
}
......@@ -153,10 +161,11 @@ func (s *Service) AddProtocol(p p2p.ProtocolSpec) (err error) {
if err != nil {
return fmt.Errorf("match semver %s: %w", id, err)
}
s.host.SetStreamHandlerMatch(id, matcher, func(s network.Stream) {
s.host.SetStreamHandlerMatch(id, matcher, func(stream network.Stream) {
s.metrics.HandledStreamCount.Inc()
ss.Handler(p2p.Peer{
Addr: s.Conn().RemoteMultiaddr(),
Stream: s,
Addr: stream.Conn().RemoteMultiaddr(),
Stream: stream,
})
})
}
......@@ -189,6 +198,8 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (peerID string
return "", err
}
s.metrics.CreatedConnectionCount.Inc()
return info.ID.String(), nil
}
......@@ -205,6 +216,7 @@ func (s *Service) NewStream(ctx context.Context, peerID, protocolName, streamNam
}
return nil, fmt.Errorf("create stream %q to %q: %w", swarmStreamName, peerID, err)
}
s.metrics.CreatedStreamCount.Inc()
return st, nil
}
......
package libp2p
import (
m "github.com/janos/bee/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)
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
CreatedStreamCount prometheus.Counter
HandledStreamCount prometheus.Counter
}
func newMetrics() (m metrics) {
return metrics{
CreatedConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_created_connection_count",
Help: "Number of initiated outgoing libp2p connections.",
}),
HandledConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_handled_connection_count",
Help: "Number of handled incoming libp2p connections.",
}),
CreatedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_created_stream_count",
Help: "Number of initiated outgoing libp2p streams.",
}),
HandledStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_handled_stream_count",
Help: "Number of handled incoming libp2p streams.",
}),
}
}
func (s *Service) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment