From 2940b3b43024ccaa6889e69d59c79612b2860545 Mon Sep 17 00:00:00 2001 From: An Tran Date: Mon, 29 Jun 2026 17:55:41 +1000 Subject: [PATCH] chore(deps): upgrade nginx-lua-prometheus to 0.20220527 --- CHANGELOG.md | 1 + Dockerfile | 2 +- gateway/Roverfile.lock | 2 +- gateway/apicast-scm-1.rockspec | 2 +- gateway/src/apicast/executor.lua | 1 + gateway/src/apicast/prometheus.lua | 24 +++++++++--- spec/prometheus_spec.lua | 62 +++++++++++++++++++----------- 7 files changed, 62 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e92171380..985b97d45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Dockerfile b/Dockerfile index d031c7d31..aa2b06ad7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/gateway/Roverfile.lock b/gateway/Roverfile.lock index 29691220e..554de9c32 100644 --- a/gateway/Roverfile.lock +++ b/gateway/Roverfile.lock @@ -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 \ No newline at end of file diff --git a/gateway/apicast-scm-1.rockspec b/gateway/apicast-scm-1.rockspec index 8fefbd822..5fbc585b0 100644 --- a/gateway/apicast-scm-1.rockspec +++ b/gateway/apicast-scm-1.rockspec @@ -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' diff --git a/gateway/src/apicast/executor.lua b/gateway/src/apicast/executor.lua index ff80e1f7d..7f40f483d 100644 --- a/gateway/src/apicast/executor.lua +++ b/gateway/src/apicast/executor.lua @@ -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 = {} diff --git a/gateway/src/apicast/prometheus.lua b/gateway/src/apicast/prometheus.lua index a41756e64..1861588e2 100644 --- a/gateway/src/apicast/prometheus.lua +++ b/gateway/src/apicast/prometheus.lua @@ -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 diff --git a/spec/prometheus_spec.lua b/spec/prometheus_spec.lua index ffbbec3c1..dae5568f6 100644 --- a/spec/prometheus_spec.lua +++ b/spec/prometheus_spec.lua @@ -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() @@ -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)