Skip to content

Commit

Permalink
emit stats
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jul 19, 2024
1 parent 467628d commit 942cefc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
6 changes: 1 addition & 5 deletions kong/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,7 @@ end


function _M.stats()
local stats = {}
for k, v in pairs(dns_client.stats) do
stats[format_key(k)] = v
end
return stats
return dns_client.stats:emit(format_key)
end


Expand Down
32 changes: 25 additions & 7 deletions kong/dns/stats.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
local tb_new = require(table.new)


local pairs = pairs
local setmetatable = setmetatable


Expand All @@ -14,25 +18,39 @@ function _M.new()
end


function _M:incr(name, key)
function _M:_get_stats(name)
local stats = self.stats

if not stats[name] then
stats[name] = {}
stats[name] = tb_new(0, 6)
end

stats[name][key] = (stats[name][key] or 0) + 1
return stats[name]
end


function _M:incr(name, key)
local stats = self:_get_stats(name)

stats[key] = (stats[key] or 0) + 1
end


function _M:set(name, key, value)
local stats = self.stats
local stats = self:_get_stats(name)

if not stats[name] then
stats[name] = {}
stats[key] = value
end


function _M:emit(fmt)
local output = {}

for k, v in pairs(self.stats) do
output[fmt(k)] = v
end

stats[name][key] = value
return output
end


Expand Down

0 comments on commit 942cefc

Please sign in to comment.