diff --git a/README.md b/README.md index 4637176..67890ac 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ init_by_lua_block { cache_errors = tonumber(os.getenv("IPREPD_CACHE_ERRORS")), statsd_host = os.getenv("STATSD_HOST") or nil, statsd_port = tonumber(os.getenv("STATSD_PORT")) or 8125, - statsd_buffer_size = tonumber(os.getenv("STATSD_BUFFER_SIZE")) or 100, + statsd_max_buffer_count = tonumber(os.getenv("STATSD_MAX_BUFFER_COUNT")) or 100, }) } @@ -99,8 +99,8 @@ violations for your environment. -- infrastructure. (defaults to disabled) -- statsd_host - Host of statsd collector. Setting this will enable statsd metrics collection -- statsd_port - Port of statsd collector. (defaults to 8125) --- statsd_buffer_size - Statsd buffer table length at which stats should be sent to --- the collector. (defaults to 100) +-- statsd_max_buffer_count - Max number of metrics in buffer before metrics should be submitted +-- to statsd (defaults to 100) -- client = require("resty.iprepd").new({ url = "http://127.0.0.1:8080", @@ -111,7 +111,7 @@ client = require("resty.iprepd").new({ cache_errors = 1, statsd_host = "127.0.0.1", statsd_port = 8125, - statsd_buffer_size = 100, + statsd_max_buffer_count = 100, }) ``` @@ -148,5 +148,5 @@ IPREPD_CACHE_TTL=60 # iprepd response cache ttl in seconds (default is 30s) IPREPD_CACHE_ERRORS=1 # enables caching iprepd non-200 responses (1 enables, 0 disables, default is 0) STATSD_HOST=127.0.0.1 # statsd host, setting this will also enable statsd metrics collection. STATSD_PORT=8125 # statsd port (default is 8125) -STATSD_BUFFER_SIZE=200 # statsd buffer size (default is 100) +STATSD_MAX_BUFFER_COUNT=200 # statsd max number of buffer items before submitting (default is 100) ``` diff --git a/etc/conf.d/server.conf b/etc/conf.d/server.conf index b070c81..7ee3a0e 100644 --- a/etc/conf.d/server.conf +++ b/etc/conf.d/server.conf @@ -8,7 +8,7 @@ init_by_lua_block { cache_errors = tonumber(os.getenv("IPREPD_CACHE_ERRORS")), statsd_host = os.getenv("STATSD_HOST") or nil, statsd_port = tonumber(os.getenv("STATSD_PORT")) or 8125, - statsd_buffer_size = tonumber(os.getenv("STATSD_BUFFER_SIZE")) or 100, + statsd_max_buffer_count = tonumber(os.getenv("STATSD_MAX_BUFFER_COUNT")) or 100, }) } diff --git a/lib/resty/iprepd.lua b/lib/resty/iprepd.lua index f280002..1b3fd80 100644 --- a/lib/resty/iprepd.lua +++ b/lib/resty/iprepd.lua @@ -47,7 +47,7 @@ function _M.new(options) statsd = statsd_client, statsd_host = options.statsd_host, statsd_port = options.statsd_port or 8125, - statsd_buffer_size = options.statsd_buffer_size or 100, + statsd_max_buffer_count = options.statsd_max_buffer_count or 100, } return setmetatable(self, mt) end @@ -108,7 +108,7 @@ end function _M.flush_stats(self) if self.statsd then - if #self.statsd.buffer >= self.statsd_buffer_size then + if self.statsd.buffer_count() >= self.statsd_max_buffer_count then self.statsd.flush(self.statsd_host, self.statsd_port) end end diff --git a/lib/resty/statsd.lua b/lib/resty/statsd.lua index bfb883f..97121ae 100644 --- a/lib/resty/statsd.lua +++ b/lib/resty/statsd.lua @@ -3,28 +3,31 @@ -- local _M = {} --- this table will be shared per worker process --- if lua_code_cache is off, it will be cleared every request -_M.buffer = {} +_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) + ngx.log(ngx.ERR, 'Error sending stats to statsd at ' .. host .. ':' .. port) end - for k in pairs(_M.buffer) do - _M.buffer[k] = nil - 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