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



##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -134,6 +219,14 @@ local function update_filter(value)
 end
 
 
+local function send(data)
+    if config.type == "TCP" then

Review comment:
       Better to use config.tcp to decide the destination, so that we don't 
need to check the `config.tcp` is nil or not.

##########
File path: t/plugin/error-log-skywalking-logger.t
##########
@@ -0,0 +1,357 @@
+#
+# 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';
+
+log_level('debug');
+repeat_each(1);
+no_long_string();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: test schema checker
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        endpoint_addr = "http://127.0.0.1";
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: test schema checker, missing required field
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        service = "APISIX"
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+property "skywalking" validation failed: property "endpoint_addr" is required
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: not enable the plugin

Review comment:
       TEST 3~5,9,10 are redundant as t/plugin/error-log-logger.t already 
checked

##########
File path: t/plugin/error-log-skywalking-logger.t
##########
@@ -0,0 +1,357 @@
+#
+# 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';
+
+log_level('debug');
+repeat_each(1);
+no_long_string();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: test schema checker
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        endpoint_addr = "http://127.0.0.1";
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: test schema checker, missing required field
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        service = "APISIX"
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+property "skywalking" validation failed: property "endpoint_addr" is required
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: not enable the plugin
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+error-log-logger
+--- wait: 2
+
+
+
+=== TEST 4: enable the plugin, but not init the metadata
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 5: set a wrong metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "port": 1999,
+                    "inactive_timeout": 1
+                }]]
+                )
+
+            -- ensure the request is rejected even this plugin doesn't
+            -- have check_schema method
+            ngx.status = code
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- error_code: 201
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 6: test unreachable server
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1988/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+            ngx.sleep(2)
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[lua\] batch-processor.lua:63: Batch Processor\[error-log-logger\] 
failed to process entries: error while sending data to 
skywalking\[http:\/\/127.0.0.1:1988\/log\] connection refused, context: 
ngx.timer/
+--- wait: 3
+
+
+
+=== TEST 7: put plugin metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1982/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: log an error level message
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            ngx.sleep(2)
+            core.log.error("this is an error message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"APISIX
 Service Instance\".*/
+--- wait: 15

Review comment:
       Do we need to wait so long as the error report is different from the 
skywalking tracer report?

##########
File path: t/plugin/error-log-skywalking-logger.t
##########
@@ -0,0 +1,357 @@
+#
+# 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';
+
+log_level('debug');
+repeat_each(1);
+no_long_string();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: test schema checker
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        endpoint_addr = "http://127.0.0.1";
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: test schema checker, missing required field
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        service = "APISIX"
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+property "skywalking" validation failed: property "endpoint_addr" is required
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: not enable the plugin
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+error-log-logger
+--- wait: 2
+
+
+
+=== TEST 4: enable the plugin, but not init the metadata
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 5: set a wrong metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "port": 1999,
+                    "inactive_timeout": 1
+                }]]
+                )
+
+            -- ensure the request is rejected even this plugin doesn't
+            -- have check_schema method
+            ngx.status = code
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- error_code: 201
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 6: test unreachable server
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1988/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+            ngx.sleep(2)
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[lua\] batch-processor.lua:63: Batch Processor\[error-log-logger\] 
failed to process entries: error while sending data to 
skywalking\[http:\/\/127.0.0.1:1988\/log\] connection refused, context: 
ngx.timer/
+--- wait: 3
+
+
+
+=== TEST 7: put plugin metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1982/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: log an error level message
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            ngx.sleep(2)
+            core.log.error("this is an error message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"APISIX
 Service Instance\".*/

Review comment:
       Need to check the error log in the text.

##########
File path: t/plugin/error-log-skywalking-logger.t
##########
@@ -0,0 +1,357 @@
+#
+# 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';
+
+log_level('debug');
+repeat_each(1);
+no_long_string();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: test schema checker
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        endpoint_addr = "http://127.0.0.1";
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: test schema checker, missing required field
+--- config
+    location /t {
+        content_by_lua_block {
+        local core = require("apisix.core")
+            local plugin = require("apisix.plugins.error-log-logger")
+            local ok, err = plugin.check_schema(
+                {
+                    type = "SKYWALKING",
+                    skywalking = {
+                        service = "APISIX"
+                    }
+                },
+                core.schema.TYPE_METADATA
+            )
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+property "skywalking" validation failed: property "endpoint_addr" is required
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: not enable the plugin
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+error-log-logger
+--- wait: 2
+
+
+
+=== TEST 4: enable the plugin, but not init the metadata
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 5: set a wrong metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "port": 1999,
+                    "inactive_timeout": 1
+                }]]
+                )
+
+            -- ensure the request is rejected even this plugin doesn't
+            -- have check_schema method
+            ngx.status = code
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- error_code: 201
+--- response_body
+--- error_log eval
+qr/please set the correct plugin_metadata for error-log-logger/
+--- wait: 2
+
+
+
+=== TEST 6: test unreachable server
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1988/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+            ngx.sleep(2)
+            core.log.warn("this is a warning message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[lua\] batch-processor.lua:63: Batch Processor\[error-log-logger\] 
failed to process entries: error while sending data to 
skywalking\[http:\/\/127.0.0.1:1988\/log\] connection refused, context: 
ngx.timer/
+--- wait: 3
+
+
+
+=== TEST 7: put plugin metadata
+--- yaml_config
+apisix:
+    enable_admin: true
+    admin_key: null
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = 
t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "type": "SKYWALKING",
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1982/log";
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: log an error level message
+--- yaml_config
+plugins:
+  - error-log-logger
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            ngx.sleep(2)
+            core.log.error("this is an error message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"APISIX
 Service Instance\".*/

Review comment:
       Also, need a case that multiple error logs are captured and sent.

##########
File path: t/plugin/error-log-skywalking-logger.t
##########
@@ -0,0 +1,357 @@
+#

Review comment:
       Should use `t/plugin/error-log-logger-skywalking.t`, the prefix is the 
plugin name.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to