You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/01/20 05:23:14 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #6113: feat: development of Loggly logging plugin

spacewander commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788354530



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",

Review comment:
       We can get the list via `severity` table?

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},

Review comment:
       Could we refactor it to avoid adding the same group of fields to each logger?

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},
+        include_resp_body = {type = "boolean", default = false},
+        include_resp_body_expr = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "array"
+            }
+        },
+        tags = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string"
+            }
+        },
+        prefer_name = {type = "boolean", default = true}
+    },
+    required = {"customer_token"}
+}
+
+
+local defaults = {
+    host = "logs-01.loggly.com",
+    port = 514,
+    protocol = "syslog",
+    timeout = 5000
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {
+            type = "string",
+            default = defaults.host
+        },
+        port = {
+            type = "integer",
+            default = defaults.port
+        },
+        protocol = {
+            type = "string",
+            default = defaults.protocol,
+            -- more methods coming soon
+            enum = {"syslog"}
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            default= defaults.timeout
+        },
+        log_format = {
+            type = "object",
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 411,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+    metadata_schema = metadata_schema
+}
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_METADATA then
+        return core.schema.check(metadata_schema, conf)
+    end
+    return core.schema.check(schema, conf)
+end
+
+local function generate_log_message(conf, ctx)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    local entry
+
+    if metadata and metadata.value.log_format
+       and core.table.nkeys(metadata.value.log_format) > 0
+    then
+        entry = log_util.get_custom_format_log(ctx, metadata.value.log_format)
+    else
+        entry = log_util.get_full_log(ngx, conf)
+    end
+
+    if conf.prefer_name then
+        if entry.service_id and entry.service_id ~= "" then
+            local svc = service_fetch(entry.service_id)
+
+            if svc and svc.value.name ~= "" then
+                entry.service_id =  svc.value.name
+            end
+        end
+
+        if ctx.route_name and ctx.route_name ~= "" then
+            entry.route_id = ctx.route_name
+        end
+    end
+
+    -- generate rfc5424 compliant syslog event
+    local json_str, err = core.json.encode(entry)
+    if not json_str then
+        core.log.error('error occurred while encoding the data: ', err)
+        return nil
+    end
+
+    local timestamp = log_util.get_rfc3339_zulu_timestamp()
+    local taglist = {}
+    if conf.tags then
+        for i = 1, #conf.tags do
+            if not conf.tags[i]:sub(1, 4) ~= "tag=" then

Review comment:
       Better to check it with jsonschema?

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},
+        include_resp_body = {type = "boolean", default = false},
+        include_resp_body_expr = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "array"
+            }
+        },
+        tags = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string"
+            }
+        },
+        prefer_name = {type = "boolean", default = true}
+    },
+    required = {"customer_token"}
+}
+
+
+local defaults = {
+    host = "logs-01.loggly.com",
+    port = 514,
+    protocol = "syslog",
+    timeout = 5000
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {
+            type = "string",
+            default = defaults.host
+        },
+        port = {
+            type = "integer",
+            default = defaults.port
+        },
+        protocol = {
+            type = "string",
+            default = defaults.protocol,
+            -- more methods coming soon
+            enum = {"syslog"}
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            default= defaults.timeout
+        },
+        log_format = {
+            type = "object",
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 411,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+    metadata_schema = metadata_schema
+}
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_METADATA then
+        return core.schema.check(metadata_schema, conf)
+    end
+    return core.schema.check(schema, conf)

Review comment:
       Need to call `log_util.check_log_schema(conf)`

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},
+        include_resp_body = {type = "boolean", default = false},
+        include_resp_body_expr = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "array"
+            }
+        },
+        tags = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string"
+            }
+        },
+        prefer_name = {type = "boolean", default = true}
+    },
+    required = {"customer_token"}
+}
+
+
+local defaults = {
+    host = "logs-01.loggly.com",
+    port = 514,
+    protocol = "syslog",
+    timeout = 5000
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {
+            type = "string",
+            default = defaults.host
+        },
+        port = {
+            type = "integer",
+            default = defaults.port
+        },
+        protocol = {
+            type = "string",
+            default = defaults.protocol,
+            -- more methods coming soon
+            enum = {"syslog"}
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            default= defaults.timeout
+        },
+        log_format = {
+            type = "object",
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 411,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+    metadata_schema = metadata_schema
+}
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_METADATA then
+        return core.schema.check(metadata_schema, conf)
+    end
+    return core.schema.check(schema, conf)
+end
+
+local function generate_log_message(conf, ctx)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    local entry
+
+    if metadata and metadata.value.log_format

Review comment:
       Let's refactor it in the next PR

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},
+        include_resp_body = {type = "boolean", default = false},
+        include_resp_body_expr = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "array"
+            }
+        },
+        tags = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string"
+            }
+        },
+        prefer_name = {type = "boolean", default = true}
+    },
+    required = {"customer_token"}
+}
+
+
+local defaults = {
+    host = "logs-01.loggly.com",
+    port = 514,
+    protocol = "syslog",
+    timeout = 5000
+}
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        host = {
+            type = "string",
+            default = defaults.host
+        },
+        port = {
+            type = "integer",
+            default = defaults.port
+        },
+        protocol = {
+            type = "string",
+            default = defaults.protocol,
+            -- more methods coming soon
+            enum = {"syslog"}
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            default= defaults.timeout
+        },
+        log_format = {
+            type = "object",
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 411,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+    metadata_schema = metadata_schema
+}
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_METADATA then
+        return core.schema.check(metadata_schema, conf)
+    end
+    return core.schema.check(schema, conf)
+end
+
+local function generate_log_message(conf, ctx)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    local entry
+
+    if metadata and metadata.value.log_format
+       and core.table.nkeys(metadata.value.log_format) > 0
+    then
+        entry = log_util.get_custom_format_log(ctx, metadata.value.log_format)
+    else
+        entry = log_util.get_full_log(ngx, conf)
+    end
+
+    if conf.prefer_name then

Review comment:
       It seems we don't need this as we support customizing format?

##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,243 @@
+--
+-- 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 plugin = require("apisix.plugin")
+local bp_manager_mod = require("apisix.utils.batch-processor-manager")
+local log_util = require("apisix.utils.log-util")
+local service_fetch = require("apisix.http.service").get
+local ngx = ngx
+local tostring = tostring
+local tab_concat = table.concat
+local udp = ngx.socket.udp
+
+local plugin_name = "loggly"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local severity = {
+    EMEGR = 0,              --  system is unusable
+    ALERT = 1,              --  action must be taken immediately
+    CRIT = 2,               --  critical conditions
+    ERR = 3,                --  error conditions
+    WARNING = 4,            --  warning conditions
+    NOTICE = 5,             --  normal but significant condition
+    INFO = 6,               --  informational
+    DEBUG = 7,              --  debug-level messages
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = {"DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMEGR",
+                    "debug", "info", "notice", "warning", "err", "crit", "alert", "emegr"},
+            description = "base severity log level",
+        },
+        include_req_body = {type = "boolean", default = false},

Review comment:
       And you miss to call `log_util.collect_body(conf, ctx)`.

##########
File path: docs/en/latest/plugins/loggly.md
##########
@@ -0,0 +1,157 @@
+---
+title: loggly
+---
+
+<!--
+#
+# 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.
+#
+-->
+
+## Summary
+
+- [Summary](#summary)

Review comment:
       Extra 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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org