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/14 18:04:03 UTC

[GitHub] [apisix] bisakhmondal opened a new pull request #6113: feat: development of Loggly logging plugin

bisakhmondal opened a new pull request #6113:
URL: https://github.com/apache/apisix/pull/6113


   Signed-off-by: Bisakh Mondal <bi...@gmail.com>
   
   ### What this PR does / why we need it:
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   #6112 
   Working implementation. TODO: testing, documentation
   ### Pre-submission checklist:
   
   <!--
   Please follow the PR manners:
   1. Use Draft if the PR is not ready to be reviewed
   2. Test is required for the feat/fix PR, unless you have a good reason
   3. Doc is required for the feat PR
   4. Use a new commit to resolve review instead of `push -f`
   5. If you need to resolve merge conflicts after the PR is reviewed, please merge master but do not rebase
   6. Use "request review" to notify the reviewer once you have resolved the review
   7. Only reviewer can click "Resolve conversation" to mark the reviewer's review resolved
   -->
   
   * [x] Did you explain what problem does this PR solve? Or what new features have been added?
   * [ ] Have you added corresponding test cases?
   * [ ] Have you modified the corresponding document?
   * [x] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
   


-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788365676



##########
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?
   
   Yes, similar to what we did to batchprocessor. Let's handle that in the next PR. 
   
   RE collect_body: Good catch. Adding, Thanks

##########
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:
       Ack.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r791323052



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,344 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_layer4").loggly()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/
+
+
+
+=== TEST 7: collect response body
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- response_body
+opentracing
+--- grep_error_log eval
+qr/message received: [ -~]+/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ] \{"apisix_latency":[\d.]*,"client_ip":"127\.0\.0\.1","latency":[\d.]*,"request":\{"headers":\{"content-type":"application\/x-www-form-urlencoded","host":"127\.0\.0\.1:1984","user-agent":"lua-resty-http\/[\d.]* \(Lua\) ngx_lua\/[\d]*"\},"method":"GET","querystring":\{\},"size":[\d]+,"uri":"\/opentracing","url":"http:\/\/127\.0\.0\.1:1984\/opentracing"\},"response":\{"headers":\{"connection":"close","content-type":"text\/plain","server":"APISIX\/[\d.]+","transfer-encoding":"chunked"\},"size":[\d]*,"status":200\},"route_id":"1","server":\{"hostname":"[ -~]*","version":"[\d.]+"\},"service_id":"","start_time":[\d]*,"upstream":"127\.0\.0\.1:1982","upstream_latency":[\d]*\}/

Review comment:
       We need to configure `include_resp_body_expr` & `include_resp_body` to get the body.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r791400048



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,344 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_layer4").loggly()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/
+
+
+
+=== TEST 7: collect response body
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- response_body
+opentracing
+--- grep_error_log eval
+qr/message received: [ -~]+/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ] \{"apisix_latency":[\d.]*,"client_ip":"127\.0\.0\.1","latency":[\d.]*,"request":\{"headers":\{"content-type":"application\/x-www-form-urlencoded","host":"127\.0\.0\.1:1984","user-agent":"lua-resty-http\/[\d.]* \(Lua\) ngx_lua\/[\d]*"\},"method":"GET","querystring":\{\},"size":[\d]+,"uri":"\/opentracing","url":"http:\/\/127\.0\.0\.1:1984\/opentracing"\},"response":\{"headers":\{"connection":"close","content-type":"text\/plain","server":"APISIX\/[\d.]+","transfer-encoding":"chunked"\},"size":[\d]*,"status":200\},"route_id":"1","server":\{"hostname":"[ -~]*","version":"[\d.]+"\},"service_id":"","start_time":[\d]*,"upstream":"127\.0\.0\.1:1982","upstream_latency":[\d]*\}/

Review comment:
       Done. Also added case for metadata.log_format




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r791035241



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,321 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_dogstatsd").go()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/

Review comment:
       Done.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788569489



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       do we need to check the return value?




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r790425163



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       here n denotes the number of processed entries, and the rest of the entries should be retried. 




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r789064178



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       Yes, if in case, the batch processor fails to process the entries (network error or something like that) the batch will be retried with a retry policy. 
   ref: 
   https://github.com/apache/apisix/blob/e9fab8b2e5b5cb79a302faf8b5c9752766681c63/apisix/utils/batch-processor.lua#L66-L89
   
   However, seeing your comment make me think that we can migrate to a fail-fast approach. It has other benefits.
   
   ```lua
       for i = 1, #entries do
           local ok, err = send_data_over_udp(entries[i])
           if not ok then
               return false, err
           end
       end
       return true
   ```
   Thank you.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r790822853



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       cc @spacewander 




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r790424879



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       Yes, that's an issue here. The way batchprocessor is written, it is meant for consuming each batch in one go (transactional). But in the case of loggly, only bulk events can be sent through HTTP endpoints. So I can think of three potential resolution
   
   1. if err and i==1 -> then return false else true | if some entries has been sent that's ok. we are loosing the rest of the entries. [some entries will be dropped]
   2. use the current behaviour. where some entries might be retried again [duplocation].
   3. Update batch processor to have this fine-grained control
   https://github.com/apache/apisix/blob/a813b5aa8980dd8fa5780af351b6b47b5219d0b9/apisix/utils/batch-processor.lua#L66-L89
   
   update 
   ```lua
   local ok, err = self.func(batch.entries, self.batch_max_size)
   
   -- to this
   local ok, err, n = self.func(batch.entries, self.batch_max_size)
    -- shorten self.entries based on n
   ```
   
   which one do you think would be good? cc @spacewander 
    




