Skip to content

Commit 33b437d

Browse files
author
Ayeshmantha Perera
authored
feature: implemented plugin sys logger. (#1414)
1 parent a446cd0 commit 33b437d

File tree

8 files changed

+585
-1
lines changed

8 files changed

+585
-1
lines changed

apisix/plugins/syslog.lua

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
local core = require("apisix.core")
18+
local log_util = require("apisix.utils.log-util")
19+
local logger_socket = require("resty.logger.socket")
20+
local plugin_name = "syslog"
21+
local ngx = ngx
22+
23+
local schema = {
24+
type = "object",
25+
properties = {
26+
host = {type = "string"},
27+
port = {type = "integer"},
28+
flush_limit = {type = "integer", minimum = 1, default = 4096},
29+
drop_limit = {type = "integer", default = 1048576},
30+
timeout = {type = "integer", minimum = 1, default = 3},
31+
sock_type = {type = "string", default = "tcp"},
32+
max_retry_times = {type = "integer", minimum = 1, default = 3},
33+
retry_interval = {type = "integer", minimum = 10, default = 100},
34+
pool_size = {type = "integer", minimum = 5, default = 5},
35+
tls = {type = "boolean", default = false},
36+
},
37+
required = {"host", "port"}
38+
}
39+
40+
local lrucache = core.lrucache.new({
41+
ttl = 300, count = 512
42+
})
43+
44+
local _M = {
45+
version = 0.1,
46+
priority = 401,
47+
name = plugin_name,
48+
schema = schema,
49+
}
50+
51+
function _M.check_schema(conf)
52+
return core.schema.check(schema, conf)
53+
end
54+
55+
function _M.flush_syslog(logger)
56+
local ok, err = logger:flush(logger)
57+
if not ok then
58+
core.log.error("failed to flush message:", err)
59+
end
60+
end
61+
62+
-- log phase in APISIX
63+
function _M.log(conf)
64+
local entry = log_util.get_full_log(ngx)
65+
66+
if not entry.route_id then
67+
core.log.error("failed to obtain the route id for sys logger")
68+
return
69+
end
70+
71+
-- fetch api_ctx
72+
local api_ctx = ngx.ctx.api_ctx
73+
if not api_ctx then
74+
core.log.error("invalid api_ctx cannot proceed with sys logger plugin")
75+
return core.response.exit(500)
76+
end
77+
78+
-- fetch it from lrucache
79+
local logger, err = lrucache(api_ctx.conf_type .. "#" .. api_ctx.conf_id, api_ctx.conf_version,
80+
logger_socket.new, logger_socket, {
81+
host = conf.host,
82+
port = conf.port,
83+
flush_limit = conf.flush_limit,
84+
drop_limit = conf.drop_limit,
85+
timeout = conf.timeout,
86+
sock_type = conf.sock_type,
87+
max_retry_times = conf.max_retry_times,
88+
retry_interval = conf.retry_interval,
89+
pool_size = conf.pool_size,
90+
tls = conf.tls,
91+
})
92+
93+
if not logger then
94+
core.log.error("failed when initiating the sys logger processor", err)
95+
end
96+
97+
-- reuse the logger object
98+
local ok, err = logger:log(core.json.encode(entry))
99+
if not ok then
100+
core.log.error("failed to log message", err)
101+
end
102+
end
103+
104+
return _M

conf/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ plugins: # plugin list
145145
- proxy-mirror
146146
- kafka-logger
147147
- cors
148+
- syslog
148149
- batch-requests
149150
stream_plugins:
150151
- mqtt-proxy

doc/plugins/syslog-cn.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!--
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
-->
19+
20+
# 摘要
21+
- [**定义**](#name)
22+
- [**属性列表**](#attributes)
23+
- [**如何开启**](#how-to-enable)
24+
- [**测试插件**](#test-plugin)
25+
- [**禁用插件**](#disable-plugin)
26+
27+
28+
## 定义
29+
30+
`sys` 是一个将Log data请求推送到Syslog的插件。
31+
32+
这将提供将Log数据请求作为JSON对象发送的功能。
33+
34+
## 属性列表
35+
36+
|属性名称 |必选项 |描述|
37+
|--------- |-------- |-----------|
38+
|host |必要的 |IP地址或主机名。|
39+
|port |必要的 |目标上游端口。|
40+
|timeout |可选的 |上游发送数据超时。|
41+
|tls |可选的 |布尔值,用于控制是否执行SSL验证。|
42+
|flush_limit |可选的 |如果缓冲的消息的大小加上当前消息的大小达到(> =)此限制(以字节为单位),则缓冲的日志消息将被写入日志服务器。默认为4096(4KB)。|
43+
|drop_limit |可选的 |如果缓冲的消息的大小加上当前消息的大小大于此限制(以字节为单位),则由于缓冲区大小有限,当前的日志消息将被丢弃。默认drop_limit为1048576(1MB)。|
44+
|sock_type|可选的 |用于传输层的IP协议类型。可以是“ tcp”或“ udp”。默认值为“ tcp”。|
45+
|max_retry_times|可选的 |连接到日志服务器失败或将日志消息发送到日志服务器失败后的最大重试次数。|
46+
|retry_interval|可选的 |重试连接到日志服务器或重试向日志服务器发送日志消息之前的时间延迟(以毫秒为单位),默认为100(0.1s)。|
47+
|pool_size |可选的 |sock:keepalive使用的Keepalive池大小。默认为10。|
48+
49+
## 如何开启
50+
51+
1. 下面例子展示了如何为指定路由开启 `sys-logger` 插件的。
52+
53+
```shell
54+
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
55+
{
56+
"username": "foo",
57+
"plugins": {
58+
"plugins": {
59+
"syslog": {
60+
"host" : "127.0.0.1",
61+
"port" : 5044,
62+
"flush_limit" : 1
63+
}
64+
},
65+
"upstream": {
66+
"type": "roundrobin",
67+
"nodes": {
68+
"127.0.0.1:1980": 1
69+
}
70+
},
71+
"uri": "/hello"
72+
}
73+
}'
74+
```
75+
76+
## 测试插件
77+
78+
* 成功的情况:
79+
80+
```shell
81+
$ curl -i http://127.0.0.1:9080/hello
82+
HTTP/1.1 200 OK
83+
...
84+
hello, world
85+
```
86+
87+
## 禁用插件
88+
89+
90+
想要禁用“sys-logger”插件,是非常简单的,将对应的插件配置从json配置删除,就会立即生效,不需要重新启动服务:
91+
92+
```shell
93+
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -X PUT -d value='
94+
{
95+
"methods": ["GET"],
96+
"uri": "/hello",
97+
"plugins": {},
98+
"upstream": {
99+
"type": "roundrobin",
100+
"nodes": {
101+
"127.0.0.1:1980": 1
102+
}
103+
}
104+
}'
105+
```

doc/plugins/syslog.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!--
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
-->
19+
20+
# Summary
21+
- [**Name**](#name)
22+
- [**Attributes**](#attributes)
23+
- [**How To Enable**](#how-to-enable)
24+
- [**Test Plugin**](#test-plugin)
25+
- [**Disable Plugin**](#disable-plugin)
26+
27+
28+
## Name
29+
30+
`sys` is a plugin which push Log data requests to Syslog.
31+
32+
This will provide the ability to send Log data requests as JSON objects.
33+
34+
## Attributes
35+
36+
|Name |Requirement |Description|
37+
|--------- |-------- |-----------|
38+
|host |required | IP address or the Hostname.|
39+
|port |required | Target upstream port.|
40+
|timeout |optional |Timeout for the upstream to send data.|
41+
|tls |optional |Boolean value to control whether to perform SSL verification|
42+
|flush_limit |optional |If the buffered messages' size plus the current message size reaches (>=) this limit (in bytes), the buffered log messages will be written to log server. Default to 4096 (4KB).|
43+
|drop_limit |optional |If the buffered messages' size plus the current message size is larger than this limit (in bytes), the current log message will be dropped because of limited buffer size. Default drop_limit is 1048576 (1MB).|
44+
|sock_type|optional |IP protocol type to use for transport layer. Can be either "tcp" or "udp". Default is "tcp".|
45+
|max_retry_times|optional |Max number of retry times after a connect to a log server failed or send log messages to a log server failed.|
46+
|retry_interval|optional |The time delay (in ms) before retry to connect to a log server or retry to send log messages to a log server, default to 100 (0.1s).|
47+
|pool_size |optional |Keepalive pool size used by sock:keepalive. Default to 10.|
48+
49+
## How To Enable
50+
51+
The following is an example on how to enable the sys-logger for a specific route.
52+
53+
```shell
54+
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
55+
{
56+
"username": "foo",
57+
"plugins": {
58+
"plugins": {
59+
"syslog": {
60+
"host" : "127.0.0.1",
61+
"port" : 5044,
62+
"flush_limit" : 1
63+
}
64+
},
65+
"upstream": {
66+
"type": "roundrobin",
67+
"nodes": {
68+
"127.0.0.1:1980": 1
69+
}
70+
},
71+
"uri": "/hello"
72+
}
73+
}'
74+
```
75+
76+
## Test Plugin
77+
78+
* success:
79+
80+
```shell
81+
$ curl -i http://127.0.0.1:9080/hello
82+
HTTP/1.1 200 OK
83+
...
84+
hello, world
85+
```
86+
87+
## Disable Plugin
88+
89+
Remove the corresponding json configuration in the plugin configuration to disable the `sys-logger`.
90+
APISIX plugins are hot-reloaded, therefore no need to restart APISIX.
91+
92+
```shell
93+
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -X PUT -d value='
94+
{
95+
"methods": ["GET"],
96+
"uri": "/hello",
97+
"plugins": {},
98+
"upstream": {
99+
"type": "roundrobin",
100+
"nodes": {
101+
"127.0.0.1:1980": 1
102+
}
103+
}
104+
}'
105+
```

rockspec/apisix-master-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies = {
5050
"jsonschema = 0.8",
5151
"lua-resty-ipmatcher = 0.6",
5252
"lua-resty-kafka = 0.07",
53+
"lua-resty-logger-socket = 2.0-0",
5354
}
5455

5556
build = {

t/admin/plugins.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ __DATA__
3030
--- request
3131
GET /apisix/admin/plugins/list
3232
--- response_body_like eval
33-
qr/\["limit-req","limit-count","limit-conn","key-auth","basic-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode","serverless-pre-function","serverless-post-function","openid-connect","proxy-rewrite","redirect","response-rewrite","fault-injection","udp-logger","wolf-rbac","proxy-cache","tcp-logger","proxy-mirror","kafka-logger","cors","batch-requests"\]/
33+
qr/\["limit-req","limit-count","limit-conn","key-auth","basic-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode","serverless-pre-function","serverless-post-function","openid-connect","proxy-rewrite","redirect","response-rewrite","fault-injection","udp-logger","wolf-rbac","proxy-cache","tcp-logger","proxy-mirror","kafka-logger","cors","syslog","batch-requests"\]/
3434
--- no_error_log
3535
[error]
3636

t/debug/debug-mode.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ loaded plugin and sort by priority: 506 name: grpc-transcode
7878
loaded plugin and sort by priority: 500 name: prometheus
7979
loaded plugin and sort by priority: 405 name: tcp-logger
8080
loaded plugin and sort by priority: 403 name: kafka-logger
81+
loaded plugin and sort by priority: 401 name: syslog
8182
loaded plugin and sort by priority: 400 name: udp-logger
8283
loaded plugin and sort by priority: 0 name: example-plugin
8384
loaded plugin and sort by priority: -1000 name: zipkin

0 commit comments

Comments
 (0)