sshniro commented on a change in pull request #1355: Updating the UDP logger to use the batch processor util URL: https://github.com/apache/incubator-apisix/pull/1355#discussion_r402148439
########## File path: lua/apisix/plugins/udp-logger.lua ########## @@ -46,38 +51,85 @@ 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_udp_data(conf, log_message) + local err_msg + local res = true local sock = udp() - sock:settimeout(conf.timeout) - + sock:settimeout(conf.timeout * 1000) local ok, err = sock:setpeername(conf.host, conf.port) + if not ok then - core.log.error("failed to connect to UDP server: host[", - conf.host, "] port[", conf.port, "] ", err) - return + return nil, "failed to connect to UDP server: host[" .. conf.host + .. "] port[" .. tostring(conf.port) .. "] err: " .. err end ok, err = sock:send(log_message) if not ok then - core.log.error("failed to send data to UDP server: host[", - conf.host, "] port[", conf.port, "] ", err) + res = false + err_msg = "failed to send data to UDP 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 UDP 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 tcp logger") + return + end + + local log_buffer = buffers[entry.route_id] + + if log_buffer then + log_buffer:push(entry) + return + end + + -- Generate a function to be executed by the batch processor + local func = function(entries, batch_max_size) Review comment: If we move it to a top-level function then we also need to persist the entire `conf` object coming from the M.log function. As it is used by the `send_udp_data` function. Now we are using the conf object via the closure. Shall I move it to top then? ---------------------------------------------------------------- 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