Skip to content

Commit 30ae5df

Browse files
authored
feat: decoupled prometheus exporter's calculation and output (#12383)
1 parent 12b0824 commit 30ae5df

17 files changed

+289
-212
lines changed

apisix/cli/config.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ local _M = {
9595
meta = {
9696
lua_shared_dict = {
9797
["prometheus-metrics"] = "15m",
98+
["prometheus-cache"] = "10m",
9899
["standalone-config"] = "10m",
99100
["status-report"] = "1m",
100101
}
@@ -323,7 +324,8 @@ local _M = {
323324
export_addr = {
324325
ip = "127.0.0.1",
325326
port = 9091
326-
}
327+
},
328+
refresh_interval = 15
327329
},
328330
["server-info"] = {
329331
report_ttl = 60

apisix/cli/ngx_tpl.lua

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ lua {
6767
{% if enabled_stream_plugins["prometheus"] then %}
6868
lua_shared_dict prometheus-metrics {* meta.lua_shared_dict["prometheus-metrics"] *};
6969
{% end %}
70+
{% if enabled_plugins["prometheus"] or enabled_stream_plugins["prometheus"] then %}
71+
lua_shared_dict prometheus-cache {* meta.lua_shared_dict["prometheus-cache"] *};
72+
{% end %}
7073
{% if standalone_with_admin_api then %}
7174
lua_shared_dict standalone-config {* meta.lua_shared_dict["standalone-config"] *};
7275
{% end %}
@@ -96,22 +99,20 @@ http {
9699
}
97100
98101
init_worker_by_lua_block {
99-
require("apisix.plugins.prometheus.exporter").http_init(true)
102+
local prometheus = require("apisix.plugins.prometheus.exporter")
103+
prometheus.http_init(true)
104+
prometheus.init_exporter_timer()
100105
}
101106
102107
server {
103-
{% if use_apisix_base then %}
104-
listen {* prometheus_server_addr *} enable_process=privileged_agent;
105-
{% else %}
106-
listen {* prometheus_server_addr *};
107-
{% end %}
108+
listen {* prometheus_server_addr *};
108109
109110
access_log off;
110111
111112
location / {
112113
content_by_lua_block {
113114
local prometheus = require("apisix.plugins.prometheus.exporter")
114-
prometheus.export_metrics(true)
115+
prometheus.export_metrics()
115116
}
116117
}
117118
@@ -577,11 +578,7 @@ http {
577578
578579
{% if enabled_plugins["prometheus"] and prometheus_server_addr then %}
579580
server {
580-
{% if use_apisix_base then %}
581-
listen {* prometheus_server_addr *} enable_process=privileged_agent;
582-
{% else %}
583-
listen {* prometheus_server_addr *};
584-
{% end %}
581+
listen {* prometheus_server_addr *};
585582
586583
access_log off;
587584

apisix/core/config_etcd.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ function _M.new(key, opts)
10011001
sync_times = 0,
10021002
running = true,
10031003
conf_version = 0,
1004-
values = nil,
1004+
values = {},
10051005
need_reload = true,
10061006
watching_stream = nil,
10071007
routes_hash = nil,

apisix/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ function _M.http_init_worker()
155155
if local_conf.apisix and local_conf.apisix.enable_server_tokens == false then
156156
ver_header = "APISIX"
157157
end
158+
159+
-- To ensure that all workers related to Prometheus metrics are initialized,
160+
-- we need to put the initialization of the Prometheus plugin here.
161+
plugin.init_prometheus()
158162
end
159163

160164

apisix/plugin.lua

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ function _M.load(config)
341341
return local_plugins
342342
end
343343

344-
local exporter = require("apisix.plugins.prometheus.exporter")
345-
346344
if ngx.config.subsystem == "http" then
347345
if not http_plugin_names then
348346
core.log.error("failed to read plugin list from local file")
@@ -356,15 +354,6 @@ function _M.load(config)
356354
if not ok then
357355
core.log.error("failed to load plugins: ", err)
358356
end
359-
360-
local enabled = core.table.array_find(http_plugin_names, "prometheus") ~= nil
361-
local active = exporter.get_prometheus() ~= nil
362-
if not enabled then
363-
exporter.destroy()
364-
end
365-
if enabled and not active then
366-
exporter.http_init()
367-
end
368357
end
369358
end
370359

@@ -808,18 +797,21 @@ do
808797
end
809798

810799

811-
function _M.init_worker()
800+
function _M.init_prometheus()
812801
local _, http_plugin_names, stream_plugin_names = get_plugin_names()
802+
local enabled_in_http = core.table.array_find(http_plugin_names, "prometheus")
803+
local enabled_in_stream = core.table.array_find(stream_plugin_names, "prometheus")
813804

814-
-- some plugins need to be initialized in init* phases
815-
if is_http and core.table.array_find(http_plugin_names, "prometheus") then
816-
local prometheus_enabled_in_stream =
817-
core.table.array_find(stream_plugin_names, "prometheus")
818-
require("apisix.plugins.prometheus.exporter").http_init(prometheus_enabled_in_stream)
819-
elseif not is_http and core.table.array_find(stream_plugin_names, "prometheus") then
820-
require("apisix.plugins.prometheus.exporter").stream_init()
805+
-- For stream-only mode, there are separate calls in ngx_tpl.lua.
806+
-- And for other modes, whether in stream or http plugins,
807+
-- the prometheus exporter needs to be initialized.
808+
if is_http and (enabled_in_http or enabled_in_stream) then
809+
require("apisix.plugins.prometheus.exporter").init_exporter_timer()
821810
end
811+
end
822812

813+
814+
function _M.init_worker()
823815
-- someone's plugin needs to be initialized after prometheus
824816
-- see https://github.com/apache/apisix/issues/3286
825817
_M.load()

apisix/plugins/prometheus.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
local core = require("apisix.core")
1818
local exporter = require("apisix.plugins.prometheus.exporter")
1919

20-
2120
local plugin_name = "prometheus"
2221
local schema = {
2322
type = "object",
@@ -35,6 +34,7 @@ local _M = {
3534
priority = 500,
3635
name = plugin_name,
3736
log = exporter.http_log,
37+
destroy = exporter.destroy,
3838
schema = schema,
3939
run_policy = "prefer_route",
4040
}
@@ -55,4 +55,11 @@ function _M.api()
5555
end
5656

5757

58+
function _M.init()
59+
local local_conf = core.config.local_conf()
60+
local enabled_in_stream = core.table.array_find(local_conf.stream_plugins, "prometheus")
61+
exporter.http_init(enabled_in_stream)
62+
end
63+
64+
5865
return _M

0 commit comments

Comments
 (0)