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

Expose the number of open connections. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions dialer_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ var (
Name: "dialer_conn_closed_total",
Help: "Total number of connections closed which originated from the dialer of a given name.",
}, []string{"dialer_name"})

dialerConnOpen = prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: "net",
Subsystem: "conntrack",
Name: "dialer_conn_open",
Help: "Number of open connections which originated from the dialer of a given name.",
}, []string{"dialer_name"})
)

func init() {
prom.MustRegister(dialerAttemptedTotal)
prom.MustRegister(dialerConnEstablishedTotal)
prom.MustRegister(dialerConnFailedTotal)
prom.MustRegister(dialerConnClosedTotal)
prom.MustRegister(dialerConnOpen)
}

// preRegisterDialerMetrics pre-populates Prometheus labels for the given dialer name, to avoid Prometheus missing labels issue.
Expand All @@ -70,6 +79,7 @@ func PreRegisterDialerMetrics(dialerName string) {
dialerConnFailedTotal.WithLabelValues(dialerName, string(reason))
}
dialerConnClosedTotal.WithLabelValues(dialerName)
dialerConnOpen.WithLabelValues(dialerName)
}

func reportDialerConnAttempt(dialerName string) {
Expand All @@ -78,10 +88,12 @@ func reportDialerConnAttempt(dialerName string) {

func reportDialerConnEstablished(dialerName string) {
dialerConnEstablishedTotal.WithLabelValues(dialerName).Inc()
dialerConnOpen.WithLabelValues(dialerName).Inc()
}

func reportDialerConnClosed(dialerName string) {
dialerConnClosedTotal.WithLabelValues(dialerName).Inc()
dialerConnOpen.WithLabelValues(dialerName).Dec()
}

func reportDialerConnFailed(dialerName string, err error) {
Expand Down
9 changes: 9 additions & 0 deletions dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (s *DialerTestSuite) TestDialerMetricsArePreregistered() {
{"net_conntrack_dialer_conn_failed_total", []string{"default", "refused"}},
{"net_conntrack_dialer_conn_failed_total", []string{"default", "timeout"}},
{"net_conntrack_dialer_conn_failed_total", []string{"default", "unknown"}},
{"net_conntrack_dialer_conn_open", []string{"default"}},
{"net_conntrack_dialer_conn_open", []string{"foobar"}},
{"net_conntrack_dialer_conn_open", []string{"something_manual"}},
} {
lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...))
assert.NotEqual(s.T(), 0, lineCount, "metrics must exist for test case %d", testId)
Expand All @@ -81,6 +84,7 @@ func (s *DialerTestSuite) TestDialerMetricsAreNotPreregisteredWithMonitoringOff(
{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "refused"}},
{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "timeout"}},
{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "unknown"}},
{"net_conntrack_dialer_conn_open", []string{"nomon"}},
} {
lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...))
assert.Equal(s.T(), 0, lineCount, "metrics should not be registered exist for test case %d", testId)
Expand All @@ -93,6 +97,7 @@ func (s *DialerTestSuite) TestDialerUnderNormalConnection() {
beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "normal_conn")
beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn")
beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn")
beforeOpen := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn")

conn, err := dialFunc(context.TODO(), "tcp", s.serverListener.Addr().String())
require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here")
Expand All @@ -102,9 +107,13 @@ func (s *DialerTestSuite) TestDialerUnderNormalConnection() {
"the established conn counter must be incremented after connection was opened")
assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn"),
"the closed conn counter must not be incremented after connection was opened")
assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn"),
"the open conn gauge must be incremented after connection was opened")
conn.Close()
assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn"),
"the closed conn counter must be incremented after connection was closed")
assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn"),
"the open conn gauge must be decremented after connection was closed")
}

func (s *DialerTestSuite) TestDialerWithContextName() {
Expand Down
11 changes: 11 additions & 0 deletions listener_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,34 @@ var (
Name: "listener_conn_closed_total",
Help: "Total number of connections closed that were made to the listener of a given name.",
}, []string{"listener_name"})
listenerOpen = prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: "net",
Subsystem: "conntrack",
Name: "listener_conn_open",
Help: "Number of open connections to the listener of a given name.",
}, []string{"listener_name"})
)

func init() {
prom.MustRegister(listenerAcceptedTotal)
prom.MustRegister(listenerClosedTotal)
prom.MustRegister(listenerOpen)
}

// preRegisterListener pre-populates Prometheus labels for the given listener name, to avoid Prometheus missing labels issue.
func preRegisterListenerMetrics(listenerName string) {
listenerAcceptedTotal.WithLabelValues(listenerName)
listenerClosedTotal.WithLabelValues(listenerName)
listenerOpen.WithLabelValues(listenerName)
}

func reportListenerConnAccepted(listenerName string) {
listenerAcceptedTotal.WithLabelValues(listenerName).Inc()
listenerOpen.WithLabelValues(listenerName).Inc()
}

func reportListenerConnClosed(listenerName string) {
listenerClosedTotal.WithLabelValues(listenerName).Inc()
listenerOpen.WithLabelValues(listenerName).Dec()
}
7 changes: 7 additions & 0 deletions listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (s *ListenerTestSuite) TestTrackingMetricsPreregistered() {
}{
{"net_conntrack_listener_conn_accepted_total", []string{"default"}},
{"net_conntrack_listener_conn_closed_total", []string{"default"}},
{"net_conntrack_listener_conn_open", []string{"default"}},
{"net_conntrack_listener_conn_accepted_total", []string{listenerName}},
{"net_conntrack_listener_conn_closed_total", []string{listenerName}},
{"net_conntrack_listener_conn_open", []string{listenerName}},
} {
lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...))
assert.NotEqual(s.T(), 0, lineCount, "metrics must exist for test case %d", testId)
Expand All @@ -70,16 +72,21 @@ func (s *ListenerTestSuite) TestMonitoringNormalConns() {

beforeAccepted := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_accepted_total", listenerName)
beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName)
beforeOpen := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName)

conn, err := (&net.Dialer{}).DialContext(context.TODO(), "tcp", s.serverListener.Addr().String())
require.NoError(s.T(), err, "DialContext should successfully establish a conn here")
assert.Equal(s.T(), beforeAccepted+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_accepted_total", listenerName),
"the accepted conn counter must be incremented after connection was opened")
assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName),
"the closed conn counter must not be incremented before the connection is closed")
assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName),
"the open conn must be incremented when the connection is opened")
conn.Close()
assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName),
"the closed conn counter must be incremented after connection was closed")
assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName),
"the open conn must be decremented when the connection is closed")
}

func (s *ListenerTestSuite) TestTracingNormalComms() {
Expand Down