16
16
--
17
17
local core = require (" apisix.core" )
18
18
local log_util = require (" apisix.utils.log-util" )
19
+ local batch_processor = require (" apisix.utils.batch-processor" )
19
20
local logger_socket = require (" resty.logger.socket" )
20
21
local plugin_name = " syslog"
21
22
local ngx = ngx
23
+ local buffers = {}
22
24
23
25
local schema = {
24
26
type = " object" ,
25
27
properties = {
26
28
host = {type = " string" },
27
29
port = {type = " integer" },
30
+ name = {type = " string" , default = " sys logger" },
28
31
flush_limit = {type = " integer" , minimum = 1 , default = 4096 },
29
32
drop_limit = {type = " integer" , default = 1048576 },
30
33
timeout = {type = " integer" , minimum = 1 , default = 3 },
31
34
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 },
35
+ max_retry_times = {type = " integer" , minimum = 1 , default = 1 },
36
+ retry_interval = {type = " integer" , minimum = 0 , default = 1 },
34
37
pool_size = {type = " integer" , minimum = 5 , default = 5 },
35
38
tls = {type = " boolean" , default = false },
39
+ batch_max_size = {type = " integer" , minimum = 1 , default = 1000 },
40
+ buffer_duration = {type = " integer" , minimum = 1 , default = 60 },
36
41
},
37
42
required = {" host" , " port" }
38
43
}
@@ -59,14 +64,9 @@ function _M.flush_syslog(logger)
59
64
end
60
65
end
61
66
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
67
+ local function send_syslog_data (conf , log_message )
68
+ local err_msg
69
+ local res = true
70
70
71
71
-- fetch api_ctx
72
72
local api_ctx = ngx .ctx .api_ctx
@@ -91,14 +91,72 @@ function _M.log(conf)
91
91
})
92
92
93
93
if not logger then
94
- core .log .error (" failed when initiating the sys logger processor" , err )
94
+ res = false
95
+ err_msg = " failed when initiating the sys logger processor" .. err
95
96
end
96
97
97
98
-- reuse the logger object
98
- local ok , err = logger :log (core .json .encode (entry ))
99
+ local ok , err = logger :log (core .json .encode (log_message ))
99
100
if not ok then
100
- core .log .error (" failed to log message" , err )
101
+ res = false
102
+ err_msg = " failed to log message" .. err
103
+ end
104
+
105
+ return res , err_msg
106
+ end
107
+
108
+ -- log phase in APISIX
109
+ function _M .log (conf )
110
+ local entry = log_util .get_full_log (ngx )
111
+
112
+ if not entry .route_id then
113
+ core .log .error (" failed to obtain the route id for sys logger" )
114
+ return
101
115
end
116
+
117
+ local log_buffer = buffers [entry .route_id ]
118
+
119
+ if log_buffer then
120
+ log_buffer :push (entry )
121
+ return
122
+ end
123
+
124
+ -- Generate a function to be executed by the batch processor
125
+ local func = function (entries , batch_max_size )
126
+ local data , err
127
+ if batch_max_size == 1 then
128
+ data , err = core .json .encode (entries [1 ]) -- encode as single {}
129
+ else
130
+ data , err = core .json .encode (entries ) -- encode as array [{}]
131
+ end
132
+
133
+ if not data then
134
+ return false , ' error occurred while encoding the data: ' .. err
135
+ end
136
+
137
+ return send_syslog_data (conf , data )
138
+ end
139
+
140
+ local config = {
141
+ name = conf .name ,
142
+ retry_delay = conf .retry_interval ,
143
+ batch_max_size = conf .batch_max_size ,
144
+ max_retry_count = conf .max_retry_times ,
145
+ buffer_duration = conf .buffer_duration ,
146
+ inactive_timeout = conf .timeout ,
147
+ }
148
+
149
+ local err
150
+ log_buffer , err = batch_processor :new (func , config )
151
+
152
+ if not log_buffer then
153
+ core .log .error (" error when creating the batch processor: " , err )
154
+ return
155
+ end
156
+
157
+ buffers [entry .route_id ] = log_buffer
158
+ log_buffer :push (entry )
159
+
102
160
end
103
161
104
162
return _M
0 commit comments