Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace safe_set with set #147

Merged
merged 6 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,16 @@ end
-- value: numeric value to increment by. Can be negative.
-- label_values: a list of label values, in the same order as label keys.
local function inc_gauge(self, value, label_values)
local k, err, _
local k, err, _, forcible
k, err = lookup_or_create(self, label_values)
if err then
self._log_error(err)
return
end

_, err, _ = self._dict:incr(k, value, 0)
if err then
self._log_error_kv(k, value, err)
_, err, forcible = self._dict:incr(k, value, 0)
if err or forcible then
self._log_error_kv(k, value, err or "lru eviction")
end
end

Expand Down Expand Up @@ -521,15 +521,15 @@ local function set(self, value, label_values)
return
end

local k, _, err
local k, _, err, forcible
k, err = lookup_or_create(self, label_values)
if err then
self._log_error(err)
return
end
_, err = self._dict:safe_set(k, value)
if err then
self._log_error_kv(k, value, err)
_, err, forcible = self._dict:set(k, value)
if err or forcible then
self._log_error_kv(k, value, err or "lru eviction")
end
end

Expand Down Expand Up @@ -632,7 +632,7 @@ local function reset(self)
end
if remove then
self._key_index:remove(key)
local _, err = self._dict:safe_set(key, nil)
local _, err = self._dict:set(key, nil)
if err then
self._log_error("Error resetting '", key, "': ", err)
end
Expand Down
8 changes: 6 additions & 2 deletions prometheus_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ function KeyIndex:add(key_or_keys)
break
end
N = N+1
local ok, err = self.dict:safe_add(self.key_prefix .. N, key)
local ok, err, forcible = self.dict:add(self.key_prefix .. N, key)
if forcible then
ngx.log(ngx.ERR, "key index: add key: shdict lru eviction: idx=",
self.key_prefix .. N, ", key=", key)
end
if ok then
self.dict:incr(self.key_count, 1, 0)
self.keys[N] = key
Expand All @@ -107,7 +111,7 @@ function KeyIndex:remove(key)
if i then
self.index[key] = nil
self.keys[i] = nil
self.dict:safe_set(self.key_prefix .. i, nil)
self.dict:set(self.key_prefix .. i, nil)
-- increment delete_count to signalize other workers that they should do a full sync
self.dict:incr(self.delete_count, 1, 0)
self.deleted = self.deleted + 1
Expand Down
7 changes: 5 additions & 2 deletions prometheus_resty_counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ local timer_started = {}
local id

local function sync(_, self)
local err, _
local err, _, forcible
local ok = true
for k, v in pairs(self.increments) do
_, err, _ = self.dict:incr(k, v, 0)
_, err, forcible = self.dict:incr(k, v, 0)
if forcible then
ngx.log(ngx.WARN, "increasing counter in shdict: lru eviction: key=", k)
end
if err then
ngx.log(ngx.WARN, "error increasing counter in shdict key: ", k, ", err: ", err)
ok = false
Expand Down
24 changes: 1 addition & 23 deletions prometheus_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ function SimpleDict:safe_set(k, v)
self:set(k, v)
return true, nil -- ok, err
end
function SimpleDict:safe_add(k, v)
if k == "willnotfit" or v == "willnotfit" then
return nil, "no memory"
end
function SimpleDict:add(k, v)
self:set(k, v)
return true, nil -- ok, err
end
function SimpleDict:incr(k, v, init)
if k:find("willnotfit") then
return nil, "no memory"
end
if not self.dict[k] then self.dict[k] = init end
self.dict[k] = self.dict[k] + (v or 1)
return self.dict[k], nil -- newval, err
Expand Down Expand Up @@ -168,17 +162,6 @@ function TestPrometheus.testErrorUnknownDict()
luaunit.assertEquals(pok, false)
luaunit.assertStrContains(perr, "does not seem to exist")
end
function TestPrometheus:testErrorNoMemory()
local gauge3 = self.p:gauge("willnotfit")
self.counter1:inc(5)
gauge3:inc(1)

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("willnotfit"), nil)
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 Expand Up @@ -703,11 +686,6 @@ function TestKeyIndex:testAdd()
self.key_index:add("single")
luaunit.assertEquals(ngx.logs, nil)
luaunit.assertEquals(self.dict:get("_prefix_key_count"), 3)

-- error should be returned when memory is full
local err = self.key_index:add("willnotfit")
luaunit.assertEquals(err, "Unexpected error adding a key: no memory")
luaunit.assertEquals(self.dict:get("_prefix_key_count"), 3)
end
function TestKeyIndex:testRemove()
self.key_index:add({"key1", "key2", "key3"})
Expand Down