Skip to content

Commit

Permalink
quic: add support for quic-go metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jun 7, 2024
1 parent 2769070 commit 0e54a7e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
50 changes: 41 additions & 9 deletions p2p/transport/quicreuse/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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":
Expand Down
7 changes: 6 additions & 1 deletion p2p/transport/quicreuse/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package quicreuse

import "github.com/prometheus/client_golang/prometheus"

type Option func(*ConnManager) error

func DisableReuseport() Option {
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion test-plans/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test-plans/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0e54a7e

Please sign in to comment.