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

fix(prometheus): disable features when plugin is turned off #11103

Closed
wants to merge 5 commits into from
Closed
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
23 changes: 23 additions & 0 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ local function check_disable(plugin_conf)
return plugin_conf._meta.disable
end

--- check if the plugin is disabled with metatable
--- if the plugin_conf is nil, return true
--- if the plugin_conf._meta is nil, return true (enable default)
--- if the plugin_conf._meta.disable is nil, return false
--- true means plugin is disabled, false means plugin is enabled
local function check_disable_with_metatable(plugin_conf)
if not plugin_conf then
return true
end

if not plugin_conf._meta then
return false
end

if type(plugin_conf._meta) ~= "table" then
return true
end

return plugin_conf._meta.disable
end

_M.check_disable_with_metatable = check_disable_with_metatable

local PLUGIN_TYPE_HTTP = 1
local PLUGIN_TYPE_STREAM = 2
local PLUGIN_TYPE_HTTP_WASM = 3
Expand Down
6 changes: 4 additions & 2 deletions apisix/utils/batch-processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- limitations under the License.
--
local core = require("apisix.core")
local plugin = require("apisix.plugin")
local setmetatable = setmetatable
local timer_at = ngx.timer.at
local ipairs = ipairs
Expand Down Expand Up @@ -184,8 +185,9 @@ function batch_processor:push(entry)
return
end

if prometheus and prometheus.get_prometheus() and not batch_metrics and self.name
and self.route_id and self.server_addr then
if not plugin.check_disable_with_metatable(plugin.get("prometheus"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your PR doesn't make sense, the existing logic is just as same as yours.

and not batch_metrics and self.name
and self.route_id and self.server_addr then
batch_metrics = prometheus.get_prometheus():gauge("batch_process_entries",
"batch process remaining entries",
{"name", "route_id", "server_addr"})
Expand Down
29 changes: 29 additions & 0 deletions t/xrpc/prometheus.t
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,32 @@ passed
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_redis_commands_total\{route="1",command="hmset"\} 1/



=== TEST 9: remove public API route and test route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_DELETE
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 10: fetch the prometheus metric data while prometheus plugin is disabled
--- yaml_config
plugins:
- limit-count
--- request
GET /apisix/prometheus/metrics
--- error_code: 404
Loading