You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/11/28 09:13:25 UTC
[GitHub] [apisix] tokers opened a new pull request #2884: feat: server info
tokers opened a new pull request #2884:
URL: https://github.com/apache/apisix/pull/2884
### What this PR does / why we need it:
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
Close #2821.
### Pre-submission checklist:
* [x] Did you explain what problem does this PR solve? Or what new features have been added?
* [x] Have you added corresponding test cases?
* [x] Have you modified the corresponding document?
* [x] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533108660
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
+ local ok, err = core.etcd.set(key, data, 180)
+ if not ok then
+ core.log.error("failed to report server info to etcd: ", err)
+ return nil, err
+ end
+
+ local ok, err = save(data, false)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ return nil, err
+ end
+end
+
+
+local function uninitialized_server_info()
+ return {
+ etcd_version = "unknown",
+ hostname = core.utils.gethostname(),
+ id = core.id.get(),
+ version = core.version.VERSION,
+ up_time = ngx_time() - boot_time,
+ last_report_time = -1,
+ }
+end
+
+
+function _M.init_worker()
+ if not is_privileged() then
+ return
+ end
+
+ local ok, err = encode_and_save(uninitialized_server_info(), true)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ end
+
+ local opts = {
+ check_interval = 5, -- in seconds
Review comment:
I still think 10 mins is too long, also for now, all the basic server info are static (except the `up_time` and `last_report_time`), a relative short period is more suitable so we can easily judge whether a node is healthy or unstable when we are viewing the server info on Dashboard.
What about 1 min or 2 mins?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533054626
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
Fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533107665
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
What about the control API's milestone?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r532510573
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
And we should implement this feature by plugin way.
Take a look at: https://github.com/apache/apisix/blob/master/apisix/plugins/node-status.lua
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-737034827
This PR was pushed directly at apache/apisix, i will close it and please see the new one https://github.com/apache/apisix/pull/2926.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] agile6v commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
agile6v commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-737032746
> > @agile6v We can add some limit to the report interval, such as the minimal report interval is 10s.
>
> agree. I think the minimal report interval can be `1 min`
If only `time` changes frequently. I'm not sure if the lease mechanism of etcd can get the creation time to achieve this.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533103834
##########
File path: apisix/admin/init.lua
##########
@@ -29,6 +30,7 @@ local reload_event = "/apisix/admin/plugins/reload"
local ipairs = ipairs
local error = error
local events
+
Review comment:
ditto
not related to this title of PR
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r532511977
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
OK, will change it.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] agile6v commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
agile6v commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-736973265
Hi @tokers @membphis Remember to discuss on #962 If the data that change frequently is reported periodically to etcd. Not sure if etcd is suitable. What do you think on this point?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533111811
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
Review comment:
@membphis @tokers
I think `/data_plane/` is better.
It would be better to limit the write of DP to certain prefix, so that we can grant the privilege strictly.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533113410
##########
File path: doc/plugins/server-info.md
##########
@@ -48,4 +85,20 @@ The meaning of each item in server information is following:
| version | string | APISIX version. |
| hostname | string | Hostname of the machine/pod that APISIX is deployed. |
-Note the server information is also reported to etcd periodically (for now the interval is `5s`) and collected by APISIX Dashboard, so you can also access it from APISIX Dashboard.
+## Disable Plugin
+
+By removing `server-info` in the plugin list of configure file `apisix/conf/config.yaml` and restart APISIX, you can diable `server-info` plugin easily.
+
+```
Review comment:
format language?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers closed pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers closed pull request #2884:
URL: https://github.com/apache/apisix/pull/2884
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r532569310
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
I think the node-status plugin is suitable to also contains the server info.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-735662254
> CI failed, you need to fix it first
Fixed, except the markdown link errors.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533103648
##########
File path: apisix/admin/init.lua
##########
@@ -18,6 +18,7 @@ local require = require
local core = require("apisix.core")
local route = require("resty.radixtree")
local plugin = require("apisix.plugin")
+
Review comment:
revert this line
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533100323
##########
File path: doc/zh-cn/plugins/server-info.md
##########
@@ -0,0 +1,104 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+- [English](../../plugins/server-info.md)
+
+# Summary
+
+- [**插件简介**](#插件简介)
+- [插件属性](#插件属性)
+- [**插件接口**](#插件接口)
+- [启用插件](#启用插件)
+- [测试插件](#测试插件)
+- [禁用插件](#禁用插件)
+- [注意事项](#注意事项)
+
+## 插件简介
+
+`server-info` 是 `APISIX` 提供的一款查询其服务基本信息的插件。
+
+## 插件属性
+
+无
+
+## 插件接口
+
+此插件提供了接口 `/apisix/server_info`,可以通过 [interceptors](../plugin-interceptors.md) 来保护该接口。
+
+## 启用插件
+
+在配置文件 `apisix/conf/config.yaml` 的插件列表中添加 `server-info`, 即可启用该插件。
+
+```
+plugins: # plugin list
+ - example-plugin
+ - limit-req
+ - node-status
+ - server-info
+ - jwt-auth
+ - zipkin
+ ......
+```
+
+在启动/重启 APISIX 之后,即可通过访问 `/apisix/server_info` 来获取服务基本信息。
+
+## 测试插件
+
+```bash
+curl http://127.0.0.1:9080/apisix/admin/server_info -s | jq
Review comment:
need to update the path
##########
File path: t/plugin/server-info.t
##########
@@ -0,0 +1,69 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+master_on();
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity check
+--- config
+location /t {
+ content_by_lua_block {
+ -- let the server info to be reported
+ ngx.sleep(5.1)
+ local json_decode = require("cjson.safe").decode
+ local t = require("lib.test_admin").test
+ local code, _, body = t('/apisix/server_info', ngx.HTTP_GET)
+ if code >= 300 then
+ ngx.status = code
+ end
+
+ local keys = {}
+ body = json_decode(body)
+ for k in pairs(body) do
+ keys[#keys + 1] = k
+ end
+
+ table.sort(keys)
+ for i = 1, #keys do
+ ngx.say(keys[i], ": ", body[keys[i]])
+ end
+ }
+}
+--- request
+GET /t
+--- response_body eval
+qr{^etcd_version: [\d\.]+
+hostname: [a-zA-Z\-0-9]+
+id: [a-zA-Z\-0-9]+
+last_report_time: \d+
+up_time: \d+
+version: [\d\.]+
+$}
+--- timeout: 6
+--- no_error_log
+[error]
+
Review comment:
one blank line is enough at the end of file
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
+ local ok, err = core.etcd.set(key, data, 180)
+ if not ok then
+ core.log.error("failed to report server info to etcd: ", err)
+ return nil, err
+ end
+
+ local ok, err = save(data, false)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ return nil, err
+ end
+end
+
+
+local function uninitialized_server_info()
+ return {
+ etcd_version = "unknown",
+ hostname = core.utils.gethostname(),
+ id = core.id.get(),
+ version = core.version.VERSION,
+ up_time = ngx_time() - boot_time,
+ last_report_time = -1,
+ }
+end
+
+
+function _M.init_worker()
+ if not is_privileged() then
+ return
+ end
+
+ local ok, err = encode_and_save(uninitialized_server_info(), true)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ end
+
+ local opts = {
+ check_interval = 5, -- in seconds
Review comment:
and we should allow the user to specify this field
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
+ local ok, err = core.etcd.set(key, data, 180)
+ if not ok then
+ core.log.error("failed to report server info to etcd: ", err)
+ return nil, err
+ end
+
+ local ok, err = save(data, false)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ return nil, err
+ end
+end
+
+
+local function uninitialized_server_info()
+ return {
+ etcd_version = "unknown",
+ hostname = core.utils.gethostname(),
+ id = core.id.get(),
+ version = core.version.VERSION,
+ up_time = ngx_time() - boot_time,
+ last_report_time = -1,
+ }
+end
+
+
+function _M.init_worker()
+ if not is_privileged() then
+ return
+ end
+
+ local ok, err = encode_and_save(uninitialized_server_info(), true)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ end
+
+ local opts = {
+ check_interval = 5, -- in seconds
Review comment:
`5` seconds is short, I think 10 mins should be good
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
Review comment:
we can not use `data_plane` here. CP will write the server info too.
how about `/nodes/server_info/{node_id}`?
##########
File path: doc/zh-cn/plugins/server-info.md
##########
@@ -0,0 +1,104 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+- [English](../../plugins/server-info.md)
+
+# Summary
+
+- [**插件简介**](#插件简介)
+- [插件属性](#插件属性)
+- [**插件接口**](#插件接口)
+- [启用插件](#启用插件)
+- [测试插件](#测试插件)
+- [禁用插件](#禁用插件)
+- [注意事项](#注意事项)
+
+## 插件简介
+
+`server-info` 是 `APISIX` 提供的一款查询其服务基本信息的插件。
+
+## 插件属性
+
+无
+
+## 插件接口
+
+此插件提供了接口 `/apisix/server_info`,可以通过 [interceptors](../plugin-interceptors.md) 来保护该接口。
+
+## 启用插件
+
+在配置文件 `apisix/conf/config.yaml` 的插件列表中添加 `server-info`, 即可启用该插件。
+
+```
+plugins: # plugin list
+ - example-plugin
+ - limit-req
+ - node-status
+ - server-info
+ - jwt-auth
+ - zipkin
+ ......
+```
+
+在启动/重启 APISIX 之后,即可通过访问 `/apisix/server_info` 来获取服务基本信息。
+
+## 测试插件
+
+```bash
+curl http://127.0.0.1:9080/apisix/admin/server_info -s | jq
+{
+ "up_time": 5,
+ "last_report_time": 1606551536,
+ "id": "71cb4999-4349-475d-aa39-c703944a63d3",
+ "etcd_version": "3.5.0",
+ "version": "2.0",
+ "hostname": "gentoo"
+}
+```
+
+服务信息中每一项的含义如下:
+
+| 名称 | 类型 | 描述 |
+|---------|------|-------------|
+| up_time | integer | APISIX 服务实例当前的运行时间, 如果对 APSIX
+进行热更新操作,该值将被重置;普通的 reload 操作不会影响该值。 |
+| last_report_time | integer | 最近一次服务信息上报的时间 (UNIX 时间戳)。|
+| id | string | APISIX 服务实例 id 。|
+| etcd_version | string | etcd 集群的版本信息,如果 APISIX 和 etcd 集群之间存在网络分区,该值将设置为 `"unknown"`。|
+| version | string | APISIX 版本信息。 |
+| hostname | string | APISIX 所部署的机器或 pod 的主机名信息。|
+
+## 禁用插件
+
+通过移除配置文件 `apisix/conf/config.yaml` 插件列表中的 `server-info`,即可方便地禁用该插件。
+
+```
+plugins: # plugin list
+ - example-plugin
+ - limit-req
+ - node-status
+ - jwt-auth
+ - zipkin
+ ......
+```
+
+## 注意事项
+
+当使用 etcd 作为 APISIX 的数据中心的说话,服务信息将被周期性地上报到 etcd(目前的上报间隔是 5
Review comment:
"当使用 etcd 作为 APISIX 的数据中心的说话"
I can not understand it
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
+ local ok, err = core.etcd.set(key, data, 180)
+ if not ok then
+ core.log.error("failed to report server info to etcd: ", err)
+ return nil, err
+ end
+
+ local ok, err = save(data, false)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ return nil, err
+ end
+end
+
+
+local function uninitialized_server_info()
+ return {
+ etcd_version = "unknown",
+ hostname = core.utils.gethostname(),
+ id = core.id.get(),
+ version = core.version.VERSION,
+ up_time = ngx_time() - boot_time,
+ last_report_time = -1,
+ }
+end
+
+
+function _M.init_worker()
+ if not is_privileged() then
+ return
+ end
+
+ local ok, err = encode_and_save(uninitialized_server_info(), true)
+ if not ok then
+ core.log.error("failed to encode and save server info: ", err)
+ end
+
+ local opts = {
+ check_interval = 5, -- in seconds
+ }
+
+ if core.config ~= require("apisix.core.config_etcd") then
Review comment:
need some comment for why call `return` here
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-737014459
@agile6v We can add some limit to the report interval, such as the minimal report interval is 10s.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-735244636
CI failed, you need to fix it first
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533113906
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
There is not milestone. So far, we can hide the entry first.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533106738
##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+ error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+ local process_type = require("ngx.process").type()
+ return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+ local handler = excl and internal_status.add or internal_status.set
+
+ local ok, err = handler(internal_status, "server_info", data)
+ if not ok then
+ if excl and err == "exists" then
+ return true
+ end
+
+ return nil, err
+ end
+
+ return true
+end
+
+
+local function encode_and_save(server_info, excl)
+ local data, err = core.json.encode(server_info)
+ if not data then
+ return nil, err
+ end
+
+ return save(data, excl)
+end
+
+
+local function report()
+ local server_info, err = _M.get()
+ if not server_info then
+ core.log.error("failed to get server_info: ", err)
+ return nil, err
+ end
+
+ if server_info.etcd_version == "unknown" then
+ local res, err = core.etcd.server_version()
+ if not res then
+ core.log.error("failed to fetch etcd version: ", err)
+ return nil, err
+
+ elseif type(res.body) ~= "table" then
+ core.log.error("failed to fetch etcd version: bad version info")
+ return nil, "bad etcd version info"
+ else
+ server_info.etcd_version = res.body.etcdcluster
+ end
+ end
+
+ server_info.last_report_time = ngx_time()
+
+ local data, err = core.json.encode(server_info)
+ if not data then
+ core.log.error("failed to encode server_info: ", err)
+ return nil, err
+ end
+
+ local key = "/data_plane/server_info/" .. server_info.id
Review comment:
The key /nodes/server_info/{node_id} is good, but why CP also writes the server info?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#issuecomment-737021744
> @agile6v We can add some limit to the report interval, such as the minimal report interval is 10s.
agree. I think the minimal report interval can be `1 min`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533059031
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
@membphis @tokers
The API should go to the control API: https://github.com/apache/apisix/issues/2798.
There is security risk if we put this feature into a plugin. Like the prometheus metrics, we don't have a default way to protect them.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] moonming commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
moonming commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r532468955
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
I think we need do this in DP, not admin API
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on a change in pull request #2884: feat: server info
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r532569310
##########
File path: apisix/admin/init.lua
##########
@@ -316,6 +330,11 @@ local uri_route = {
methods = {"PUT"},
handler = post_reload_plugins,
},
+ {
+ paths = [[/apisix/admin/server_info]],
Review comment:
I think the node-status plugin is suitable to also contain the server info.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org