From c454f9c06e094b071b3eb7419a926baafce522cd Mon Sep 17 00:00:00 2001 From: sukun Date: Thu, 6 Jun 2024 13:59:06 +0530 Subject: [PATCH] quic: add support for quic-go metrics --- config/config.go | 6 +++- go.mod | 2 +- go.sum | 4 +-- p2p/transport/quicreuse/connmgr.go | 50 ++++++++++++++++++++++++------ p2p/transport/quicreuse/options.go | 7 ++++- test-plans/go.mod | 2 +- test-plans/go.sum | 4 +-- 7 files changed, 58 insertions(+), 17 deletions(-) diff --git a/config/config.go b/config/config.go index cc689baf7b..9d4dbc225e 100644 --- a/config/config.go +++ b/config/config.go @@ -261,7 +261,11 @@ func (cfg *Config) addTransports() ([]fx.Option, error) { } else { fxopts = append(fxopts, fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, _ *swarm.Swarm, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) { - cm, err := quicreuse.NewConnManager(key, tokenGenerator) + var opts []quicreuse.Option + if !cfg.DisableMetrics { + opts = append(opts, quicreuse.EnableMetrics(cfg.PrometheusRegisterer)) + } + cm, err := quicreuse.NewConnManager(key, tokenGenerator, opts...) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 527379744f..8ddffe7550 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( github.com/pion/webrtc/v3 v3.2.40 github.com/prometheus/client_golang v1.19.1 github.com/prometheus/client_model v0.6.1 - github.com/quic-go/quic-go v0.44.0 + github.com/quic-go/quic-go v0.45.0 github.com/quic-go/webtransport-go v0.8.0 github.com/raulk/go-watchdog v1.3.0 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index 0d5cfeca4c..367ee9bac1 100644 --- a/go.sum +++ b/go.sum @@ -336,8 +336,8 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= -github.com/quic-go/quic-go v0.44.0 h1:So5wOr7jyO4vzL2sd8/pD9Kesciv91zSk8BoFngItQ0= -github.com/quic-go/quic-go v0.44.0/go.mod h1:z4cx/9Ny9UtGITIPzmPTXh1ULfOyWh4qGQlpnPcWmek= +github.com/quic-go/quic-go v0.45.0 h1:OHmkQGM37luZITyTSu6ff03HP/2IrwDX1ZFiNEhSFUE= +github.com/quic-go/quic-go v0.45.0/go.mod h1:1dLehS7TIR64+vxGR70GDcatWTOtMX2PUtnKsjbTurI= github.com/quic-go/webtransport-go v0.8.0 h1:HxSrwun11U+LlmwpgM1kEqIqH90IT4N8auv/cD7QFJg= github.com/quic-go/webtransport-go v0.8.0/go.mod h1:N99tjprW432Ut5ONql/aUhSLT0YVSlwHohQsuac9WaM= github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk= diff --git a/p2p/transport/quicreuse/connmgr.go b/p2p/transport/quicreuse/connmgr.go index ec2b7b280e..00cd5750f5 100644 --- a/p2p/transport/quicreuse/connmgr.go +++ b/p2p/transport/quicreuse/connmgr.go @@ -9,15 +9,23 @@ import ( ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" + "github.com/prometheus/client_golang/prometheus" "github.com/quic-go/quic-go" quiclogging "github.com/quic-go/quic-go/logging" + quicmetrics "github.com/quic-go/quic-go/metrics" ) type ConnManager struct { reuseUDP4 *reuse reuseUDP6 *reuse enableReuseport bool - enableMetrics bool + + enableMetrics bool + registerer prometheus.Registerer + + metricsOnce sync.Once + serverConnTracer *quiclogging.ConnectionTracer + clientConnTracer *quiclogging.ConnectionTracer serverConfig *quic.Config clientConfig *quic.Config @@ -40,6 +48,7 @@ func NewConnManager(statelessResetKey quic.StatelessResetKey, tokenKey quic.Toke quicListeners: make(map[string]quicListenerEntry), srk: statelessResetKey, tokenKey: tokenKey, + registerer: prometheus.DefaultRegisterer, } for _, o := range opts { if err := o(cm); err != nil { @@ -48,14 +57,7 @@ func NewConnManager(statelessResetKey quic.StatelessResetKey, tokenKey quic.Toke } quicConf := quicConfig.Clone() - - quicConf.Tracer = func(ctx context.Context, p quiclogging.Perspective, ci quic.ConnectionID) *quiclogging.ConnectionTracer { - var tracer *quiclogging.ConnectionTracer - if qlogTracerDir != "" { - tracer = qloggerForDir(qlogTracerDir, p, ci) - } - return tracer - } + quicConf.Tracer = cm.getTracer() serverConfig := quicConf.Clone() cm.clientConfig = quicConf @@ -67,6 +69,36 @@ func NewConnManager(statelessResetKey quic.StatelessResetKey, tokenKey quic.Toke return cm, nil } +func (c *ConnManager) getTracer() func(context.Context, quiclogging.Perspective, quic.ConnectionID) *quiclogging.ConnectionTracer { + return func(ctx context.Context, p quiclogging.Perspective, ci quic.ConnectionID) *quiclogging.ConnectionTracer { + c.metricsOnce.Do(func() { + if c.enableMetrics { + c.clientConnTracer = quicmetrics.NewClientConnectionTracerWithRegisterer(c.registerer) + c.serverConnTracer = quicmetrics.NewServerConnectionTracerWithRegisterer(c.registerer) + } + }) + var tracer *quiclogging.ConnectionTracer + switch p { + case quiclogging.PerspectiveClient: + tracer = c.clientConnTracer + case quiclogging.PerspectiveServer: + tracer = c.serverConnTracer + default: + log.Errorf("invalid perspective for logging: %s", p) + } + if qlogTracerDir != "" { + dirTracer := qloggerForDir(qlogTracerDir, p, ci) + if tracer != nil { + tracer = quiclogging.NewMultiplexedConnectionTracer(tracer, + dirTracer) + } else { + tracer = dirTracer + } + } + return tracer + } +} + func (c *ConnManager) getReuse(network string) (*reuse, error) { switch network { case "udp4": diff --git a/p2p/transport/quicreuse/options.go b/p2p/transport/quicreuse/options.go index c325069209..30d07d4443 100644 --- a/p2p/transport/quicreuse/options.go +++ b/p2p/transport/quicreuse/options.go @@ -1,5 +1,7 @@ package quicreuse +import "github.com/prometheus/client_golang/prometheus" + type Option func(*ConnManager) error func DisableReuseport() Option { @@ -10,9 +12,12 @@ func DisableReuseport() Option { } // EnableMetrics enables Prometheus metrics collection. -func EnableMetrics() Option { +func EnableMetrics(reg prometheus.Registerer) Option { return func(m *ConnManager) error { m.enableMetrics = true + if reg != nil { + m.registerer = reg + } return nil } } diff --git a/test-plans/go.mod b/test-plans/go.mod index f0cf86aae5..6c45277292 100644 --- a/test-plans/go.mod +++ b/test-plans/go.mod @@ -87,7 +87,7 @@ require ( github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/quic-go/qpack v0.4.0 // indirect - github.com/quic-go/quic-go v0.44.0 // indirect + github.com/quic-go/quic-go v0.45.0 // indirect github.com/quic-go/webtransport-go v0.8.0 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect diff --git a/test-plans/go.sum b/test-plans/go.sum index e94d7dccde..be15ddfdf6 100644 --- a/test-plans/go.sum +++ b/test-plans/go.sum @@ -284,8 +284,8 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= -github.com/quic-go/quic-go v0.44.0 h1:So5wOr7jyO4vzL2sd8/pD9Kesciv91zSk8BoFngItQ0= -github.com/quic-go/quic-go v0.44.0/go.mod h1:z4cx/9Ny9UtGITIPzmPTXh1ULfOyWh4qGQlpnPcWmek= +github.com/quic-go/quic-go v0.45.0 h1:OHmkQGM37luZITyTSu6ff03HP/2IrwDX1ZFiNEhSFUE= +github.com/quic-go/quic-go v0.45.0/go.mod h1:1dLehS7TIR64+vxGR70GDcatWTOtMX2PUtnKsjbTurI= github.com/quic-go/webtransport-go v0.8.0 h1:HxSrwun11U+LlmwpgM1kEqIqH90IT4N8auv/cD7QFJg= github.com/quic-go/webtransport-go v0.8.0/go.mod h1:N99tjprW432Ut5ONql/aUhSLT0YVSlwHohQsuac9WaM= github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk=