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

metrics: connections use active instead of closed #972

Merged
merged 5 commits into from
Dec 3, 2024
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
85 changes: 44 additions & 41 deletions net_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

type dialerMetrics struct {
dialed *prometheus.CounterVec
errors *prometheus.CounterVec
closed *prometheus.CounterVec
dialed *prometheus.CounterVec
active *prometheus.GaugeVec
}

func newDialerMetrics(r prometheus.Registerer, namespace string) *dialerMetrics {
Expand All @@ -28,34 +28,36 @@ func newDialerMetrics(r prometheus.Registerer, namespace string) *dialerMetrics
l := []string{"host"}

return &dialerMetrics{
dialed: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_dialed_total",
Namespace: namespace,
Help: "Number of dialed connections",
}, l),
errors: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_errors_total",
Namespace: namespace,
Help: "Number of dialer errors",
Help: "Number of errors dialing connections",
}, l),
closed: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_closed_total",
dialed: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_cx_total",
Namespace: namespace,
Help: "Number of closed connections",
Help: "Number of dialed connections",
}, l),
active: f.NewGaugeVec(prometheus.GaugeOpts{
Name: "dialer_cx_active",
Namespace: namespace,
Help: "Number of active connections",
}, l),
}
}

func (m *dialerMetrics) dial(addr string) {
m.dialed.WithLabelValues(addr2Host(addr)).Inc()
}

func (m *dialerMetrics) error(addr string) {
m.errors.WithLabelValues(addr2Host(addr)).Inc()
}

func (m *dialerMetrics) dial(addr string) {
host := addr2Host(addr)
m.dialed.WithLabelValues(host).Inc()
m.active.WithLabelValues(host).Inc()
}

func (m *dialerMetrics) close(addr string) {
m.closed.WithLabelValues(addr2Host(addr)).Inc()
m.active.WithLabelValues(addr2Host(addr)).Dec()
}

func addr2Host(addr string) string {
Expand All @@ -82,9 +84,9 @@ func addr2Host(addr string) string {
}

type listenerMetrics struct {
accepted prometheus.Counter
errors prometheus.Counter
closed prometheus.Counter
accepted prometheus.Counter
active prometheus.Gauge
}

func newListenerMetrics(r prometheus.Registerer, namespace string) *listenerMetrics {
Expand All @@ -94,34 +96,35 @@ func newListenerMetrics(r prometheus.Registerer, namespace string) *listenerMetr
f := promauto.With(r)

return &listenerMetrics{
accepted: f.NewCounter(prometheus.CounterOpts{
Name: "listener_accepted_total",
Namespace: namespace,
Help: "Number of accepted connections",
}),
errors: f.NewCounter(prometheus.CounterOpts{
Name: "listener_errors_total",
Namespace: namespace,
Help: "Number of listener errors when accepting connections",
}),
closed: f.NewCounter(prometheus.CounterOpts{
Name: "listener_closed_total",
accepted: f.NewCounter(prometheus.CounterOpts{
Name: "listener_cx_total",
Namespace: namespace,
Help: "Number of accepted connections",
}),
active: f.NewGauge(prometheus.GaugeOpts{
Name: "listener_cx_active",
Namespace: namespace,
Help: "Number of closed connections",
Help: "Number of active connections",
}),
}
}

func (m *listenerMetrics) accept() {
m.accepted.Inc()
}

func (m *listenerMetrics) error() {
m.errors.Inc()
}

func (m *listenerMetrics) accept() {
m.accepted.Inc()
m.active.Inc()
}

func (m *listenerMetrics) close() {
m.closed.Inc()
m.active.Dec()
}

func newListenerMetricsWithNameFunc(r prometheus.Registerer, namespace string) func(name string) *listenerMetrics {
Expand All @@ -130,27 +133,27 @@ func newListenerMetricsWithNameFunc(r prometheus.Registerer, namespace string) f
}
f := promauto.With(r)

accepted := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_accepted_total",
Namespace: namespace,
Help: "Number of accepted connections",
}, []string{"name"})
errors := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_errors_total",
Namespace: namespace,
Help: "Number of listener errors when accepting connections",
}, []string{"name"})
closed := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_closed_total",
accepted := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_cx_total",
Namespace: namespace,
Help: "Number of closed connections",
Help: "Number of accepted connections",
}, []string{"name"})
active := f.NewGaugeVec(prometheus.GaugeOpts{
Name: "listener_cx_active",
Namespace: namespace,
Help: "Number of active connections",
}, []string{"name"})

return func(name string) *listenerMetrics {
return &listenerMetrics{
accepted: accepted.WithLabelValues(name),
errors: errors.WithLabelValues(name),
closed: closed.WithLabelValues(name),
accepted: accepted.WithLabelValues(name),
active: active.WithLabelValues(name),
}
}
}
2 changes: 1 addition & 1 deletion testdata/TestDialerMetricsErrors.golden.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# HELP test_dialer_errors_total Number of dialer errors
# HELP test_dialer_errors_total Number of errors dialing connections
# TYPE test_dialer_errors_total counter
test_dialer_errors_total{host="localhost"} 1
12 changes: 6 additions & 6 deletions testdata/TestDialerMetrics_default.golden.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# HELP test_dialer_closed_total Number of closed connections
# TYPE test_dialer_closed_total counter
test_dialer_closed_total{host="localhost"} 10
# HELP test_dialer_dialed_total Number of dialed connections
# TYPE test_dialer_dialed_total counter
test_dialer_dialed_total{host="localhost"} 10
# HELP test_dialer_cx_active Number of active connections
# TYPE test_dialer_cx_active gauge
test_dialer_cx_active{host="localhost"} 0
# HELP test_dialer_cx_total Number of dialed connections
# TYPE test_dialer_cx_total counter
test_dialer_cx_total{host="localhost"} 10
12 changes: 6 additions & 6 deletions testdata/TestDialerMetrics_traffic.golden.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP test_dialer_closed_total Number of closed connections
# TYPE test_dialer_closed_total counter
test_dialer_closed_total{host="localhost"} 10
# HELP test_dialer_dialed_total Number of dialed connections
# TYPE test_dialer_dialed_total counter
test_dialer_dialed_total{host="localhost"} 10
# HELP test_dialer_cx_active Number of active connections
# TYPE test_dialer_cx_active gauge
test_dialer_cx_active{host="localhost"} 0
# HELP test_dialer_cx_total Number of dialed connections
# TYPE test_dialer_cx_total counter
test_dialer_cx_total{host="localhost"} 10
# HELP test_dialer_rx_bytes_total Total number of bytes read by the dialer.
# TYPE test_dialer_rx_bytes_total counter
test_dialer_rx_bytes_total{conn_id="0"} 1
Expand Down
12 changes: 6 additions & 6 deletions testdata/TestListenerMetricsAccepted.golden.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP test_listener_accepted_total Number of accepted connections
# TYPE test_listener_accepted_total counter
test_listener_accepted_total 10
# HELP test_listener_closed_total Number of closed connections
# TYPE test_listener_closed_total counter
test_listener_closed_total 10
# HELP test_listener_cx_active Number of active connections
# TYPE test_listener_cx_active gauge
test_listener_cx_active 0
# HELP test_listener_cx_total Number of accepted connections
# TYPE test_listener_cx_total counter
test_listener_cx_total 10
# HELP test_listener_errors_total Number of listener errors when accepting connections
# TYPE test_listener_errors_total counter
test_listener_errors_total 0
12 changes: 6 additions & 6 deletions testdata/TestListenerMetricsAcceptedWithTLS.golden.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP test_listener_accepted_total Number of accepted connections
# TYPE test_listener_accepted_total counter
test_listener_accepted_total 10
# HELP test_listener_closed_total Number of closed connections
# TYPE test_listener_closed_total counter
test_listener_closed_total 10
# HELP test_listener_cx_active Number of active connections
# TYPE test_listener_cx_active gauge
test_listener_cx_active 0
# HELP test_listener_cx_total Number of accepted connections
# TYPE test_listener_cx_total counter
test_listener_cx_total 10
# HELP test_listener_errors_total Number of listener errors when accepting connections
# TYPE test_listener_errors_total counter
test_listener_errors_total 0
12 changes: 6 additions & 6 deletions testdata/TestListenerMetricsClosed.golden.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP test_listener_accepted_total Number of accepted connections
# TYPE test_listener_accepted_total counter
test_listener_accepted_total 1
# HELP test_listener_closed_total Number of closed connections
# TYPE test_listener_closed_total counter
test_listener_closed_total 1
# HELP test_listener_cx_active Number of active connections
# TYPE test_listener_cx_active gauge
test_listener_cx_active 0
# HELP test_listener_cx_total Number of accepted connections
# TYPE test_listener_cx_total counter
test_listener_cx_total 1
# HELP test_listener_errors_total Number of listener errors when accepting connections
# TYPE test_listener_errors_total counter
test_listener_errors_total 0
12 changes: 6 additions & 6 deletions testdata/TestListenerMetricsErrors.golden.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP test_listener_accepted_total Number of accepted connections
# TYPE test_listener_accepted_total counter
test_listener_accepted_total 0
# HELP test_listener_closed_total Number of closed connections
# TYPE test_listener_closed_total counter
test_listener_closed_total 0
# HELP test_listener_cx_active Number of active connections
# TYPE test_listener_cx_active gauge
test_listener_cx_active 0
# HELP test_listener_cx_total Number of accepted connections
# TYPE test_listener_cx_total counter
test_listener_cx_total 0
# HELP test_listener_errors_total Number of listener errors when accepting connections
# TYPE test_listener_errors_total counter
test_listener_errors_total 1
16 changes: 8 additions & 8 deletions testdata/TestMultiListenerMetrics.golden.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# HELP test_listener_accepted_total Number of accepted connections
# TYPE test_listener_accepted_total counter
test_listener_accepted_total{name="a"} 10
test_listener_accepted_total{name="b"} 10
# HELP test_listener_closed_total Number of closed connections
# TYPE test_listener_closed_total counter
test_listener_closed_total{name="a"} 10
test_listener_closed_total{name="b"} 10
# HELP test_listener_cx_active Number of active connections
# TYPE test_listener_cx_active gauge
test_listener_cx_active{name="a"} 0
test_listener_cx_active{name="b"} 0
# HELP test_listener_cx_total Number of accepted connections
# TYPE test_listener_cx_total counter
test_listener_cx_total{name="a"} 10
test_listener_cx_total{name="b"} 10
# HELP test_listener_errors_total Number of listener errors when accepting connections
# TYPE test_listener_errors_total counter
test_listener_errors_total{name="a"} 0
Expand Down
Loading