From f4dd8494b7909cc14a58241ecb7c07f858eea758 Mon Sep 17 00:00:00 2001 From: xinchenyuan Date: Thu, 18 Aug 2022 20:34:13 +0800 Subject: [PATCH] refa refa --- apisix/plugins/tencent-cloud-cls/cls-sdk.lua | 22 ++++++++--- t/plugin/tencent-cloud-cls.t | 41 ++++++++++++++------ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua index a0b809a3e033..a2be95cc60e6 100644 --- a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua +++ b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua @@ -147,7 +147,7 @@ local mt = { __index = _M } local pb_state local function init_pb_state() - pb.state(nil) + local old_pb_state = pb.state(nil) protoc.reload() local cls_sdk_protoc = protoc.new() -- proto file in https://www.tencentcloud.com/document/product/614/42787 @@ -187,9 +187,10 @@ message LogGroupList ]], "tencent-cloud-cls/cls.proto") if not ok then cls_sdk_protoc:reset() + pb.state(old_pb_state) return "failed to load cls.proto: ".. err end - pb_state = pb.state(nil) + pb_state = pb.state(old_pb_state) end @@ -210,7 +211,19 @@ function _M.new(host, topic, secret_id, secret_key) end +local function do_request_uri(uri, params) + local client = http:new() + client:set_timeouts(cls_conn_timeout, cls_send_timeout, cls_read_timeout) + local res, err = client:request_uri(uri, params) + client:close() + return res, err +end + + function _M.send_cls_request(self, pb_obj) + -- recovery of stored pb_store + pb.state(pb_state) + local ok, pb_data = pcall(pb.encode, "cls.LogGroupList", pb_obj) if not ok or not pb_data then core.log.error("failed to encode LogGroupList, err: ", pb_data) @@ -232,7 +245,7 @@ function _M.send_cls_request(self, pb_obj) local cls_url = "http://" .. self.host .. cls_api_path .. "?topic_id=" .. self.topic core.log.debug("CLS request URL: ", cls_url) - local res, err = client:request_uri(cls_url, params_cache) + local res, err = do_request_uri(cls_url, params_cache) if not res then return false, err end @@ -258,9 +271,6 @@ function _M.send_to_cls(self, logs) clear_tab(log_group_list) local now = ngx_now() * 1000 - -- recovery of stored pb_store - pb.state(pb_state) - local total_size = 0 local format_logs = new_tab(#logs, 0) -- sums of all value in all LogGroup should be no more than 5MB diff --git a/t/plugin/tencent-cloud-cls.t b/t/plugin/tencent-cloud-cls.t index 26e478f83d57..0d6d2c82aba5 100644 --- a/t/plugin/tencent-cloud-cls.t +++ b/t/plugin/tencent-cloud-cls.t @@ -271,20 +271,39 @@ Batch Processor[tencent-cloud-cls] successfully processed the entries --- wait: 0.5 -=== TEST 7: verify request ---- extra_init_by_lua - local cls = require("apisix.plugins.tencent-cloud-cls.cls-sdk") - cls.send_to_cls = function(self, logs) - if (#logs ~= 1) then - ngx.log(ngx.ERR, "unexpected logs length: ", #logs) - return - end - return true - end +=== TEST 9: plugin metadata +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.tencent-cloud-cls") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/tencent-cloud-cls', + ngx.HTTP_PUT, + [[{ + "log_format": { + "host": "$host", + "@timestamp": "$time_iso8601", + "client_ip": "$remote_addr" + } + }]] + ) + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 10: log use log_format --- request GET /opentracing --- response_body opentracing --- error_log -Batch Processor[tencent-cloud-cls] successfully processed the entries +"@timestamp":[true,"time_iso8601"] --- wait: 0.5