Use Gauge instead of Counter for connections_active Prometheus metric

This commit is contained in:
Uladzimir Mihura 2019-03-04 22:35:45 +01:00
parent 213b8c8267
commit 13d0f0ddab
2 changed files with 13 additions and 15 deletions

View file

@ -89,7 +89,7 @@ func NewNGINXStatus(podName, namespace, ingressClass string) (NGINXStatusCollect
p.data = &nginxStatusData{ p.data = &nginxStatusData{
connectionsTotal: prometheus.NewDesc( connectionsTotal: prometheus.NewDesc(
prometheus.BuildFQName(PrometheusNamespace, subSystem, "connections_total"), prometheus.BuildFQName(PrometheusNamespace, subSystem, "connections_total"),
"total number of connections with state {active, accepted, handled}", "total number of connections with state {accepted, handled}",
[]string{"state"}, constLabels), []string{"state"}, constLabels),
requestsTotal: prometheus.NewDesc( requestsTotal: prometheus.NewDesc(
@ -99,7 +99,7 @@ func NewNGINXStatus(podName, namespace, ingressClass string) (NGINXStatusCollect
connections: prometheus.NewDesc( connections: prometheus.NewDesc(
prometheus.BuildFQName(PrometheusNamespace, subSystem, "connections"), prometheus.BuildFQName(PrometheusNamespace, subSystem, "connections"),
"current number of client connections with state {reading, writing, waiting}", "current number of client connections with state {active, reading, writing, waiting}",
[]string{"state"}, constLabels), []string{"state"}, constLabels),
} }
@ -180,14 +180,14 @@ func (p nginxStatusCollector) scrape(ch chan<- prometheus.Metric) {
s := parse(string(data)) s := parse(string(data))
ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal,
prometheus.CounterValue, float64(s.Active), "active")
ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal, ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal,
prometheus.CounterValue, float64(s.Accepted), "accepted") prometheus.CounterValue, float64(s.Accepted), "accepted")
ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal, ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal,
prometheus.CounterValue, float64(s.Handled), "handled") prometheus.CounterValue, float64(s.Handled), "handled")
ch <- prometheus.MustNewConstMetric(p.data.requestsTotal, ch <- prometheus.MustNewConstMetric(p.data.requestsTotal,
prometheus.CounterValue, float64(s.Requests)) prometheus.CounterValue, float64(s.Requests))
ch <- prometheus.MustNewConstMetric(p.data.connections,
prometheus.GaugeValue, float64(s.Active), "active")
ch <- prometheus.MustNewConstMetric(p.data.connections, ch <- prometheus.MustNewConstMetric(p.data.connections,
prometheus.GaugeValue, float64(s.Reading), "reading") prometheus.GaugeValue, float64(s.Reading), "reading")
ch <- prometheus.MustNewConstMetric(p.data.connections, ch <- prometheus.MustNewConstMetric(p.data.connections,

View file

@ -41,10 +41,9 @@ func TestStatusCollector(t *testing.T) {
mock: ` mock: `
`, `,
want: ` want: `
# HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {active, accepted, handled} # HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {accepted, handled}
# TYPE nginx_ingress_controller_nginx_process_connections_total counter # TYPE nginx_ingress_controller_nginx_process_connections_total counter
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 0 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 0
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="active"} 0
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 0 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 0
`, `,
metrics: []string{"nginx_ingress_controller_nginx_process_connections_total"}, metrics: []string{"nginx_ingress_controller_nginx_process_connections_total"},
@ -52,16 +51,15 @@ func TestStatusCollector(t *testing.T) {
{ {
name: "should return metrics for total connections", name: "should return metrics for total connections",
mock: ` mock: `
Active connections: 1 Active connections: 15
server accepts handled requests server accepts handled requests
1 2 3 1 2 3
Reading: 4 Writing: 5 Waiting: 6 Reading: 4 Writing: 5 Waiting: 6
`, `,
want: ` want: `
# HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {active, accepted, handled} # HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {accepted, handled}
# TYPE nginx_ingress_controller_nginx_process_connections_total counter # TYPE nginx_ingress_controller_nginx_process_connections_total counter
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 1 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 1
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="active"} 1
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 2 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 2
`, `,
metrics: []string{"nginx_ingress_controller_nginx_process_connections_total"}, metrics: []string{"nginx_ingress_controller_nginx_process_connections_total"},
@ -69,21 +67,21 @@ func TestStatusCollector(t *testing.T) {
{ {
name: "should return nginx metrics all available metrics", name: "should return nginx metrics all available metrics",
mock: ` mock: `
Active connections: 1 Active connections: 15
server accepts handled requests server accepts handled requests
1 2 3 1 2 3
Reading: 4 Writing: 5 Waiting: 6 Reading: 4 Writing: 5 Waiting: 6
`, `,
want: ` want: `
# HELP nginx_ingress_controller_nginx_process_connections current number of client connections with state {reading, writing, waiting} # HELP nginx_ingress_controller_nginx_process_connections current number of client connections with state {active, reading, writing, waiting}
# TYPE nginx_ingress_controller_nginx_process_connections gauge # TYPE nginx_ingress_controller_nginx_process_connections gauge
nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="active"} 15
nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="reading"} 4 nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="reading"} 4
nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="waiting"} 6 nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="waiting"} 6
nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="writing"} 5 nginx_ingress_controller_nginx_process_connections{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="writing"} 5
# HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {active, accepted, handled} # HELP nginx_ingress_controller_nginx_process_connections_total total number of connections with state {accepted, handled}
# TYPE nginx_ingress_controller_nginx_process_connections_total counter # TYPE nginx_ingress_controller_nginx_process_connections_total counter
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 1 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="accepted"} 1
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="active"} 1
nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 2 nginx_ingress_controller_nginx_process_connections_total{controller_class="nginx",controller_namespace="default",controller_pod="pod",state="handled"} 2
# HELP nginx_ingress_controller_nginx_process_requests_total total number of client requests # HELP nginx_ingress_controller_nginx_process_requests_total total number of client requests
# TYPE nginx_ingress_controller_nginx_process_requests_total counter # TYPE nginx_ingress_controller_nginx_process_requests_total counter