Skip to content

Commit

Permalink
Add up metrics
Browse files Browse the repository at this point in the history
Add nginx_up and nginxplus_up metrics that show the status of the
last metric scrape from NGINX or NGINX Plus.
  • Loading branch information
pleshakov committed Jan 9, 2019
1 parent ad0a472 commit 59be0a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ Usage of ./nginx-prometheus-exporter:

### Exported Metrics

* 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.
* For NGINX, the following metrics are exported:
* All [stub_status](http://nginx.org/en/docs/http/ngx_http_stub_status_module.html) metrics.
* `nginx_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one.

Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions.
* For NGINX Plus, the following metrics are exported:
* [Connections](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_connections).
* [HTTP](http://nginx.org/en/docs/http/ngx_http_api_module.html#http_).
Expand All @@ -86,6 +90,7 @@ Usage of ./nginx-prometheus-exporter:
* [Stream Server Zones](http://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_server_zone).
* [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`.
* [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`.
* `nginxplus_up` -- shows the status of the last metric scrape: `1` for a successful scrape and `0` for a failed one.


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).
Expand Down
11 changes: 11 additions & 0 deletions collector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ package collector

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

const nginxUp = 1
const nginxDown = 0

func newGlobalMetric(namespace string, metricName string, docString string) *prometheus.Desc {
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, nil)
}

func newUpMetric(namespace string) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Status of the last metric scrape",
})
}
9 changes: 9 additions & 0 deletions collector/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type NginxCollector struct {
nginxClient *client.NginxClient
metrics map[string]*prometheus.Desc
upMetric prometheus.Gauge
mutex sync.Mutex
}

Expand All @@ -28,12 +29,15 @@ func NewNginxCollector(nginxClient *client.NginxClient, namespace string) *Nginx
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections"),
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"),
},
upMetric: newUpMetric(namespace),
}
}

// Describe sends the super-set of all possible descriptors of NGINX metrics
// to the provided channel.
func (c *NginxCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.upMetric.Desc()

for _, m := range c.metrics {
ch <- m
}
Expand All @@ -46,10 +50,15 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) {

stats, err := c.nginxClient.GetStubStats()
if err != nil {
c.upMetric.Set(nginxDown)
ch <- c.upMetric
log.Printf("Error getting stats: %v", err)
return
}

c.upMetric.Set(nginxUp)
ch <- c.upMetric

ch <- prometheus.MustNewConstMetric(c.metrics["connections_active"],
prometheus.GaugeValue, float64(stats.Connections.Active))
ch <- prometheus.MustNewConstMetric(c.metrics["connections_accepted"],
Expand Down
9 changes: 9 additions & 0 deletions collector/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type NginxPlusCollector struct {
streamServerZoneMetrics map[string]*prometheus.Desc
streamUpstreamMetrics map[string]*prometheus.Desc
streamUpstreamServerMetrics map[string]*prometheus.Desc
upMetric prometheus.Gauge
mutex sync.Mutex
}

Expand Down Expand Up @@ -99,12 +100,15 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
"health_checks_fails": newStreamUpstreamServerMetric(namespace, "health_checks_fails", "Failed health checks"),
"health_checks_unhealthy": newStreamUpstreamServerMetric(namespace, "health_checks_unhealthy", "How many times the server became unhealthy (state 'unhealthy')"),
},
upMetric: newUpMetric(namespace),
}
}

// Describe sends the super-set of all possible descriptors of NGINX Plus metrics
// to the provided channel.
func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.upMetric.Desc()

for _, m := range c.totalMetrics {
ch <- m
}
Expand Down Expand Up @@ -135,10 +139,15 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {

stats, err := c.nginxClient.GetStats()
if err != nil {
c.upMetric.Set(nginxDown)
ch <- c.upMetric
log.Printf("Error getting stats: %v", err)
return
}

c.upMetric.Set(nginxUp)
ch <- c.upMetric

ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_accepted"],
prometheus.CounterValue, float64(stats.Connections.Accepted))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_dropped"],
Expand Down

0 comments on commit 59be0a4

Please sign in to comment.