Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Bump pcre to pcre2 10.47 [PR #1577](https://github.com/3scale/APIcast/pull/1577) [THREESCALE-14541](https://redhat.atlassian.net/browse/THREESCALE-14541)
- Bump zlib to 1.3.1 [PR #1577](https://github.com/3scale/APIcast/pull/1577) [THREESCALE-12242](https://redhat.atlassian.net/browse/THREESCALE-12242)
- Bump liquid-lua to 0.2.1 [PR #1590](https://github.com/3scale/APIcast/pull/1590)
- Bump nginx-lua-prometheus to 0.20220527 [PR #1591](https://github.com/3scale/APIcast/pull/1591)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/man
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/3scale/lua-resty-execvp-0.1.1-1.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/hisham/luafilesystem-1.8.0-1.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/thibaultcha/lua-resty-jit-uuid-0.0.7-2.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/knyar/nginx-lua-prometheus-0.20181120-2.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/knyar/nginx-lua-prometheus-0.20220527-1.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/hamish/lua-resty-iputils-0.3.0-1.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/golgote/net-url-0.9-1.src.rock
RUN luarocks install --deps-mode=none --tree /usr/local https://luarocks.org/manifests/membphis/lua-resty-ipmatcher-0.6.1-0.src.rock
Expand Down
2 changes: 1 addition & 1 deletion gateway/Roverfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lyaml 6.2.8-1||production
markdown 0.33-1|8c09109924b218aaecbfd4d4b1de538269c4d765|development
mediator_lua 1.1.2-0||testing
net-url 1.1-1|32acd84d06e16ddffc975adafce9cea26f3b2dd1|testing
nginx-lua-prometheus 0.20181120-3|379c0a4d4d6f3c5b0eb93691fc7e14fff498e1ca|production
nginx-lua-prometheus 0.20220527-1||production
penlight 1.15.0-1||testing,development,production
router 2.1-0||production
say 1.4.1-3|45a3057e68c52b34ab59ef167efeb2340e356661|testing
2 changes: 1 addition & 1 deletion gateway/apicast-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = {
'liquid == 0.2.1',
'argparse',
'penlight == 1.15.0',
'nginx-lua-prometheus == 0.20181120',
'nginx-lua-prometheus == 0.20220527',
'lua-resty-jit-uuid',
'lua-resty-ipmatcher',
'lua-resty-openssl == 1.7.1'
Expand Down
1 change: 1 addition & 0 deletions gateway/src/apicast/executor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ do
-- Need to seed the UUID in init_worker.
-- Ref: https://github.com/thibaultcha/lua-resty-jit-uuid/blob/c4c0004da0c4c4cdd23644a5472ea5c0d18decbb/README.md#usage
uuid.seed()
prometheus.init_worker()

local executed = {}

Expand Down
24 changes: 18 additions & 6 deletions gateway/src/apicast/prometheus.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
local prometheus = require('nginx.prometheus')
local assert = assert
local dict = 'prometheus_metrics'

if ngx.shared[dict] then
local init = prometheus.init(dict)

local metrics = { }
local prometheus = require('prometheus').init(dict)

local __call = function(_, type, name, ...)
local metric_name = assert(name, 'missing metric name')

if not metrics[metric_name] then
metrics[metric_name] = init[assert(type, 'missing metric type')](init, metric_name, ...)
metrics[metric_name] = prometheus[assert(type, 'missing metric type')](prometheus, metric_name, ...)
end

return metrics[metric_name]
end

return setmetatable({ }, { __call = __call, __index = init })
local function init_worker()
prometheus:init_worker()
end

local function collect()
prometheus:collect()
end

return setmetatable({
init_worker = init_worker,
collect = collect,
}, { __call = __call})
else
local noop = function() end
return setmetatable({ collect = noop }, { __call = noop })
local noop_metric = { inc = noop, set = noop, observe = noop, del = noop, reset = noop }
local __call = function() return noop_metric end
return setmetatable({ collect = noop, init_worker = noop }, { __call = __call })
end
62 changes: 39 additions & 23 deletions spec/prometheus_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@

describe('prometheus', function()
before_each(function() package.loaded['apicast.prometheus'] = nil end)
before_each(function()
package.loaded['apicast.prometheus'] = nil
package.loaded['prometheus'] = nil
end)

describe('shared dictionary is missing', function()
before_each(function() ngx.shared.prometheus_metrics = nil end)

it('can be called', function()
assert.is_nil(require('apicast.prometheus')())
local prom = require('apicast.prometheus')
local metric = prom()
assert.is_not_nil(metric)
assert.is_function(metric.inc)
end)

it('can be collected', function()
Expand All @@ -15,37 +21,47 @@ describe('prometheus', function()
end)

describe('shared dictionary is there', function()
local saved_get_phase

before_each(function()
ngx.shared.prometheus_metrics = {
set = function() end,
get_keys = function() return {} end
set = function() return true end,
safe_set = function() return true end,
safe_add = function() return true end,
incr = function() return 0 end,
get = function() end,
delete = function() return true end,
get_keys = function() return {} end,
}
saved_get_phase = ngx.get_phase
ngx.get_phase = function() return 'init' end
end)

local prometheus
local Prometheus

before_each(function()
prometheus = assert(require('apicast.prometheus'))
Prometheus = getmetatable(prometheus).__index
after_each(function()
ngx.get_phase = saved_get_phase
end)

for _,metric_type in pairs{ 'counter', 'gauge', 'histogram' } do
describe(metric_type, function()
it('can be called', function()
stub(Prometheus, metric_type)

prometheus(metric_type, 'some_metric')
it('returns a callable wrapper', function()
local prometheus = assert(require('apicast.prometheus'))
local metric = prometheus('counter', 'test_metric', 'A test counter')
assert.is_not_nil(metric)
end)

assert.stub(Prometheus[metric_type]).was.called_with(Prometheus, 'some_metric')
end)
end)
end
it('caches metrics by name', function()
local prometheus = assert(require('apicast.prometheus'))
local m1 = prometheus('counter', 'cached_metric', 'A counter')
local m2 = prometheus('counter', 'cached_metric', 'A counter')
assert.are.equal(m1, m2)
end)

it('exposes collect', function()
local prometheus = assert(require('apicast.prometheus'))
assert.is_function(prometheus.collect)
end)

it('can be collected', function()
ngx.header = { }
assert.is_nil(require('apicast.prometheus'):collect())
it('exposes init_worker', function()
local prometheus = assert(require('apicast.prometheus'))
assert.is_function(prometheus.init_worker)
end)
end)

Expand Down
Loading