Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add control api for plugin server-info #3088

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apisix/plugins/server-info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ local function get()
end


local function get_server_info()
local info, err = get()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like we need to way to store the boot_time in shdict when etcd is not used. Otherwise the boot_time will change when plugin reloads.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really, i will change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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
Expand Down Expand Up @@ -158,6 +169,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()))

Expand Down
6 changes: 6 additions & 0 deletions doc/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ Here is the supported API:
Introduced since `v2.2`.

Return the jsonschema used by this APISIX instance.

### GET /v1/server_info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to put this to plugin's own section. So that the API in this page is builtin and will be provided whether you use the plugin or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


Introduced since `v2.2`.

Return the server info generated by plugin [server-info](plugins/server-info.md).
19 changes: 17 additions & 2 deletions doc/plugins/server-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ None

## API

None
This plugin exposes one API `/v1/server_info` to [Control API](../control-api.md).

## How to Enable

Expand Down Expand Up @@ -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

Expand Down
19 changes: 17 additions & 2 deletions doc/zh-cn/plugins/server-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

## 插件接口

该插件在 [Control API](../../control-api.md) 下暴露了一个 API 接口 `/v1/server_info`。

## 启用插件

Expand Down Expand Up @@ -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 来查看这些数据。

## 禁用插件

Expand Down
51 changes: 51 additions & 0 deletions t/plugin/server-info.t
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,54 @@ 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
enable_control: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This configuration may confuses beginner, because the control server in the test is hardcoded in APISIX.pm, instead of generating from config.yaml.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

control:
port: 1984
plugins:
- server-info
--- config
location /t {
content_by_lua_block {
local json_decode = require("apisix.core").json.decode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/api7/test-toolkit

we can use this json to print the sorted data. we can do this in a new PR

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]