This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 60547a947 chore: someone's plugin needs to be initialized after 
prometheus (#7423)
60547a947 is described below

commit 60547a9475c064cdd050cd1782c9086af6a18a4b
Author: 罗泽轩 <[email protected]>
AuthorDate: Tue Jul 19 08:56:16 2022 +0800

    chore: someone's plugin needs to be initialized after prometheus (#7423)
    
    Signed-off-by: spacewander <[email protected]>
---
 apisix/core/table.lua |  4 ++++
 apisix/plugin.lua     | 27 +++++++++++++++++++++------
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/apisix/core/table.lua b/apisix/core/table.lua
index 2a6bb47ce..b307cc25d 100644
--- a/apisix/core/table.lua
+++ b/apisix/core/table.lua
@@ -91,6 +91,10 @@ end
 -- local arr = {"a", "b", "c"}
 -- local idx = core.table.array_find(arr, "b") -- idx = 2
 function _M.array_find(array, val)
+    if type(array) ~= "table" then
+        return nil
+    end
+
     for i, v in ipairs(array) do
         if v == val then
             return i
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index d8f4d538c..b20ff2787 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -272,7 +272,7 @@ local function load_stream(plugin_names)
 end
 
 
-function _M.load(config)
+local function get_plugin_names(config)
     local http_plugin_names
     local stream_plugin_names
 
@@ -294,7 +294,7 @@ function _M.load(config)
         local plugins_conf = config.value
         -- plugins_conf can be nil when another instance writes into etcd key 
"/apisix/plugins/"
         if not plugins_conf then
-            return local_plugins
+            return true
         end
 
         for _, conf in ipairs(plugins_conf) do
@@ -306,6 +306,16 @@ function _M.load(config)
         end
     end
 
+    return false, http_plugin_names, stream_plugin_names
+end
+
+
+function _M.load(config)
+    local ignored, http_plugin_names, stream_plugin_names = 
get_plugin_names(config)
+    if ignored then
+        return local_plugins
+    end
+
     if ngx.config.subsystem == "http" then
         if not http_plugin_names then
             core.log.error("failed to read plugin list from local file")
@@ -620,16 +630,21 @@ end
 
 
 function _M.init_worker()
-    _M.load()
+    local _, http_plugin_names, stream_plugin_names = get_plugin_names()
 
     -- some plugins need to be initialized in init* phases
-    if is_http and local_plugins_hash["prometheus"] then
-        local prometheus_enabled_in_stream = 
stream_local_plugins_hash["prometheus"]
+    if is_http and core.table.array_find(http_plugin_names, "prometheus") then
+        local prometheus_enabled_in_stream =
+            core.table.array_find(stream_plugin_names, "prometheus")
         
require("apisix.plugins.prometheus.exporter").http_init(prometheus_enabled_in_stream)
-    elseif not is_http and stream_local_plugins_hash["prometheus"] then
+    elseif not is_http and core.table.array_find(stream_plugin_names, 
"prometheus") then
         require("apisix.plugins.prometheus.exporter").stream_init()
     end
 
+    -- someone's plugin needs to be initialized after prometheus
+    -- see https://github.com/apache/apisix/issues/3286
+    _M.load()
+
     if local_conf and not local_conf.apisix.enable_admin then
         init_plugins_syncer()
     end

Reply via email to