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, tcp: only register Prometheus counters when metrics are enabled #1971

Merged
merged 2 commits into from
Feb 3, 2023
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
2 changes: 1 addition & 1 deletion p2p/transport/quicreuse/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
9 changes: 8 additions & 1 deletion p2p/transport/quicreuse/tracer_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On L103 we're using the default prometheus Registerer. I think in general we should accept this as a parameter rather than hardcoded since we are a library.

No change needed now. If you agree, let's open an issue.

Expand Down Expand Up @@ -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}
}
Expand Down
5 changes: 4 additions & 1 deletion p2p/transport/tcp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down