From e37ca354f160b1c5f27c76cf1906f94a6c0139fd Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 2 Jan 2023 13:18:06 +1300 Subject: [PATCH] quic: only register Prometheus counters when metrics are enabled --- p2p/transport/quicreuse/connmgr.go | 2 +- p2p/transport/quicreuse/tracer_metrics.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/p2p/transport/quicreuse/connmgr.go b/p2p/transport/quicreuse/connmgr.go index 36fdb582..b426818b 100644 --- a/p2p/transport/quicreuse/connmgr.go +++ b/p2p/transport/quicreuse/connmgr.go @@ -54,7 +54,7 @@ func NewConnManager(statelessResetKey quic.StatelessResetKey, opts ...Option) (* tracers = append(tracers, qlogTracer) } if cm.enableMetrics { - tracers = append(tracers, &metricsTracer{}) + tracers = append(tracers, newMetricsTracer()) } if len(tracers) > 0 { quicConf.Tracer = quiclogging.NewMultiplexedTracer(tracers...) diff --git a/p2p/transport/quicreuse/tracer_metrics.go b/p2p/transport/quicreuse/tracer_metrics.go index 3282b747..213d11c7 100644 --- a/p2p/transport/quicreuse/tracer_metrics.go +++ b/p2p/transport/quicreuse/tracer_metrics.go @@ -85,7 +85,9 @@ func (c *aggregatingCollector) RemoveConn(id string) { var collector *aggregatingCollector -func init() { +var initMetricsOnce sync.Once + +func initMetrics() { const ( direction = "direction" encLevel = "encryption_level" @@ -173,6 +175,11 @@ type metricsTracer struct { var _ logging.Tracer = &metricsTracer{} +func newMetricsTracer() *metricsTracer { + initMetricsOnce.Do(func() { initMetrics() }) + return &metricsTracer{} +} + func (m *metricsTracer) TracerForConnection(_ context.Context, p logging.Perspective, connID logging.ConnectionID) logging.ConnectionTracer { return &metricsConnTracer{perspective: p, connID: connID} }