Skip to content

Commit

Permalink
Increment error metric on resty_counter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
knyar committed Aug 5, 2022
1 parent dd230d2 commit 3a33ce3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ license = mit
lib_dir = .
doc_dir = .
main_module = prometheus.lua
exclude_files = prometheus_test.lua, integration, prometheus_resty_counter.update.sh
exclude_files = prometheus_test.lua, integration
repo_link = https://github.com/knyar/nginx-lua-prometheus
2 changes: 1 addition & 1 deletion prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ function Prometheus:init_worker(sync_interval)
end
self.sync_interval = sync_interval or DEFAULT_SYNC_INTERVAL
local counter_instance, err = resty_counter_lib.new(
self.dict_name, self.sync_interval)
self.dict_name, self.sync_interval, self.error_metric_name)
if err then
error(err, 2)
end
Expand Down
8 changes: 7 additions & 1 deletion prometheus_resty_counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local function sync(_, self)
_, err, forcible = self.dict:incr(k, v, 0)
if forcible then
ngx.log(ngx.ERR, "increasing counter in shdict: lru eviction: key=", k)
ok = false
end
if err then
ngx.log(ngx.ERR, "error increasing counter in shdict key: ", k, ", err: ", err)
Expand All @@ -45,10 +46,14 @@ local function sync(_, self)
end

clear_tab(self.increments)
if ok == false then
self.dict:incr(self.error_metric_name, 1, 0)
end

return ok
end

function _M.new(shdict_name, sync_interval)
function _M.new(shdict_name, sync_interval, error_metric_name)
id = ngx.worker.id()

if not ngx_shared[shdict_name] then
Expand All @@ -62,6 +67,7 @@ function _M.new(shdict_name, sync_interval)
local self = setmetatable({
dict = ngx_shared[shdict_name],
increments = increments[shdict_name],
error_metric_name = error_metric_name,
}, mt)

if sync_interval then
Expand Down
10 changes: 0 additions & 10 deletions prometheus_resty_counter.update.sh

This file was deleted.

13 changes: 12 additions & 1 deletion prometheus_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function TestPrometheus.testErrorUnknownDict()
luaunit.assertEquals(pok, false)
luaunit.assertStrContains(perr, "does not seem to exist")
end
function TestPrometheus:testErrorNoMemory()
function TestPrometheus:testErrorNoMemoryGauge()
local gauge3 = self.p:gauge("willnotfitk")
self.counter1:inc(5)
gauge3:inc(111)
Expand All @@ -181,6 +181,17 @@ function TestPrometheus:testErrorNoMemory()
luaunit.assertEquals(self.dict:get("willnotfitk"), 111)
luaunit.assertEquals(#ngx.logs, 1)
end
function TestPrometheus:testErrorNoMemoryCounter()
local counter = self.p:counter("willnotfitk")
self.counter1:inc(5)
counter:inc(11)

self.p._counter:sync()
luaunit.assertEquals(self.dict:get("metric1"), 5)
luaunit.assertEquals(self.dict:get("nginx_metric_errors_total"), 1)
luaunit.assertEquals(self.dict:get("willnotfitk"), 11)
luaunit.assertEquals(#ngx.logs, 1)
end
function TestPrometheus:testErrorInvalidMetricName()
self.p:histogram("name with a space", "Histogram")
self.p:gauge("nonprintable\004characters", "Gauge")
Expand Down

0 comments on commit 3a33ce3

Please sign in to comment.