Skip to content

Commit

Permalink
quic: add a WithMetrics option (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Aug 25, 2022
1 parent 8df365b commit 173fb50
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
9 changes: 9 additions & 0 deletions p2p/transport/quic/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Option func(opts *config) error

type config struct {
disableReuseport bool
metrics bool
}

func (cfg *config) apply(opts ...Option) error {
Expand All @@ -22,3 +23,11 @@ func DisableReuseport() Option {
return nil
}
}

// WithMetrics enables Prometheus metrics collection.
func WithMetrics() Option {
return func(cfg *config) error {
cfg.metrics = true
return nil
}
}
8 changes: 2 additions & 6 deletions p2p/transport/quic/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ import (
"github.com/lucas-clemente/quic-go/qlog"
)

var tracer logging.Tracer
var qlogTracer logging.Tracer

func init() {
tracers := []logging.Tracer{&metricsTracer{}}
if qlogDir := os.Getenv("QLOGDIR"); len(qlogDir) > 0 {
if qlogger := initQlogger(qlogDir); qlogger != nil {
tracers = append(tracers, qlogger)
}
qlogTracer = initQlogger(qlogDir)
}
tracer = logging.NewMultiplexedTracer(tracers...)
}

func initQlogger(qlogDir string) logging.Tracer {
Expand Down
12 changes: 11 additions & 1 deletion p2p/transport/quic/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

logging "github.com/ipfs/go-log/v2"
"github.com/lucas-clemente/quic-go"
quiclogging "github.com/lucas-clemente/quic-go/logging"
"github.com/minio/sha256-simd"
)

Expand Down Expand Up @@ -199,7 +200,16 @@ func NewTransport(key ic.PrivKey, psk pnet.PSK, gater connmgr.ConnectionGater, r
if _, err := io.ReadFull(keyReader, qconfig.StatelessResetKey); err != nil {
return nil, err
}
qconfig.Tracer = tracer
var tracers []quiclogging.Tracer
if qlogTracer != nil {
tracers = append(tracers, qlogTracer)
}
if cfg.metrics {
tracers = append(tracers, &metricsTracer{})
}
if len(tracers) > 0 {
qconfig.Tracer = quiclogging.NewMultiplexedTracer(tracers...)
}

tr := &transport{
privKey: key,
Expand Down

0 comments on commit 173fb50

Please sign in to comment.