/
statsd.lua
50 lines (40 loc) · 942 Bytes
/
statsd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--
-- Forked from https://github.com/lonelyplanet/openresty-statsd
--
local _M = {}
_M.buffer = {count=0}
function _M.flush(host, port)
if pcall(function()
local udp = ngx.socket.udp()
udp:setpeername(host, port)
_M.buffer['count'] = nil
udp:send(_M.buffer)
udp:close()
end) then
-- pass
else
ngx.log(ngx.ERR, 'Error sending stats to statsd at ' .. host .. ':' .. port)
end
-- reset buffer
_M.buffer = {count=0}
end
function _M.buffer_count()
return _M.buffer['count']
end
function _M.register(bucket, suffix)
_M.buffer['count'] = _M.buffer['count'] + 1
table.insert(_M.buffer, bucket .. ':' .. suffix .. '\n')
end
function _M.time(bucket, time)
_M.register(bucket, time .. '|ms')
end
function _M.set(bucket, value)
_M.register(bucket, value .. '|s')
end
function _M.count(bucket, n)
_M.register(bucket, n .. '|c')
end
function _M.incr(bucket)
_M.count(bucket, 1)
end
return _M