Skip to content

Commit

Permalink
refa
Browse files Browse the repository at this point in the history
refa
  • Loading branch information
ychensha committed Aug 18, 2022
1 parent e1066b2 commit f4dd849
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
22 changes: 16 additions & 6 deletions apisix/plugins/tencent-cloud-cls/cls-sdk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
41 changes: 30 additions & 11 deletions t/plugin/tencent-cloud-cls.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f4dd849

Please sign in to comment.