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

Implement up metrics and timeout cli argument #31

Merged
merged 2 commits into from
Jan 11, 2019
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Usage of ./nginx-prometheus-exporter:
A URI for scraping NGINX or NGINX Plus metrics.
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable. (default "http://127.0.0.1:8080/stub_status")
-nginx.ssl-verify
Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable.
Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable. (default true)
-nginx.timeout duration
A timeout for scraping metrics from NGINX or NGINX Plus. (default 5s)
-web.listen-address string
An address to listen on for web interface and telemetry. The default value can be overwritten by LISTEN_ADDRESS environment variable. (default ":9113")
-web.telemetry-path string
Expand All @@ -77,7 +79,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 +92,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
13 changes: 9 additions & 4 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strconv"
"time"

plusclient "github.com/nginxinc/nginx-plus-go-sdk/client"
"github.com/nginxinc/nginx-prometheus-exporter/client"
Expand Down Expand Up @@ -59,6 +60,7 @@ var (
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable.`)
sslVerify = flag.Bool("nginx.ssl-verify", defaultSslVerify,
"Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable.")
timeout = flag.Duration("nginx.timeout", 5*time.Second, "A timeout for scraping metrics from NGINX or NGINX Plus.")
)

func main() {
Expand All @@ -68,19 +70,22 @@ func main() {

registry := prometheus.NewRegistry()

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !*sslVerify},
httpClient := &http.Client{
Timeout: *timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !*sslVerify},
},
}

if *nginxPlus {
client, err := plusclient.NewNginxClient(&http.Client{Transport: tr}, *scrapeURI)
client, err := plusclient.NewNginxClient(httpClient, *scrapeURI)
if err != nil {
log.Fatalf("Could not create Nginx Plus Client: %v", err)
}

registry.MustRegister(collector.NewNginxPlusCollector(client, "nginxplus"))
} else {
client, err := client.NewNginxClient(&http.Client{Transport: tr}, *scrapeURI)
client, err := client.NewNginxClient(httpClient, *scrapeURI)
if err != nil {
log.Fatalf("Could not create Nginx Client: %v", err)
}
Expand Down