-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(plugins/prometheus): add unit tests for prometheus plugin (#11157)
- Loading branch information
Showing
2 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
|
||
describe("Plugin: prometheus (unit)", function() | ||
local prometheus | ||
|
||
local orig_ngx_shared = ngx.shared | ||
local orig_ngx_get_phase = ngx.get_phase | ||
local orig_ngx_timer = ngx.timer | ||
|
||
setup(function() | ||
ngx.shared = require("spec.fixtures.shm-stub") | ||
ngx.get_phase = function() -- luacheck: ignore | ||
return "init_worker" | ||
end | ||
ngx.timer = { -- luacheck: ignore | ||
every = function() end, | ||
} | ||
|
||
package.loaded['prometheus_resty_counter'] = require("resty.counter") | ||
prometheus = require("kong.plugins.prometheus.prometheus") | ||
end) | ||
|
||
teardown(function() | ||
ngx.shared = orig_ngx_shared | ||
ngx.get_phase = orig_ngx_get_phase -- luacheck: ignore | ||
ngx.timer = orig_ngx_timer -- luacheck: ignore | ||
end) | ||
|
||
it("check metric names", function() | ||
local prom = prometheus.init("prometheus_metrics", "kong_") | ||
local m | ||
|
||
m = prom:counter("mem_used") | ||
assert.truthy(m) | ||
|
||
m = prom:counter("Mem_Used") | ||
assert.truthy(m) | ||
|
||
m = prom:counter(":mem_used") | ||
assert.truthy(m) | ||
|
||
m = prom:counter("mem_used:") | ||
assert.truthy(m) | ||
|
||
m = prom:counter("_mem_used_") | ||
assert.truthy(m) | ||
|
||
m = prom:counter("mem-used") | ||
assert.falsy(m) | ||
|
||
m = prom:counter("0name") | ||
assert.falsy(m) | ||
|
||
m = prom:counter("name$") | ||
assert.falsy(m) | ||
end) | ||
|
||
it("check metric label names", function() | ||
local prom = prometheus.init("prometheus_metrics", "kong_") | ||
local m | ||
|
||
m = prom:counter("mem0", nil, {"LUA"}) | ||
assert.truthy(m) | ||
|
||
m = prom:counter("mem1", nil, {"lua"}) | ||
assert.truthy(m) | ||
|
||
m = prom:counter("mem2", nil, {"_lua_"}) | ||
assert.truthy(m) | ||
|
||
m = prom:counter("mem3", nil, {":lua"}) | ||
assert.falsy(m) | ||
|
||
m = prom:counter("mem4", nil, {"0lua"}) | ||
assert.falsy(m) | ||
|
||
m = prom:counter("mem5", nil, {"lua*"}) | ||
assert.falsy(m) | ||
|
||
m = prom:counter("mem6", nil, {"lua\\5.1"}) | ||
assert.falsy(m) | ||
|
||
m = prom:counter("mem7", nil, {"lua\"5.1\""}) | ||
assert.falsy(m) | ||
|
||
m = prom:counter("mem8", nil, {"lua-vm"}) | ||
assert.falsy(m) | ||
end) | ||
|
||
it("check metric full name", function() | ||
local prom = prometheus.init("prometheus_metrics", "kong_") | ||
local shm = ngx.shared["prometheus_metrics"] | ||
local m | ||
|
||
m = prom:counter("mem", nil, {"lua"}) | ||
assert.truthy(m) | ||
m:inc(1, {"2.1"}) | ||
|
||
m = prom:counter("file", nil, {"path"}) | ||
assert.truthy(m) | ||
m:inc(1, {"\\root"}) | ||
|
||
m = prom:counter("user", nil, {"name"}) | ||
assert.truthy(m) | ||
m:inc(1, {"\"quote"}) | ||
|
||
-- sync to shdict | ||
prom._counter:sync() | ||
|
||
assert.equal(shm:get([[mem{lua="2.1"}]]), 1) | ||
assert.equal(shm:get([[file{path="\\root"}]]), 1) | ||
assert.equal(shm:get([[user{name="\"quote"}]]), 1) | ||
end) | ||
|
||
it("emit metric data", function() | ||
local prom = prometheus.init("metrics", "kong_") | ||
local m | ||
|
||
m = prom:counter("mem", nil, {"lua"}) | ||
assert.truthy(m) | ||
m:inc(2, {"2.1"}) | ||
|
||
m = prom:counter("file", nil, {"path"}) | ||
assert.truthy(m) | ||
m:inc(3, {"\\root"}) | ||
|
||
m = prom:counter("user", nil, {"name"}) | ||
assert.truthy(m) | ||
m:inc(5, {"\"quote"}) | ||
m:inc(1, {"\"quote"}) | ||
|
||
local str = "" | ||
prom:metric_data(function(d) | ||
str = str .. d | ||
end) | ||
|
||
assert.truthy(str:find([[kong_mem{lua="2.1"} 2]], 1, true)) | ||
assert.truthy(str:find([[kong_file{path="\\root"} 3]], 1, true)) | ||
assert.truthy(str:find([[kong_user{name="\"quote"} 6]], 1, true)) | ||
end) | ||
|
||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
-- DICT Proxy | ||
-- https://github.com/bsm/fakengx/blob/master/fakengx.lua | ||
|
||
local SharedDict = {} | ||
|
||
local function set(data, key, value) | ||
data[key] = { | ||
value = value, | ||
info = {expired = false} | ||
} | ||
end | ||
|
||
function SharedDict:new() | ||
return setmetatable({data = {}}, {__index = self}) | ||
end | ||
|
||
function SharedDict:get(key) | ||
return self.data[key] and self.data[key].value, nil | ||
end | ||
|
||
function SharedDict:set(key, value) | ||
set(self.data, key, value) | ||
return true, nil, false | ||
end | ||
|
||
SharedDict.safe_set = SharedDict.set | ||
|
||
function SharedDict:add(key, value) | ||
if self.data[key] ~= nil then | ||
return false, "exists", false | ||
end | ||
|
||
set(self.data, key, value) | ||
return true, nil, false | ||
end | ||
|
||
function SharedDict:replace(key, value) | ||
if self.data[key] == nil then | ||
return false, "not found", false | ||
end | ||
|
||
set(self.data, key, value) | ||
return true, nil, false | ||
end | ||
|
||
function SharedDict:delete(key) | ||
if self.data[key] ~= nil then | ||
self.data[key] = nil | ||
end | ||
end | ||
|
||
function SharedDict:incr(key, value, init) | ||
if not self.data[key] then | ||
if not init then | ||
return nil, "not found" | ||
else | ||
set(self.data, key, init) | ||
end | ||
elseif type(self.data[key]) ~= "table" then | ||
return nil, "not a table" | ||
end | ||
|
||
self.data[key].value = self.data[key].value + value | ||
return self.data[key].value, nil | ||
end | ||
|
||
function SharedDict:flush_all() | ||
for _, item in pairs(self.data) do | ||
item.info.expired = true | ||
end | ||
end | ||
|
||
function SharedDict:flush_expired(n) | ||
local data = self.data | ||
local flushed = 0 | ||
|
||
for key, item in pairs(self.data) do | ||
if item.info.expired then | ||
data[key] = nil | ||
flushed = flushed + 1 | ||
if n and flushed == n then | ||
break | ||
end | ||
end | ||
end | ||
|
||
self.data = data | ||
|
||
return flushed | ||
end | ||
|
||
function SharedDict:get_keys() | ||
local keys = {} | ||
for k, _ in pairs(self.data) do | ||
table.insert(keys, k) | ||
end | ||
|
||
return keys | ||
end | ||
|
||
local shared_mt = { | ||
__index = function(self, key) | ||
if rawget(self, key) == nil then | ||
self[key] = SharedDict:new() | ||
end | ||
return self[key] | ||
end | ||
} | ||
|
||
return setmetatable({}, shared_mt) |
852071b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bazel Build
Docker image available
kong/kong:852071b0315f66cc8622721f7519c830d167a2b8
Artifacts available https://github.com/Kong/kong/actions/runs/5440673317