membphis commented on a change in pull request #2177: URL: https://github.com/apache/apisix/pull/2177#discussion_r518047713
########## File path: apisix/plugins/sls-logger.lua ########## @@ -0,0 +1,213 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local log_util = require("apisix.utils.log-util") +local batch_processor = require("apisix.utils.batch-processor") +local plugin_name = "sls-logger" +local ngx = ngx +local rf5424 = require("apisix.plugins.slslog.rfc5424") +local stale_timer_running = false; +local timer_at = ngx.timer.at +local tcp = ngx.socket.tcp +local buffers = {} +local tostring = tostring +local ipairs = ipairs +local schema = { + type = "object", + properties = { + include_req_body = {type = "boolean", default = false}, + name = {type = "string", default = "sls-logger"}, + timeout = {type = "integer", minimum = 1, default= 5000}, + max_retry_count = {type = "integer", minimum = 0, default = 0}, + retry_delay = {type = "integer", minimum = 0, default = 1}, + buffer_duration = {type = "integer", minimum = 1, default = 60}, + inactive_timeout = {type = "integer", minimum = 1, default = 5}, + batch_max_size = {type = "integer", minimum = 1, default = 1000}, + host = {type = "string"}, + port = {type = "integer"}, + project = {type = "string"}, + logstore = {type = "string"}, + access_key_id = {type = "string"}, + access_key_secret = {type ="string"} + }, + required = {"host", "port", "project", "logstore", "access_key_id", "access_key_secret"} +} + +local _M = { + version = 0.1, + priority = 406, + name = plugin_name, + schema = schema, +} + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + +local function send_tcp_data(route_conf, log_message) + local err_msg + local res = true + local sock, soc_err = tcp() + local can_close + + if not sock then + err_msg = "failed to init the socket" .. soc_err + core.log.error(err_msg) Review comment: we should avoid to concat string in Lua land, it'll make the GC busy. we should use this style: `core.log.error("failed to init the socket", soc_err)` please fix the similar points. ########## File path: apisix/plugins/slslog/rfc5424.lua ########## @@ -0,0 +1,107 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local LOG_EMERG = 0 -- system is unusable +local LOG_ALERT = 1 -- action must be taken immediately +local LOG_CRIT = 2 -- critical conditions +local LOG_ERR = 3 -- error conditions +local LOG_WARNING = 4 -- warning conditions +local LOG_NOTICE = 5 -- normal but significant condition +local LOG_INFO = 6 -- informational +local LOG_DEBUG = 7 -- debug-level messages + +local LOG_KERN = 0 -- kernel messages +local LOG_USER = 1 -- random user-level messages +local LOG_MAIL = 2 -- mail system +local LOG_DAEMON = 3 -- system daemons +local LOG_AUTH = 4 -- security/authorization messages +local LOG_SYSLOG = 5 -- messages generated internally by syslogd +local LOG_LPR = 6 -- line printer subsystem +local LOG_NEWS = 7 -- network news subsystem +local LOG_UUCP = 8 -- UUCP subsystem +local LOG_CRON = 9 -- clock daemon +local LOG_AUTHPRIV = 10 -- security/authorization messages (private) +local LOG_FTP = 11 -- FTP daemon +local LOG_LOCAL0 = 16 -- reserved for local use +local LOG_LOCAL1 = 17 -- reserved for local use +local LOG_LOCAL2 = 18 -- reserved for local use +local LOG_LOCAL3 = 19 -- reserved for local use +local LOG_LOCAL4 = 20 -- reserved for local use +local LOG_LOCAL5 = 21 -- reserved for local use +local LOG_LOCAL6 = 22 -- reserved for local use +local LOG_LOCAL7 = 23 -- reserved for local use + +local Facility = { + ["KERN"] = LOG_KERN, + ["USER"] = LOG_USER, + ["MAIL"] = LOG_MAIL, + ["DAEMON"] = LOG_DAEMON, + ["AUTH"] = LOG_AUTH, + ["SYSLOG"] = LOG_SYSLOG, + ["LPR"] = LOG_LPR, + ["NEWS"] = LOG_NEWS, + ["UUCP"] = LOG_UUCP, + ["CRON"] = LOG_CRON, + ["AUTHPRIV"] = LOG_AUTHPRIV, + ["FTP"] = LOG_FTP, + ["LOCAL0"] = LOG_LOCAL0, + ["LOCAL1"] = LOG_LOCAL1, + ["LOCAL2"] = LOG_LOCAL2, + ["LOCAL3"] = LOG_LOCAL3, + ["LOCAL4"] = LOG_LOCAL4, + ["LOCAL5"] = LOG_LOCAL5, + ["LOCAL6"] = LOG_LOCAL6, + ["LOCAL7"] = LOG_LOCAL7, +} + +local Severity = { + ["EMEGR"] = LOG_EMERG, + ["ALERT"] = LOG_ALERT, + ["CRIT"] = LOG_CRIT, + ["ERR"] = LOG_ERR, + ["WARNING"] = LOG_WARNING, + ["NOTICE"] = LOG_NOTICE, + ["INFO"] = LOG_INFO, + ["DEBUG"] = LOG_DEBUG, +} +local os_date = os.date +local ngx = ngx +local string_format = string.format +local rfc5424_timestamp_format = "!%Y-%m-%dT%H:%M:%S.000Z" +local rfc5424_format = "<%d>1 %s %s %s %d - [logservice project=\"%s\" logstore=\"%s\"" .. + " access-key-id=\"%s\" access-key-secret=\"%s\"] %s\n" + +local _M = { version = 0.1 } + +function _M.encode(facility, severity, hostname, appname, pid, project, + logstore, access_key_id, access_key_secret, msg) + local pri = (Facility[facility] * 8 + Severity[severity]) + ngx.update_time() + local t = os_date(rfc5424_timestamp_format, ngx.now()) + + if not hostname then + hostname = "-" + end + + if not appname then + appname = "-" + end + + return string_format(rfc5424_format, pri, t, hostname, appname, pid, project, + logstore, access_key_id, access_key_secret, msg) Review comment: bad indentation ########## File path: apisix/plugins/sls-logger.lua ########## @@ -0,0 +1,213 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local log_util = require("apisix.utils.log-util") +local batch_processor = require("apisix.utils.batch-processor") +local plugin_name = "sls-logger" +local ngx = ngx +local rf5424 = require("apisix.plugins.slslog.rfc5424") +local stale_timer_running = false; +local timer_at = ngx.timer.at +local tcp = ngx.socket.tcp +local buffers = {} +local tostring = tostring +local ipairs = ipairs +local schema = { + type = "object", + properties = { + include_req_body = {type = "boolean", default = false}, + name = {type = "string", default = "sls-logger"}, + timeout = {type = "integer", minimum = 1, default= 5000}, + max_retry_count = {type = "integer", minimum = 0, default = 0}, + retry_delay = {type = "integer", minimum = 0, default = 1}, + buffer_duration = {type = "integer", minimum = 1, default = 60}, + inactive_timeout = {type = "integer", minimum = 1, default = 5}, + batch_max_size = {type = "integer", minimum = 1, default = 1000}, + host = {type = "string"}, + port = {type = "integer"}, + project = {type = "string"}, + logstore = {type = "string"}, + access_key_id = {type = "string"}, + access_key_secret = {type ="string"} + }, + required = {"host", "port", "project", "logstore", "access_key_id", "access_key_secret"} +} + +local _M = { + version = 0.1, + priority = 406, + name = plugin_name, + schema = schema, +} + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + +local function send_tcp_data(route_conf, log_message) + local err_msg + local res = true + local sock, soc_err = tcp() + local can_close + + if not sock then + err_msg = "failed to init the socket" .. soc_err + core.log.error(err_msg) + return false, err_msg + end + + sock:settimeout(route_conf.timeout) + local ok, err = sock:connect(route_conf.host, route_conf.port) + if not ok then + err_msg = "failed to connect to TCP server: host[" .. route_conf.host + .. "] port[" .. tostring(route_conf.port) .. "] err: " .. err + core.log.error(err_msg) + return false, err_msg + end + + ok, err = sock:sslhandshake(true, nil, false) + if not ok then + err_msg = "failed to to perform TLS handshake to TCP server: host[" + .. route_conf.host .. "] port[" .. tostring(route_conf.port) .. "] err: " .. err + core.log.error(err_msg) + return false, err_msg + end + + core.log.info("sls logger send data ", log_message) + ok, err = sock:send(log_message) + if not ok then + res = false + can_close = true + err_msg = "failed to send data to TCP server: host[" .. route_conf.host + .. "] port[" .. tostring(route_conf.port) .. "] err: " .. err + core.log.error(err_msg) + else + ok, err = sock:setkeepalive(120 * 1000, 20) + if not ok then + can_close = true + core.log.error("failed to set socket keepalive: host[" .. route_conf.host + .. "] port[" .. tostring(route_conf.port) .. "] err: " .. err) + end + end + + if can_close then + ok, err = sock:close() + if not ok then + core.log.error("failed to close the TCP connection, host[", + route_conf.host, "] port[", route_conf.port, "] ", err) + end + end + + return res, err_msg +end + +-- remove stale objects from the memory after timer expires +local function remove_stale_objects(premature) + if premature then + return + end + + for key, batch in ipairs(buffers) do + if #batch.entry_buffer.entries == 0 and #batch.batch_to_process == 0 then + core.log.warn("removing batch processor stale object, route id:", tostring(key)) + buffers[key] = nil + end + end + + stale_timer_running = false +end + +local function combine_syslog(entries) + local data + for _, entry in ipairs(entries) do + if not data then + data = entry.data + end + + data = data .. entry.data + core.log.info(entry.data) + end + + return data +end + +local function handle_log(entries) + local data = combine_syslog(entries) + if not data then + return true + end + + -- get the config from entries, replace of local value + return send_tcp_data(entries[1].route_conf, data) +end + +-- log phase in APISIX +function _M.log(conf) + local entry = log_util.get_full_log(ngx, conf) + if not entry.route_id then + core.log.error("failed to obtain the route id for sys logger") + return + end + + local json_str, err = core.json.encode(entry) + if not json_str then + core.log.error('error occurred while encoding the data: ', err) + return + end + + local rf5424_data = rf5424.encode("SYSLOG", "INFO", ngx.var.host Review comment: in APISIX, we shuold use `ctx.var.****` to fetch nginx variable. here is an example: https://github.com/apache/apisix/blob/master/apisix/plugins/limit-count.lua#L166 ---------------------------------------------------------------- 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: [email protected]
