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



##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -36,23 +37,46 @@ local lrucache = core.lrucache.new({
 local metadata_schema = {
     type = "object",
     properties = {
-        host = schema_def.host_def,
-        port = {type = "integer", minimum = 0},
-        tls = {type = "boolean", default = false},
-        tls_server_name = {type = "string"},
-        timeout = {type = "integer", minimum = 1, default = 3},
-        keepalive = {type = "integer", minimum = 1, default = 30},
+        type = {type = "string", default = "TCP", enum = {"TCP", 
"SKYWALKING"}},
+        tcp = {
+            type = "object",
+            properties = {
+                host = schema_def.host_def,
+                port = {type = "integer", minimum = 0},
+                tls = {type = "boolean", default = false},
+                tls_server_name = {type = "string"},
+            },
+            required = {"host", "port"}
+        },
+        skywalking = {
+            type = "object",
+            properties = {
+                endpoint_addr = schema_def.uri,
+                service_name = {type = "string", default = "APISIX"},
+                service_instance_name = {type="string", default = "APISIX 
Service Instance"},
+            },
+            required = {"endpoint_addr"}
+        },
+        host = {schema_def.host_def, description = "Deprecated, use `tcp.host` 
instead."},
+        port = {type = "integer", minimum = 0, description = "Deprecated, use 
`tcp.port` instead."},
+        tls = {type = "boolean", default = false,
+                description = "Deprecated, use `tcp.tls` instead."},
+        tls_server_name = {type = "string",
+                description = "Deprecated, use `tcp.tls_server_name` 
instead."},
         name = {type = "string", default = plugin_name},
         level = {type = "string", default = "WARN", enum = {"STDERR", "EMERG", 
"ALERT", "CRIT",
                 "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"}},
+        timeout = {type = "integer", minimum = 1, default = 3},
+        keepalive = {type = "integer", minimum = 1, default = 30},
         batch_max_size = {type = "integer", minimum = 0, default = 1000},
         max_retry_count = {type = "integer", minimum = 0, default = 0},
         retry_delay = {type = "integer", minimum = 0, default = 1},
         buffer_duration = {type = "integer", minimum = 1, default = 60},
         inactive_timeout = {type = "integer", minimum = 1, default = 3},
     },
-    required = {"host", "port"}

Review comment:
       Now the outer requirement constraint should be either `tcp` or 
`skywalking`.

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -94,33 +126,86 @@ local function send_to_server(data)
 
     sock:settimeout(config.timeout * 1000)
 
-    local ok, err = sock:connect(config.host, config.port)
+    local tcp_config = config.tcp
+    local ok, err = sock:connect(tcp_config.host, tcp_config.port)
     if not ok then
-        return false, "failed to connect the TCP server: host[" .. config.host
-            .. "] port[" .. tostring(config.port) .. "] err: " .. err
+        return false, "failed to connect the TCP server: host[" .. 
tcp_config.host
+            .. "] port[" .. tostring(tcp_config.port) .. "] err: " .. err
     end
 
-    if config.tls then
-        ok, err = sock:sslhandshake(false, config.tls_server_name, false)
+    if tcp_config.tls then
+        ok, err = sock:sslhandshake(false, tcp_config.tls_server_name, false)
         if not ok then
             sock:close()
             return false, "failed to perform TLS handshake to TCP server: 
host["
-                .. config.host .. "] port[" .. tostring(config.port) .. "] 
err: " .. err
+                .. tcp_config.host .. "] port[" .. tostring(tcp_config.port) 
.. "] err: " .. err
         end
     end
 
     local bytes, err = sock:send(data)
     if not bytes then
         sock:close()
-        return false, "failed to send data to TCP server: host[" .. config.host
-            .. "] port[" .. tostring(config.port) .. "] err: " .. err
+        return false, "failed to send data to TCP server: host[" .. 
tcp_config.host
+            .. "] port[" .. tostring(tcp_config.port) .. "] err: " .. err
     end
 
     sock:setkeepalive(config.keepalive * 1000)
     return true
 end
 
 
+local function send_to_skywalking(log_message)
+    local err_msg
+    local res = true
+    core.log.info("sending a batch logs to ", config.skywalking.endpoint_addr)
+
+    local httpc = http.new()
+    httpc:set_timeout(config.timeout * 1000)
+
+    local entries = {}
+    for i = 1, #log_message, 2 do
+        local content = {
+            service = config.skywalking.service_name,
+            serviceInstance = config.skywalking.service_instance_name,
+            endpoint = "",
+            body = {
+                text = {
+                    text = log_message[i]
+                }
+           }
+        }
+        table.insert(entries, content)
+    end
+
+    local httpc_res, httpc_err = httpc:request_uri(
+        config.skywalking.endpoint_addr,
+        {
+            method = "POST",
+            body = core.json.encode(entries),
+            keepalive_timeout = config.keepalive * 1000,
+            headers = {
+                ["Content-Type"] = "application/json",
+            }
+        }
+    )
+
+    if not httpc_res then
+        return false, "error while sending data to skywalking["
+            .. config.skywalking.endpoint_addr .. "] " .. httpc_err
+    end
+
+    -- some error occurred in the server
+    if httpc_res.status >= 400 then
+        res =  false
+        err_msg = "server returned status code[" .. httpc_res.status

Review comment:
       Use `string.format` to make the message more readable?




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