tcp: only register Prometheus counters when metrics are enabled

This commit is contained in:
Marten Seemann 2023-01-02 13:24:00 +13:00
parent e37ca354f1
commit fac76291e0
1 changed files with 4 additions and 1 deletions

View File

@ -26,7 +26,9 @@ const collectFrequency = 10 * time.Second
var collector *aggregatingCollector
func init() {
var initMetricsOnce sync.Once
func initMetrics() {
segsSentDesc = prometheus.NewDesc("tcp_sent_segments_total", "TCP segments sent", nil, nil)
segsRcvdDesc = prometheus.NewDesc("tcp_rcvd_segments_total", "TCP segments received", nil, nil)
bytesSentDesc = prometheus.NewDesc("tcp_sent_bytes", "TCP bytes sent", nil, nil)
@ -210,6 +212,7 @@ type tracingConn struct {
}
func newTracingConn(c manet.Conn, isClient bool) (*tracingConn, error) {
initMetricsOnce.Do(func() { initMetrics() })
conn, err := tcp.NewConn(c)
if err != nil {
return nil, err