You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/12/22 03:05:07 UTC

[apisix] branch master updated: feat: add control api for plugin server-info (#3088)

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

membphis 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 c3d61fc  feat: add control api for plugin server-info (#3088)
c3d61fc is described below

commit c3d61fcfa371e4ca23e73af832141f84e53d6435
Author: Alex Zhang <zc...@gmail.com>
AuthorDate: Tue Dec 22 11:04:56 2020 +0800

    feat: add control api for plugin server-info (#3088)
    
    * feat: add control api for plugin server-info
    
    Previously we added plugin server-info without exposing API since the same port problem of admin api and proxy. Now we have Control API support so we expose server-info API through Control API. People now can insight the server info about APISIX instance easily.
---
 apisix/plugins/server-info.lua   | 45 ++++++++++++++++++++++++++++++++++++-
 doc/plugins/server-info.md       | 19 ++++++++++++++--
 doc/zh-cn/plugins/server-info.md | 19 ++++++++++++++--
 t/plugin/server-info.t           | 48 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 126 insertions(+), 5 deletions(-)

diff --git a/apisix/plugins/server-info.lua b/apisix/plugins/server-info.lua
index 4c90682..5038eae 100644
--- a/apisix/plugins/server-info.lua
+++ b/apisix/plugins/server-info.lua
@@ -23,7 +23,7 @@ local ngx_timer_at = ngx.timer.at
 local ngx_worker_id = ngx.worker.id
 local type = type
 
-local boot_time = os.time()
+local load_time = os.time()
 local plugin_name = "server-info"
 local default_report_interval = 60
 local default_report_ttl = 7200
@@ -66,7 +66,28 @@ local _M = {
 }
 
 
+local function get_boot_time()
+    local time, err = internal_status:get("server_info:boot_time")
+    if err ~= nil then
+        core.log.error("failed to get boot_time from shdict: ", err)
+        return load_time
+    end
+
+    if time ~= nil then
+        return time
+    end
+
+    local _, err = internal_status:set("server_info:boot_time", load_time)
+    if err ~= nil then
+        core.log.error("failed to save boot_time to shdict: ", err)
+    end
+
+    return load_time
+end
+
+
 local function uninitialized_server_info()
+    local boot_time = get_boot_time()
     return {
         etcd_version     = "unknown",
         hostname         = core.utils.gethostname(),
@@ -99,6 +120,17 @@ local function get()
 end
 
 
+local function get_server_info()
+    local info, err = get()
+    if not info then
+        core.log.error("failed to get server_info: ", err)
+        return 500
+    end
+
+    return 200, info
+end
+
+
 local function report(premature, report_ttl)
     if premature then
         return
@@ -158,6 +190,17 @@ function _M.check_schema(conf)
 end
 
 
+function _M.control_api()
+    return {
+        {
+            methods = {"GET"},
+            uris ={"/v1/server_info"},
+            handler = get_server_info,
+        }
+    }
+end
+
+
 function _M.init()
     core.log.info("server info: ", core.json.delay_encode(get()))
 
diff --git a/doc/plugins/server-info.md b/doc/plugins/server-info.md
index bcd2a09..cee2cae 100644
--- a/doc/plugins/server-info.md
+++ b/doc/plugins/server-info.md
@@ -50,7 +50,7 @@ None
 
 ## API
 
-None
+This plugin exposes one API `/v1/server_info` to [Control API](../control-api.md).
 
 ## How to Enable
 
@@ -87,7 +87,22 @@ plugin_attr:
 
 ## Test Plugin
 
-The APISIX Dashboard will collects server info in etcd, after enabling this plugin, you may try to check them through Dashboard.
+After enabling this plugin, you can access these data through the plugin Control API:
+
+```shell
+$ curl http://127.0.0.1:9090/v1/server_info -s | jq .
+{
+  "etcd_version": "3.5.0",
+  "up_time": 9460,
+  "last_report_time": 1608531519,
+  "id": "b7ce1c5c-b1aa-4df7-888a-cbe403f3e948",
+  "hostname": "fedora32",
+  "version": "2.1",
+  "boot_time": 1608522102
+}
+```
+
+The APISIX Dashboard will collects server info in etcd, so you may also try to check them through Dashboard.
 
 ## Disable Plugin
 
diff --git a/doc/zh-cn/plugins/server-info.md b/doc/zh-cn/plugins/server-info.md
index 375ac77..59bede4 100644
--- a/doc/zh-cn/plugins/server-info.md
+++ b/doc/zh-cn/plugins/server-info.md
@@ -51,7 +51,7 @@
 
 ## 插件接口
 
-无
+该插件在 [Control API](../../control-api.md) 下暴露了一个 API 接口 `/v1/server_info`。
 
 ## 启用插件
 
@@ -89,7 +89,22 @@ plugin_attr:
 
 ## 测试插件
 
-Apache APISIX Dashboard 会收集上报到 etcd 中的服务信息,在启用这个插件后,你可以通过 APISIX Dashboard 来查看这些数据。
+在启用该插件后,你可以通过插件的 Control API 来访问到这些数据:
+
+```shell
+$ curl http://127.0.0.1:9090/v1/server_info -s | jq .
+{
+  "etcd_version": "3.5.0",
+  "up_time": 9460,
+  "last_report_time": 1608531519,
+  "id": "b7ce1c5c-b1aa-4df7-888a-cbe403f3e948",
+  "hostname": "fedora32",
+  "version": "2.1",
+  "boot_time": 1608522102
+}
+```
+
+Apache APISIX Dashboard 会收集上报到 etcd 中的服务信息,因此你也可以通过 APISIX Dashboard 来查看这些数据。
 
 ## 禁用插件
 
diff --git a/t/plugin/server-info.t b/t/plugin/server-info.t
index ae8306c..7ee7eb0 100644
--- a/t/plugin/server-info.t
+++ b/t/plugin/server-info.t
@@ -133,3 +133,51 @@ integral
 [error]
 --- error_log
 timer created to report server info, interval: 60
+
+
+
+=== TEST 3: get server_info from plugin control API
+--- yaml_config
+apisix:
+    id: 123456
+plugins:
+    - server-info
+--- config
+location /t {
+    content_by_lua_block {
+        local json_decode = require("apisix.core").json.decode
+        local t = require("lib.test_admin").test
+        local code, _, body = t("/v1/server_info")
+        if code >= 300 then
+            ngx.status = code
+        end
+
+        local keys = {}
+        local value, err = json_decode(body)
+        if not value then
+            ngx.say(err)
+            return
+        end
+        for k in pairs(value) do
+            keys[#keys + 1] = k
+        end
+
+        table.sort(keys)
+        for i = 1, #keys do
+            ngx.say(keys[i], ": ", value[keys[i]])
+        end
+    }
+}
+--- request
+GET /t
+--- response_body eval
+qr{^boot_time: \d+
+etcd_version: [\d\.]+
+hostname: [a-zA-Z\-0-9\.]+
+id: [a-zA-Z\-0-9]+
+last_report_time: \d+
+up_time: \d+
+version: [\d\.]+
+$}
+--- no_error_log
+[error]