[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399581354
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 if conf.tls then
 ok, err = sock:sslhandshake(true, conf.tls_options, false)
 if not ok then
-core.log.error("failed to to perform TLS handshake to TCP server: 
host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to to perform TLS handshake to TCP server: 
host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. 
err
 end
 end
 
 ok, err = sock:send(log_message)
 if not ok then
-core.log.error("failed to send data to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
+res = false
+err_msg = "failed to send data to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 ok, err = sock:close()
 if not ok then
 core.log.error("failed to close the TCP connection, host[",
-conf.host, "] port[", conf.port, "] ", err)
+conf.host, "] port[", conf.port, "] ", err)
 end
+
+return res, err_msg
 end
 
 
 function _M.log(conf)
-return timer_at(0, log, conf, core.json.encode(log_util.get_full_log(ngx)))
+local entry = log_util.get_full_log(ngx)
+
+if not entry.route_id then
+core.log.error("failed to obtain the route id for udp logger")
+return
+end
+
+local log_buffer = buffers[entry.route_id]
+
+-- If a logger is not present for the route, create one
+if not log_buffer then
+-- Generate a function to be executed by the batch processor
+local func = function(entries, batch_max_size)
+local data
+if batch_max_size == 1 then
+data = core.json.encode(entries[1]) -- encode as single {}
 
 Review comment:
   We should catch the error message.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399581928
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 if conf.tls then
 ok, err = sock:sslhandshake(true, conf.tls_options, false)
 if not ok then
-core.log.error("failed to to perform TLS handshake to TCP server: 
host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to to perform TLS handshake to TCP server: 
host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. 
err
 end
 end
 
 ok, err = sock:send(log_message)
 if not ok then
-core.log.error("failed to send data to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
+res = false
+err_msg = "failed to send data to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 ok, err = sock:close()
 if not ok then
 core.log.error("failed to close the TCP connection, host[",
-conf.host, "] port[", conf.port, "] ", err)
+conf.host, "] port[", conf.port, "] ", err)
 end
+
+return res, err_msg
 end
 
 
 function _M.log(conf)
-return timer_at(0, log, conf, core.json.encode(log_util.get_full_log(ngx)))
+local entry = log_util.get_full_log(ngx)
+
+if not entry.route_id then
+core.log.error("failed to obtain the route id for udp logger")
+return
+end
+
+local log_buffer = buffers[entry.route_id]
+
+-- If a logger is not present for the route, create one
+if not log_buffer then
+-- Generate a function to be executed by the batch processor
+local func = function(entries, batch_max_size)
+local data
+if batch_max_size == 1 then
+data = core.json.encode(entries[1]) -- encode as single {}
+else
+data = core.json.encode(entries) -- encode as array [{}]
+end
+return send_tcp_data(conf, data)
+end
+
+local config = {
+name = conf.name,
+retry_delay = conf.retry_delay,
+batch_max_size = conf.batch_max_size,
+max_retry_count = conf.max_retry_count,
+buffer_duration = conf.buffer_duration,
+inactive_timeout = conf.inactive_timeout,
+}
+
+local err
+log_buffer, err = batch_processor:new(func, config)
+
+if not log_buffer then
+core.log.err("error when creating the batch processor: " .. err)
 
 Review comment:
   we should avoid generating new object.
   
   `core.log.error("error when creating the batch processor: ", err)`


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399582801
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 if conf.tls then
 ok, err = sock:sslhandshake(true, conf.tls_options, false)
 if not ok then
-core.log.error("failed to to perform TLS handshake to TCP server: 
host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to to perform TLS handshake to TCP server: 
host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. 
err
 end
 end
 
 ok, err = sock:send(log_message)
 if not ok then
-core.log.error("failed to send data to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
+res = false
 
 Review comment:
   this style is better: `return false, "failed to send "`


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399583847
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
 
 Review comment:
   I think we can use "false" instead of "nil", it is more reasonable.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399582039
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 if conf.tls then
 ok, err = sock:sslhandshake(true, conf.tls_options, false)
 if not ok then
-core.log.error("failed to to perform TLS handshake to TCP server: 
host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to to perform TLS handshake to TCP server: 
host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. 
err
 end
 end
 
 ok, err = sock:send(log_message)
 if not ok then
-core.log.error("failed to send data to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
+res = false
+err_msg = "failed to send data to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 ok, err = sock:close()
 if not ok then
 core.log.error("failed to close the TCP connection, host[",
-conf.host, "] port[", conf.port, "] ", err)
+conf.host, "] port[", conf.port, "] ", err)
 end
+
+return res, err_msg
 end
 
 
 function _M.log(conf)
-return timer_at(0, log, conf, core.json.encode(log_util.get_full_log(ngx)))
+local entry = log_util.get_full_log(ngx)
+
+if not entry.route_id then
+core.log.error("failed to obtain the route id for udp logger")
+return
+end
+
+local log_buffer = buffers[entry.route_id]
+
+-- If a logger is not present for the route, create one
+if not log_buffer then
+-- Generate a function to be executed by the batch processor
+local func = function(entries, batch_max_size)
+local data
+if batch_max_size == 1 then
+data = core.json.encode(entries[1]) -- encode as single {}
+else
+data = core.json.encode(entries) -- encode as array [{}]
+end
+return send_tcp_data(conf, data)
+end
+
+local config = {
+name = conf.name,
+retry_delay = conf.retry_delay,
+batch_max_size = conf.batch_max_size,
+max_retry_count = conf.max_retry_count,
+buffer_duration = conf.buffer_duration,
+inactive_timeout = conf.inactive_timeout,
+}
+
+local err
+log_buffer, err = batch_processor:new(func, config)
+
+if not log_buffer then
+core.log.err("error when creating the batch processor: " .. err)
 
 Review comment:
   `core.log.err` is wrong, it should be `core.log.error`


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399584496
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -48,51 +53,92 @@ function _M.check_schema(conf)
 return core.schema.check(schema, conf)
 end
 
-local function log(premature, conf, log_message)
-if premature then
-return
-end
+local function send_tcp_data(conf, log_message)
+local err_msg
+local res = true
+local sock, soc_err = tcp()
 
-local sock,err = tcp()
 if not sock then
-core.log.error("failed to init the socket", err)
-return
+return nil, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout)
+sock:settimeout(conf.timeout * 1000)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-core.log.error("failed to connect to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to connect to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 if conf.tls then
 ok, err = sock:sslhandshake(true, conf.tls_options, false)
 if not ok then
-core.log.error("failed to to perform TLS handshake to TCP server: 
host[",
-conf.host, "] port[", conf.port, "] ", err)
-return
+return nil, "failed to to perform TLS handshake to TCP server: 
host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. 
err
 end
 end
 
 ok, err = sock:send(log_message)
 if not ok then
-core.log.error("failed to send data to TCP server: host[",
-conf.host, "] port[", conf.port, "] ", err)
+res = false
+err_msg = "failed to send data to TCP server: host[" ..
+conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 end
 
 ok, err = sock:close()
 if not ok then
 core.log.error("failed to close the TCP connection, host[",
-conf.host, "] port[", conf.port, "] ", err)
+conf.host, "] port[", conf.port, "] ", err)
 end
+
+return res, err_msg
 end
 
 
 function _M.log(conf)
-return timer_at(0, log, conf, core.json.encode(log_util.get_full_log(ngx)))
+local entry = log_util.get_full_log(ngx)
+
+if not entry.route_id then
+core.log.error("failed to obtain the route id for udp logger")
+return
+end
+
+local log_buffer = buffers[entry.route_id]
 
 Review comment:
   how about this style:
   
   ```lua
   local log_buffer = buffers[entry.route_id]
   if log_buffer then
   log_buffer:push(entry)
   return
   end
   
   local func = function(entries, batch_max_size)
   -- ...
   
   log_buffer:push(entry)
   
   ```


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-27 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399580985
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -16,22 +16,27 @@
 --
 local core = require("apisix.core")
 local log_util = require("apisix.utils.log-util")
+local batch_processor = require("apisix.utils.batch-processor")
 local plugin_name = "tcp-logger"
+local tostring = tostring
+local buffers = {}
 local ngx = ngx
-
-local timer_at = ngx.timer.at
 local tcp = ngx.socket.tcp
 
 local schema = {
 type = "object",
 properties = {
 host = {type = "string"},
 port = {type = "integer", minimum = 0},
-timeout = {   -- timeout in milliseconds
-type = "integer", minimum = 1, default= 1000
-},
-tls = { type = "boolean", default = false },
-tls_options = { type = "string" }
+tls = {type = "boolean", default = false},
+tls_options = {type = "string"},
+timeout = {type = "integer", minimum = 1, default = 3},
 
 Review comment:
   I think it can be kept in milliseconds.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-29 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399891484
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -59,21 +59,21 @@ local function send_tcp_data(conf, log_message)
 local sock, soc_err = tcp()
 
 if not sock then
-return nil, "failed to init the socket" .. soc_err
+return false, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout * 1000)
+sock:settimeout(conf.timeout)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-return nil, "failed to connect to TCP server: host[" ..
+return false, "failed to connect to TCP server: host[" ..
 conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 
 Review comment:
   please fix other similar points


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-29 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r399891409
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -59,21 +59,21 @@ local function send_tcp_data(conf, log_message)
 local sock, soc_err = tcp()
 
 if not sock then
-return nil, "failed to init the socket" .. soc_err
+return false, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout * 1000)
+sock:settimeout(conf.timeout)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-return nil, "failed to connect to TCP server: host[" ..
+return false, "failed to connect to TCP server: host[" ..
 conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 
 Review comment:
   Bad indentation, here is good style:
   
   ```lua
   return false, "failed to connect to TCP server: host[" ..
  conf.host .. "] port[" .. tostring(conf.port) .. "] 
err: " .. err
   ```


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-apisix] membphis commented on a change in pull request #1356: Updating TCP logger to use the batch processor util

2020-03-30 Thread GitBox
membphis commented on a change in pull request #1356: Updating TCP logger to 
use the batch processor util
URL: https://github.com/apache/incubator-apisix/pull/1356#discussion_r400597617
 
 

 ##
 File path: lua/apisix/plugins/tcp-logger.lua
 ##
 @@ -59,21 +59,21 @@ local function send_tcp_data(conf, log_message)
 local sock, soc_err = tcp()
 
 if not sock then
-return nil, "failed to init the socket" .. soc_err
+return false, "failed to init the socket" .. soc_err
 end
 
-sock:settimeout(conf.timeout * 1000)
+sock:settimeout(conf.timeout)
 
 local ok, err = sock:connect(conf.host, conf.port)
 if not ok then
-return nil, "failed to connect to TCP server: host[" ..
+return false, "failed to connect to TCP server: host[" ..
 conf.host .. "] port[" .. tostring(conf.port) .. "] err: " .. err
 
 Review comment:
   you are right. the new style is clearer.


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:
us...@infra.apache.org


With regards,
Apache Git Services