This is an automated email from the ASF dual-hosted git repository.

wenming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 87cb2f8c0 fix(tencent-cloud-cls): DNS parsing failure (#9843)
87cb2f8c0 is described below

commit 87cb2f8c03069f1ae8fd3cd300d2966612927dcd
Author: Fucheng Jiang <[email protected]>
AuthorDate: Thu Aug 17 10:34:36 2023 +0800

    fix(tencent-cloud-cls): DNS parsing failure (#9843)
---
 apisix/plugins/tencent-cloud-cls/cls-sdk.lua | 23 +++++++++--
 t/plugin/tencent-cloud-cls.t                 | 58 ++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua 
b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua
index d2b6e8ad4..650d4ab0e 100644
--- a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua
+++ b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua
@@ -41,6 +41,7 @@ local type = type
 local tostring = tostring
 local setmetatable = setmetatable
 local pcall = pcall
+local unpack = unpack
 
 -- api doc https://www.tencentcloud.com/document/product/614/16873
 local MAX_SINGLE_VALUE_SIZE = 1 * 1024 * 1024
@@ -62,13 +63,20 @@ local params_cache = {
 local function get_ip(hostname)
     local _, resolved = socket.dns.toip(hostname)
     local ip_list = {}
-    for _, v in ipairs(resolved.ip) do
-        insert_tab(ip_list, v)
+    if not resolved.ip then
+        -- DNS parsing failure
+        local err = resolved
+        core.log.error("resolve ip failed, hostname: " .. hostname .. ", 
error: " .. err)
+        return nil, err
+    else
+        for _, v in ipairs(resolved.ip) do
+            insert_tab(ip_list, v)
+        end
     end
     return ip_list
 end
 
-local host_ip = tostring(unpack(get_ip(core_gethostname())))
+local host_ip
 local log_group_list = {}
 local log_group_list_pb = {
     logGroupList = log_group_list,
@@ -273,6 +281,15 @@ function _M.send_to_cls(self, logs)
     -- sums of all value in all LogGroup should be no more than 5MB
     -- so send whenever size exceed max size
     local group_list_start = 1
+
+    if not host_ip then
+        local host_ip_list, err = get_ip(core_gethostname())
+        if not host_ip_list then
+            return false, err
+        end
+        host_ip = tostring(unpack(host_ip_list))
+    end
+
     for i = 1, #logs, 1 do
         local contents, log_size = normalize_log(logs[i])
         if log_size > MAX_LOG_GROUP_VALUE_SIZE then
diff --git a/t/plugin/tencent-cloud-cls.t b/t/plugin/tencent-cloud-cls.t
index fff3bc129..6ad4d585b 100644
--- a/t/plugin/tencent-cloud-cls.t
+++ b/t/plugin/tencent-cloud-cls.t
@@ -500,3 +500,61 @@ GET /opentracing
 --- response_body
 opentracing
 --- wait: 0.5
+
+
+
+=== TEST 15: add plugin
+--- 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": {
+                            "tencent-cloud-cls": {
+                                "cls_host": "127.0.0.1:10420",
+                                "cls_topic": 
"143b5d70-139b-4aec-b54e-bb97756916de",
+                                "secret_id": "secret_id",
+                                "secret_key": "secret_key",
+                                "batch_max_size": 1,
+                                "max_retry_count": 1,
+                                "retry_delay": 2,
+                                "buffer_duration": 2,
+                                "inactive_timeout": 2
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 16: test resolvt e ip failed
+--- extra_init_by_lua
+    local socket = require("socket")
+    socket.dns.toip = function(address)
+        return nil, "address can't be resolved"
+    end
+--- request
+GET /opentracing
+--- response_body
+opentracing
+--- error_log eval
+qr/resolve ip failed, hostname: .*, error: address can't be resolved/
+--- wait: 0.5

Reply via email to