A Lua module for openresty to send metrics to StatsD
- increments!
- counts!
- timers!
- cosocket frolics
- batchin'
- Install openresty configured
--with-luajit
- Copy
lib/statsd.lua
somewhere that openresty nginx can find (you may need to adjust yourLUA_PATH
or uselua_package_path
directive - Configure nginx:
# an nginx conf
http {
# optionally set relative lua_package_path
lua_package_path "${prefix}lua/*.lua";
# create a shared dictionary to for statsd. The default name is STATSD
lua_shared_dict STATSD 20k;
location /some_location {
content_by_lua '
-- this is the phase where metrics are registered
local statsd = require 'statsd'
local s = statsd:new()
s:incr("test.status." .. ngx.var.status)
s:time("test.req_time", ngx.now() - ngx.req.start_time())
';
log_by_lua '
-- this is the phase where metrics are sent
-- batch metrics into packets of 50 metrics by default
local statsd = require 'statsd'
local s = statsd:new()
s:flush()
';
}
}
The request-response lifecycle in nginx has eight phases. The data you are likely to want to report (HTTP status, request time) is available in the last phase, log
, but the socket API is not available. That's why stats are registered in log_by_lua
and sent via flush
in content_by_lua
.
- 0.0.1: Works. Tested.
- 0.0.2: Uses a dictionary
- luarocks
- Clone the repo
luarocks install md5 [--local]
luarocks install mime [--local]
luarocks install busted [--local]
Then on the project folder console just run:
busted
- lua-statsd - doesn't use openresty's cosockets
- nginx-statsd - written in C
- fozzie - our Ruby StatsD client