Skip to content

Commit 5ee605a

Browse files
committed
Add up metrics
Add nginx_up and nginxplus_up metrics that show the status of the last metric scrape from NGINX or NGINX Plus.
1 parent ad0a472 commit 5ee605a

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ Usage of ./nginx-prometheus-exporter:
7777
7878
### Exported Metrics
7979
80-
* For NGINX, all stub_status metrics are exported. Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions.
80+
* For NGINX, the following metrics are exported:
81+
* All [stub_status](http://nginx.org/en/docs/http/ngx_http_stub_status_module.html) metrics.
82+
* `nginx_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one.
83+
84+
Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions.
8185
* For NGINX Plus, the following metrics are exported:
8286
* [Connections](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_connections).
8387
* [HTTP](http://nginx.org/en/docs/http/ngx_http_api_module.html#http_).
@@ -86,6 +90,7 @@ Usage of ./nginx-prometheus-exporter:
8690
* [Stream Server Zones](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_server_zone).
8791
* [HTTP Upstreams](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_http_upstream). Note: for the `state` metric, the string values are converted to float64 using the following rule: `"up"` -> `1.0`, `"draining"` -> `2.0`, `"down"` -> `3.0`, `"unavail"` –> `4.0`, `"checking"` –> `5.0`, `"unhealthy"` -> `6.0`.
8892
* [Stream Upstreams](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_upstream). Note: for the `state` metric, the string values are converted to float64 using the following rule: `"up"` -> `1.0`, `"down"` -> `3.0`, `"unavail"` –> `4.0`, `"checking"` –> `5.0`, `"unhealthy"` -> `6.0`.
93+
* `nginxplus_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one.
8994
9095
9196
Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions. Note: to see server zones related metrics you must configure [status zones](https://nginx.org/en/docs/http/ngx_http_status_module.html#status_zone) and to see upstream related metrics you must configure upstreams with a [shared memory zone](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#zone).

collector/helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ package collector
22

33
import "github.com/prometheus/client_golang/prometheus"
44

5+
const nginxUp = 1
6+
const nginxDown = 0
7+
58
func newGlobalMetric(namespace string, metricName string, docString string) *prometheus.Desc {
69
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, nil)
710
}
11+
12+
func newUpMetric(namespace string) prometheus.Gauge {
13+
return prometheus.NewGauge(prometheus.GaugeOpts{
14+
Namespace: namespace,
15+
Name: "up",
16+
Help: "Status of the last metric scrape",
17+
})
18+
}

collector/nginx.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type NginxCollector struct {
1313
nginxClient *client.NginxClient
1414
metrics map[string]*prometheus.Desc
15+
upMetric prometheus.Gauge
1516
mutex sync.Mutex
1617
}
1718

@@ -28,12 +29,15 @@ func NewNginxCollector(nginxClient *client.NginxClient, namespace string) *Nginx
2829
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections"),
2930
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"),
3031
},
32+
upMetric: newUpMetric(namespace),
3133
}
3234
}
3335

3436
// Describe sends the super-set of all possible descriptors of NGINX metrics
3537
// to the provided channel.
3638
func (c *NginxCollector) Describe(ch chan<- *prometheus.Desc) {
39+
ch <- c.upMetric.Desc()
40+
3741
for _, m := range c.metrics {
3842
ch <- m
3943
}
@@ -46,10 +50,15 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) {
4650

4751
stats, err := c.nginxClient.GetStubStats()
4852
if err != nil {
53+
c.upMetric.Set(nginxDown)
54+
ch <- c.upMetric
4955
log.Printf("Error getting stats: %v", err)
5056
return
5157
}
5258

59+
c.upMetric.Set(nginxUp)
60+
ch <- c.upMetric
61+
5362
ch <- prometheus.MustNewConstMetric(c.metrics["connections_active"],
5463
prometheus.GaugeValue, float64(stats.Connections.Active))
5564
ch <- prometheus.MustNewConstMetric(c.metrics["connections_accepted"],

collector/nginx_plus.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type NginxPlusCollector struct {
1818
streamServerZoneMetrics map[string]*prometheus.Desc
1919
streamUpstreamMetrics map[string]*prometheus.Desc
2020
streamUpstreamServerMetrics map[string]*prometheus.Desc
21+
upMetric prometheus.Gauge
2122
mutex sync.Mutex
2223
}
2324

@@ -99,12 +100,15 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
99100
"health_checks_fails": newStreamUpstreamServerMetric(namespace, "health_checks_fails", "Failed health checks"),
100101
"health_checks_unhealthy": newStreamUpstreamServerMetric(namespace, "health_checks_unhealthy", "How many times the server became unhealthy (state 'unhealthy')"),
101102
},
103+
upMetric: newUpMetric(namespace),
102104
}
103105
}
104106

105107
// Describe sends the super-set of all possible descriptors of NGINX Plus metrics
106108
// to the provided channel.
107109
func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) {
110+
ch <- c.upMetric.Desc()
111+
108112
for _, m := range c.totalMetrics {
109113
ch <- m
110114
}
@@ -135,10 +139,15 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
135139

136140
stats, err := c.nginxClient.GetStats()
137141
if err != nil {
142+
c.upMetric.Set(nginxDown)
143+
ch <- c.upMetric
138144
log.Printf("Error getting stats: %v", err)
139145
return
140146
}
141147

148+
c.upMetric.Set(nginxUp)
149+
ch <- c.upMetric
150+
142151
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_accepted"],
143152
prometheus.CounterValue, float64(stats.Connections.Accepted))
144153
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_dropped"],

0 commit comments

Comments
 (0)