Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
* Removed comment about lua_code_cache directive
* Added array length 'count' into buffer table
* Changed statsd_buffer_size -> statsd_max_buffer_count
  • Loading branch information
ajvb committed Nov 7, 2018
1 parent 506ae1f commit 23f39dd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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",
Expand All @@ -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,
})
```

Expand Down Expand Up @@ -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)
```
2 changes: 1 addition & 1 deletion etc/conf.d/server.conf
Expand Up @@ -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,
})
}

Expand Down
4 changes: 2 additions & 2 deletions lib/resty/iprepd.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions lib/resty/statsd.lua
Expand Up @@ -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

Expand Down

0 comments on commit 23f39dd

Please sign in to comment.