quic: only register Prometheus counters when metrics are enabled

This commit is contained in:
Marten Seemann 2023-01-02 13:18:06 +13:00
parent 82315917f7
commit e37ca354f1
2 changed files with 9 additions and 2 deletions

View File

@ -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...)

View File

@ -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}
}