-- 
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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#issuecomment-1018084309


   > > need to update the `README.md`
   > 
   > Yes definitely. Do you want this to be handled in this PR or as usual a small PR with a readme update? Thanks.
   
   I suggest doing it in the next PR.


-- 
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



[GitHub] [apisix] bisakhmondal commented on pull request #6113: feat: development of Loggly logging plugin

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#issuecomment-1017139831


   > need to update the `README.md`
   > 
   > ![image](https://user-images.githubusercontent.com/6814606/150271755-737ce3b1-0560-409d-b659-67efa9542d7f.png)
   
   Yes definitely. Do you want this to be handled in this PR or as usual a small PR with a readme update?
   Thanks.


-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788404930



##########
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:
       Done




-- 
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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r789279683



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,321 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_dogstatsd").go()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/

Review comment:
       Let's add more tests:
   1. collect response body
   2. failed to send UDP log




-- 
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



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

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r785289990



##########
File path: apisix/plugins/slslog/rfc5424.lua
##########
@@ -81,7 +81,10 @@ local Severity = {
 local log_util = require("apisix.utils.log-util")
 
 
-local _M = { version = 0.1 }
+local _M = {
+    version = 0.1,
+    severity = Severity

Review comment:
       one PR for one thing. so I think we need to revert this PR

##########
File path: apisix/plugins/slslog/rfc5424.lua
##########
@@ -81,7 +81,10 @@ local Severity = {
 local log_util = require("apisix.utils.log-util")
 
 
-local _M = { version = 0.1 }
+local _M = {
+    version = 0.1,
+    severity = Severity

Review comment:
       why we need to update this code?




-- 
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



[GitHub] [apisix] spacewander merged pull request #6113: feat: development of Loggly logging plugin

Posted by GitBox <gi...@apache.org>.
spacewander merged pull request #6113:
URL: https://github.com/apache/apisix/pull/6113


   


-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788366082



##########
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:
       Ahh, auto-generated from my vscode markdown extension. Removing ASAP

##########
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:
       yes
   




-- 
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



[GitHub] [apisix] bisakhmondal edited a comment on pull request #6113: feat: development of Loggly logging plugin

Posted by GitBox <gi...@apache.org>.
bisakhmondal edited a comment on pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#issuecomment-1017139831


   > need to update the `README.md`
   > 
   
   Yes definitely. Do you want this to be handled in this PR or as usual a small PR with a readme update?
   Thanks.


-- 
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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r789425667



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,256 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+function _M.body_filter(conf, ctx)
+    log_util.collect_body(conf, ctx)
+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
+
+    -- 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
+            core.table.insert(taglist, "tag=\"" .. conf.tags[i] .. "\"")
+        end
+    end
+    local message = {
+        -- facility LOG_USER - random user level message
+        "<".. tostring(8 + severity[conf.severity:upper()]) .. ">1",-- <PRIVAL>1
+        timestamp,                                                  -- timestamp
+        ctx.var.host or "-",                                        -- hostname
+        "apisix",                                                   -- appname
+        ctx.var.pid,                                                -- proc-id
+        "-",                                                        -- msgid
+        "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
+        json_str
+    }
+
+    return tab_concat(message, " ")
+end
+
+
+local function send_data_over_udp(message)
+    local metadata = plugin.plugin_metadata(plugin_name)
+    core.log.info("metadata: ", core.json.delay_encode(metadata))
+
+    if not metadata then
+        core.log.info("received nil metadata: using metadata defaults: ",
+                            core.json.delay_encode(defaults, true))
+        metadata = {}
+        metadata.value = defaults
+    end
+    local err_msg
+    local res = true
+    local sock = udp()
+    local host, port = metadata.value.host, metadata.value.port
+    sock:settimeout(metadata.value.timeout)
+
+    core.log.info("sending a batch logs to ", host, ":", port)
+
+    local ok, err = sock:setpeername(host, port)
+
+    if not ok then
+        return false, "failed to connect to UDP server: host[" .. host
+                    .. "] port[" .. tostring(port) .. "] err: " .. err
+    end
+
+    ok, err = sock:send(message)
+    if not ok then
+        res = false
+        err_msg = "failed to send data to UDP server: host[" .. host
+                  .. "] port[" .. tostring(port) .. "] err:" .. err
+    end
+
+    ok, err = sock:close()
+    if not ok then
+        core.log.error("failed to close the UDP connection, host[",
+                        host, "] port[", port, "] ", err)
+    end
+
+    return res, err_msg
+end
+
+
+local function handle_log(entries)
+    local ok, err
+    for i = 1, #entries do
+        ok, err = send_data_over_udp(entries[i])

Review comment:
       Look like we need to remove the sent entries? Otherwise, they will be retried.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r789570211



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,258 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)

Review comment:
       ```suggestion
       if schema_type == core.schema.TYPE_METADATA then
           return core.schema.check(metadata_schema, conf)
       end
       return core.schema.check(schema, conf)
   ```
   
   can be handled in this way.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r785298487



##########
File path: apisix/plugins/slslog/rfc5424.lua
##########
@@ -81,7 +81,10 @@ local Severity = {
 local log_util = require("apisix.utils.log-util")
 
 
-local _M = { version = 0.1 }
+local _M = {
+    version = 0.1,
+    severity = Severity

Review comment:
       Hi, thanks for the code review and the concern. The current PR need to access the rfc5424 severity map to decode alert status into integer numbers.
   >    local message = {
           -- facility LOG_USER - random user level message
           "<".. tostring(8 + severity[conf.severity]) .. ">1", -- <PRIVAL>1
           timestamp,                                           -- timestamp
           ctx.var.host or "-",                                 -- hostname
           "apisix",                                            -- appname
           ctx.var.pid,                                         -- proc-id
           "-",                                                 -- msgid
           "[" .. conf.customer_token .. "@41058 " .. tab_concat(taglist, " ") .. "]",
           json_str
       }
   
   I have made the field exported here from that module. Okay, reverting the changes back with a local copy of that severity map inside the plugin. Thank you




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r790422599



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,258 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)

Review comment:
       Hi, thanks for the review. As the plugin schema contains, `include_resp_body_expr`, this need to be validated. core.schema.check just performs jsonschema validation based on the constraints set to the schema.  
   https://github.com/apache/apisix/blob/a813b5aa8980dd8fa5780af351b6b47b5219d0b9/apisix/core/schema.lua#L31-L65
   
   while the check_log_schema performs validation on the custom DSL
   https://github.com/apache/apisix/blob/a813b5aa8980dd8fa5780af351b6b47b5219d0b9/apisix/utils/log-util.lua#L214-L228
   
   In this case, we need to perform validation on log_schema. Thanks.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r791035241



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,321 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_dogstatsd").go()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/

Review comment:
       Done.

##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,344 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_layer4").loggly()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/
+
+
+
+=== TEST 7: collect response body
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- response_body
+opentracing
+--- grep_error_log eval
+qr/message received: [ -~]+/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ] \{"apisix_latency":[\d.]*,"client_ip":"127\.0\.0\.1","latency":[\d.]*,"request":\{"headers":\{"content-type":"application\/x-www-form-urlencoded","host":"127\.0\.0\.1:1984","user-agent":"lua-resty-http\/[\d.]* \(Lua\) ngx_lua\/[\d]*"\},"method":"GET","querystring":\{\},"size":[\d]+,"uri":"\/opentracing","url":"http:\/\/127\.0\.0\.1:1984\/opentracing"\},"response":\{"headers":\{"connection":"close","content-type":"text\/plain","server":"APISIX\/[\d.]+","transfer-encoding":"chunked"\},"size":[\d]*,"status":200\},"route_id":"1","server":\{"hostname":"[ -~]*","version":"[\d.]+"\},"service_id":"","start_time":[\d]*,"upstream":"127\.0\.0\.1:1982","upstream_latency":[\d]*\}/

Review comment:
       Done. Also added case for metadata.log_format




-- 
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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r791323052



##########
File path: t/plugin/loggly.t
##########
@@ -0,0 +1,344 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    $block->set_value("stream_conf_enable", 1);
+
+    if (!defined $block->extra_stream_config) {
+        my $stream_config = <<_EOC_;
+    server {
+        listen 8126 udp;
+        content_by_lua_block {
+            -- mock statsd server is just accepts udp connection and log into error.log
+            require("lib.mock_layer4").loggly()
+        }
+    }
+_EOC_
+        $block->set_value("extra_stream_config", $stream_config);
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity check metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.loggly")
+            local configs = {
+                -- full configuration
+                {
+                    customer_token = "TEST-Token-Must-Be-Passed",
+                    severity = "INFO",
+                    tags = {"special-route", "highpriority-route"},
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize schema
+                {
+                    customer_token = "minimized-cofig",
+                },
+                -- property "customer_token" is required
+                {
+                    severity = "DEBUG",
+                },
+                -- unknown severity
+                {
+                    customer_token = "test",
+                    severity = "UNKNOWN",
+                },
+                -- severity in lower case, should pass
+                {
+                    customer_token = "test",
+                    severity = "crit",
+                }
+            }
+
+            for i = 1, #configs do
+                local ok, err = plugin.check_schema(configs[i])
+                if not ok then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body
+passed
+passed
+property "customer_token" is required
+property "severity" validation failed: matches none of the enum values
+passed
+
+
+
+=== TEST 2: set route with loggly enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "test-token",
+                                "batch_max_size": 1
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "name": "loggly-enabled-route",
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: update loggly metadata with host port
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/loggly',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host":"127.0.0.1",
+                        "port": 8126
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "host": "127.0.0.1",
+                            "protocol": "syslog",
+                            "timeout": 5000,
+                            "port": 8126
+                        },
+                        "key": "/apisix/plugin_metadata/loggly"
+                    },
+                    "action": "set"
+                }]])
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: testing udp packet with mock loggly udp suite
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            -- request 1
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+
+            -- request 2
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+opentracing
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]
+message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[test-token\@41058 ]/
+
+
+
+=== TEST 5: checking loggly tags
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "tags": ["abc", "def"]
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <14>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 tag="abc" tag="def"]/
+
+
+
+=== TEST 6: checking loggly log severity
+log severity is calculated based on PRIVAL
+8 + LOG_SEVERITY value
+CRIT has value 2 so test should return PRIVAL <10>
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "loggly": {
+                                "customer_token" : "token-1",
+                                "batch_max_size": 1,
+                                "severity": "CRIT"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- wait: 0.5
+--- response_body
+passed
+opentracing
+--- grep_error_log eval
+qr/message received: .+?(?= \{)/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ]/
+
+
+
+=== TEST 7: collect response body
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, _, body = t("/opentracing", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- response_body
+opentracing
+--- grep_error_log eval
+qr/message received: [ -~]+/
+--- grep_error_log_out eval
+qr/message received: <10>1 [\d\-T:.]+Z [\d.]+ apisix [\d]+ - \[token-1\@41058 ] \{"apisix_latency":[\d.]*,"client_ip":"127\.0\.0\.1","latency":[\d.]*,"request":\{"headers":\{"content-type":"application\/x-www-form-urlencoded","host":"127\.0\.0\.1:1984","user-agent":"lua-resty-http\/[\d.]* \(Lua\) ngx_lua\/[\d]*"\},"method":"GET","querystring":\{\},"size":[\d]+,"uri":"\/opentracing","url":"http:\/\/127\.0\.0\.1:1984\/opentracing"\},"response":\{"headers":\{"connection":"close","content-type":"text\/plain","server":"APISIX\/[\d.]+","transfer-encoding":"chunked"\},"size":[\d]*,"status":200\},"route_id":"1","server":\{"hostname":"[ -~]*","version":"[\d.]+"\},"service_id":"","start_time":[\d]*,"upstream":"127\.0\.0\.1:1982","upstream_latency":[\d]*\}/

Review comment:
       We need to configure `include_resp_body_expr` & `include_resp_body` to get the body.




-- 
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



[GitHub] [apisix] bisakhmondal commented on pull request #6113: feat: development of Loggly logging plugin

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#issuecomment-1013388198


   The current logger implementation
   ![image](https://user-images.githubusercontent.com/41498427/149570019-605dc736-372a-4b9a-8520-c1979004b276.png)
   


-- 
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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788365676



##########
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?
   Yes, similar to what we did to batchprocessor. Let's handle that in the next PR. 
   
   Good catch. Adding, Thanks




-- 
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



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

Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r788365104



##########
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:
       Yep. Sounds good




-- 
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



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

Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on a change in pull request #6113:
URL: https://github.com/apache/apisix/pull/6113#discussion_r790431430



##########
File path: apisix/plugins/loggly.lua
##########
@@ -0,0 +1,258 @@
+--
+-- 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 ngx = ngx
+local tostring = tostring
+local pairs = pairs
+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 severity_enums = {}
+do
+    for k, _ in pairs(severity) do
+        severity_enums[#severity_enums+1] = k
+        severity_enums[#severity_enums+1] = k:lower()
+    end
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        customer_token = {type = "string"},
+        severity = {
+            type = "string",
+            default = "INFO",
+            enum = severity_enums,
+            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",
+                -- we prevent of having `tag=` prefix
+                pattern = "^(?!tag=)[ -~]*",
+            },
+        },
+    },
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)

Review comment:
       OK, understood




-- 
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