This is an automated email from the ASF dual-hosted git repository. wenming 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 f267291 feat(prometheus): allow customizing Prometheus export uri (#2826) f267291 is described below commit f267291f3a0c102c879b5c12d02e140ede0f5a4b Author: 罗泽轩 <spacewander...@gmail.com> AuthorDate: Mon Nov 30 14:35:06 2020 +0800 feat(prometheus): allow customizing Prometheus export uri (#2826) * feat(prometheus): allow customizing Prometheus export uri Close #2296. --- apisix/api_router.lua | 25 ++++++++++++++++++++ apisix/init.lua | 4 +++- apisix/plugins/prometheus.lua | 13 +++++++++-- conf/config-default.yaml | 2 ++ doc/plugins/prometheus.md | 15 ++++++++++++ doc/zh-cn/plugins/prometheus.md | 15 ++++++++++++ t/admin/plugins-reload.t | 52 +++++++++++++++++++++++++++++++++++++++++ t/plugin/prometheus.t | 52 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 175 insertions(+), 3 deletions(-) diff --git a/apisix/api_router.lua b/apisix/api_router.lua index 264c478..3f1cebc 100644 --- a/apisix/api_router.lua +++ b/apisix/api_router.lua @@ -20,10 +20,12 @@ local plugin_mod = require("apisix.plugin") local ip_restriction = require("apisix.plugins.ip-restriction") local core = require("apisix.core") local ipairs = ipairs +local type = type local _M = {} local match_opts = {} +local has_route_not_under_apisix local interceptors = { ["ip-restriction"] = { run = function (conf, ctx) @@ -76,6 +78,8 @@ do function fetch_api_router() core.table.clear(routes) + has_route_not_under_apisix = false + for _, plugin in ipairs(plugin_mod.plugins) do local api_fun = plugin.api if api_fun then @@ -84,6 +88,18 @@ function fetch_api_router() core.log.debug("fetched api routes: ", core.json.delay_encode(api_routes, true)) for _, route in ipairs(api_routes) do + local typ_uri = type(route.uri) + if typ_uri == "string" then + has_route_not_under_apisix = + not core.string.has_prefix(route.uri, "/apisix/") + else + for _, uri in ipairs(route.uri) do + if not core.string.has_prefix(route.uri, "/apisix/") then + has_route_not_under_apisix = true + end + end + end + core.table.insert(routes, { methods = route.methods, paths = route.uri, @@ -121,6 +137,15 @@ end end -- do +function _M.has_route_not_under_apisix() + if has_route_not_under_apisix == nil then + return true + end + + return has_route_not_under_apisix +end + + function _M.match(api_ctx) local api_router = core.lrucache.global("api_router", plugin_mod.load_times, fetch_api_router) if not api_router then diff --git a/apisix/init.lua b/apisix/init.lua index 7333dd1..827a1fb 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -373,7 +373,9 @@ function _M.http_access_phase() end end - if core.string.has_prefix(uri, "/apisix/") then + if router.api.has_route_not_under_apisix() or + core.string.has_prefix(uri, "/apisix/") + then local matched = router.api.match(api_ctx) if matched then return diff --git a/apisix/plugins/prometheus.lua b/apisix/plugins/prometheus.lua index 8a0a340..54bdaad 100644 --- a/apisix/plugins/prometheus.lua +++ b/apisix/plugins/prometheus.lua @@ -16,9 +16,10 @@ -- local core = require("apisix.core") local exporter = require("apisix.plugins.prometheus.exporter") -local plugin_name = "prometheus" +local plugin_name = "prometheus" +local default_export_uri = "/apisix/prometheus/metrics" local schema = { type = "object", additionalProperties = false, @@ -45,10 +46,18 @@ end function _M.api() + local export_uri = default_export_uri + local local_conf = core.config.local_conf() + local attr = core.table.try_read_attr(local_conf, "plugin_attr", + plugin_name) + if attr and attr.export_uri then + export_uri = attr.export_uri + end + return { { methods = {"GET"}, - uri = "/apisix/prometheus/metrics", + uri = export_uri, handler = exporter.collect } } diff --git a/conf/config-default.yaml b/conf/config-default.yaml index ea4053a..d166e1f 100644 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -249,3 +249,5 @@ plugin_attr: service_name: APISIX service_instance_name: "APISIX Instance Name" endpoint_addr: http://127.0.0.1:12800 + prometheus: + export_uri: /apisix/prometheus/metrics diff --git a/doc/plugins/prometheus.md b/doc/plugins/prometheus.md index 1c1a7ab..68e900c 100644 --- a/doc/plugins/prometheus.md +++ b/doc/plugins/prometheus.md @@ -89,6 +89,21 @@ And we can check the status at prometheus console: ![](../../doc/images/plugin/prometheus02.png) +## How to specify export uri + +We can change the default export uri in the `plugin_attr` section of `conf/config.yaml`. + +| Name | Type | Default | Description | +| ------------ | ------ | -------- | -------------------------------------------------------------------- | +| export_uri | string | "/apisix/prometheus/metrics" | uri to get the prometheus metrics | + +Here is an example: + +```yaml +plugin_attr: + prometheus: + export_uri: /apisix/metrics +``` ### Grafana dashboard diff --git a/doc/zh-cn/plugins/prometheus.md b/doc/zh-cn/plugins/prometheus.md index 826e45a..a1c7c90 100644 --- a/doc/zh-cn/plugins/prometheus.md +++ b/doc/zh-cn/plugins/prometheus.md @@ -87,6 +87,21 @@ scrape_configs: ![](../../images/plugin/prometheus02.png) +## 如何修改暴露指标的uri + +我们可以在 `conf/config.yaml` 的 `plugin_attr` 修改默认的uri + +| 名称 | 类型 | 默认值 | 描述 | +| ------------ | ------ | -------- | -------------------------------------------------------------------- | +| export_uri | string | "/apisix/prometheus/metrics" | 暴露指标的uri | + +配置示例: + +```yaml +plugin_attr: + prometheus: + export_uri: /apisix/metrics +``` ### Grafana 面板 diff --git a/t/admin/plugins-reload.t b/t/admin/plugins-reload.t index 667c9ad..c84c955 100644 --- a/t/admin/plugins-reload.t +++ b/t/admin/plugins-reload.t @@ -196,3 +196,55 @@ example-plugin get plugin attr val: 1 --- error_log plugin_attr of example-plugin changed plugins not changed + + + +=== TEST 4: reload plugins to change prometheus' export uri +--- yaml_config +apisix: + node_listen: 1984 + admin_key: null +plugins: + - prometheus +plugin_attr: + prometheus: + export_uri: /metrics +--- config +location /t { + content_by_lua_block { + local core = require "apisix.core" + ngx.sleep(0.1) + local t = require("lib.test_admin").test + local code, _, org_body = t('/apisix/metrics', + ngx.HTTP_GET) + ngx.say(code) + + local data = [[ +apisix: + node_listen: 1984 + admin_key: null +plugins: + - prometheus +plugin_attr: + prometheus: + export_uri: /apisix/metrics + ]] + require("lib.test_admin").set_config_yaml(data) + + local code, _, org_body = t('/apisix/admin/plugins/reload', + ngx.HTTP_PUT) + + ngx.say(org_body) + + ngx.sleep(0.1) + local code, _, org_body = t('/apisix/metrics', + ngx.HTTP_GET) + ngx.say(code) + } +} +--- request +GET /t +--- response_body +404 +done +200 diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t index e9f0e0f..7122b6e 100644 --- a/t/plugin/prometheus.t +++ b/t/plugin/prometheus.t @@ -1036,3 +1036,55 @@ GET /apisix/prometheus/metrics qr/apisix_http_status\{code="404",route="9",matched_uri="\/bar\*",matched_host="bar.com",service="",consumer="",node="127.0.0.1"\} \d+/ --- no_error_log [error] + + + +=== TEST 58: customize export uri, not found +--- yaml_config +plugin_attr: + prometheus: + export_uri: /a +--- request +GET /apisix/prometheus/metrics +--- error_code: 404 +--- no_error_log +[error] + + + +=== TEST 59: customize export uri, found +--- yaml_config +plugin_attr: + prometheus: + export_uri: /a +--- request +GET /a +--- error_code: 200 +--- no_error_log +[error] + + + +=== TEST 60: customize export uri, missing plugin, use default +--- yaml_config +plugin_attr: + x: + y: z +--- request +GET /apisix/prometheus/metrics +--- error_code: 200 +--- no_error_log +[error] + + + +=== TEST 61: customize export uri, missing attr, use default +--- yaml_config +plugin_attr: + prometheus: + y: z +--- request +GET /apisix/prometheus/metrics +--- error_code: 200 +--- no_error_log +[error]