Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quic: add a WithMetrics option #1716

Merged
merged 1 commit into from
Aug 25, 2022
Merged
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
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