1
0
mirror of https://github.com/cmur2/nginx-metrics-graphite.git synced 2024-11-15 02:56:17 +01:00
nginx-metrics-graphite/README.md

62 lines
3.5 KiB
Markdown
Raw Normal View History

2016-11-23 19:40:22 +01:00
# nginx-metrics-graphite
This is a Lua plugin for the Nginx web server that automatically collects and submits several important Nginx metrics to [Graphite](https://graphiteapp.org/) suitable for visualisation with e.g. [Grafana](http://grafana.org/).
2016-11-29 20:26:43 +01:00
In constrast to commercial and proprietary solutions such as [Luameter](https://luameter.com/) or [NGINX Plus](https://www.nginx.com/products/) with it's [ngx_http_status_module](http://nginx.org/en/docs/http/ngx_http_status_module.html), this plugin is open source software while featuring more and additional metrics compared to those available via the open source [ngx_http_stub_status_module](http://nginx.org/en/docs/http/ngx_http_stub_status_module.html). (Yea, [other web servers](https://redmine.lighttpd.net/projects/1/wiki/Docs_ModStatus) deliver more information by default...)
2016-11-23 19:40:22 +01:00
This plugin takes inspiration from other Nginx metric libraries like [nginx-lua-prometheus](https://github.com/knyar/nginx-lua-prometheus) but differs fundamentally in the metrics submission handling by. In certain intervals it automatically pushes metrics to the configured Graphite (i.e. Carbon) host using pure Lua code instead of exposing them via a separate web page for polling.
The metrics collection happens on every request for which the user configures a suitable `log_by_lua` direcitve and towards server-wide global counters (finer granularity might be added later). The counters are realized using a single shared dictionary across all Nginx worker threads which has constant memory usage (128 KiB currently, may be reduced further).
Collected metrics in this prototype implementation:
2016-11-29 20:26:43 +01:00
* numbers of requests (total, to upstream, using ssl, using gzip)
2016-11-23 19:40:22 +01:00
* average request duration
* accumulated request sizes over all requests
* accumulated response sizes over all requests
* HTTP status code classes (1xx, 2xx, 3xx, 4xx, 5xx)
* HTTP methods (GET, HEAD, PUT, POST, DELETE, OPTIONS, others)
2016-11-29 20:26:43 +01:00
* HTTP versions (0.9, 1.0, 1.1, 2.0)
Successfully tested with:
* Nginx 1.6.2 and ngx_lua 0.9.12 on Debian Jessie
* Nginx 1.10.3 and ngx_lua 0.10.7 on Debian Stretch
* Nginx 1.14.0 and ngx_lua 0.10.13 from backports on Debian Stretch
2016-11-23 19:40:22 +01:00
## Caveats
A short metric submission interval might cause blocking on the Nginx worker threads since the shared dictionary storing all counters has to be locked.
2016-11-29 20:26:43 +01:00
Intermittent network errors while communicating with Graphite might leed to permanent loss of metric information. The communication happens in clear text and thus needs a secure separate network or other means.
2016-11-23 19:40:22 +01:00
## Install
* Install `nginx-extra` (includes Lua support) on Debian Jessie and Debian Stretch
2016-11-23 19:40:22 +01:00
* Clone the nginx-metrics-graphite repository to */opt/nginx-metrics-graphite*
2016-11-29 20:26:43 +01:00
* Add the following config to top-level `http` block (300 second submission interval):
2016-11-23 19:40:22 +01:00
```nginx
resolver x.y.z.w; # DNS resolver IP address needed
lua_shared_dict metrics_graphite 128k;
lua_package_path ";;/opt/nginx-metrics-graphite/?.lua";
init_by_lua 'metrics_graphite = require("metrics_graphite").init({"graphite.example.net"}, 300, "my.node.prefix")';
2016-11-23 19:40:22 +01:00
init_worker_by_lua 'metrics_graphite:worker()';
```
* Instrument the `http` block or any server or location beneath it using `log_by_lua 'metrics_graphite:log()';`
2016-11-23 19:43:56 +01:00
2016-11-29 20:26:43 +01:00
## Development
```sh
apt-get install luarocks # on Debian
luarocks --local install luacheck
luacheck --globals ngx -- metrics_graphite.lua
```
2016-11-23 19:43:56 +01:00
## License
nginx-metrics-graphite is licensed under the Apache License, Version 2.0. See LICENSE for more information.