diff --git a/apisix/plugins/tencent-cloud-cls.lua b/apisix/plugins/tencent-cloud-cls.lua index e2a69aa38b2e..2a6190b49e4f 100644 --- a/apisix/plugins/tencent-cloud-cls.lua +++ b/apisix/plugins/tencent-cloud-cls.lua @@ -19,8 +19,7 @@ local core = require("apisix.core") local log_util = require("apisix.utils.log-util") local bp_manager_mod = require("apisix.utils.batch-processor-manager") local cls_sdk = require("apisix.plugins.tencent-cloud-cls.cls-sdk") -local random = math.random -math.randomseed(ngx.time() + ngx.worker.pid()) +local math = math local ngx = ngx local pairs = pairs @@ -35,7 +34,12 @@ local schema = { -- https://console.cloud.tencent.com/capi secret_id = { type = "string" }, secret_key = { type = "string" }, - sample_rate = { type = "integer", minimum = 1, maximum = 100, default = 100 }, + sample_ratio = { + type = "number", + minimum = 0.00001, + maximum = 1, + default = 1 + }, include_req_body = { type = "boolean", default = false }, include_resp_body = { type = "boolean", default = false }, global_tag = { type = "object" }, @@ -76,11 +80,13 @@ end function _M.access(conf, ctx) -- sample if set - if conf.sample_rate < 100 and random(1, 100) > conf.sample_rate then - core.log.debug("not sampled") + ctx.cls_sample = false + if conf.sample_ratio == 1 or math.random() < conf.sample_ratio then + core.log.debug("cls sampled") + ctx.cls_sample = true return end - ctx.cls_sample = true + core.log.debug("cls not sampled") end @@ -94,7 +100,7 @@ end function _M.log(conf, ctx) -- sample if set if not ctx.cls_sample then - core.log.debug("not sampled") + core.log.debug("cls not sampled, skip log") return end local metadata = plugin.plugin_metadata(plugin_name) @@ -110,10 +116,6 @@ function _M.log(conf, ctx) entry = log_util.get_full_log(ngx, conf) end - if not entry.route_id then - entry.route_id = "no-matched" - end - if conf.global_tag then for k, v in pairs(conf.global_tag) do entry[k] = v diff --git a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua index ab4f0d40c558..2a53abcefde6 100644 --- a/apisix/plugins/tencent-cloud-cls/cls-sdk.lua +++ b/apisix/plugins/tencent-cloud-cls/cls-sdk.lua @@ -25,13 +25,11 @@ local core = require("apisix.core") local core_gethostname = require("apisix.core.utils").gethostname local json = core.json local json_encode = json.encode - local ngx = ngx local ngx_time = ngx.time local ngx_now = ngx.now local ngx_sha1_bin = ngx.sha1_bin local ngx_hmac_sha1 = ngx.hmac_sha1 - local fmt = string.format local table = table local concat_tab = table.concat @@ -58,13 +56,14 @@ local params_cache = { headers = headers_cache, } + local function get_ip(hostname) local _, resolved = socket.dns.toip(hostname) - local ListTab = {} + local ip_list = {} for _, v in ipairs(resolved.ip) do - insert_tab(ListTab, v) + insert_tab(ip_list, v) end - return ListTab + return ip_list end local host_ip = tostring(unpack(get_ip(core_gethostname()))) @@ -73,14 +72,17 @@ local log_group_list_pb = { logGroupList = log_group_list, } + local function sha1(msg) return str_util.to_hex(ngx_sha1_bin(msg)) end + local function sha1_hmac(key, msg) return str_util.to_hex(ngx_hmac_sha1(key, msg)) end + -- sign algorithm https://cloud.tencent.com/document/product/614/12445 local function sign(secret_id, secret_key) local method = "post" @@ -109,6 +111,7 @@ local function sign(secret_id, secret_key) return concat_tab(arr, '&') end + local function send_cls_request(host, topic, secret_id, secret_key, pb_data) local http_new = http:new() http_new:set_timeouts(cls_conn_timeout, cls_send_timeout, cls_read_timeout) @@ -146,6 +149,7 @@ local function send_cls_request(host, topic, secret_id, secret_key, pb_data) return true end + -- normalized log data for CLS API local function normalize_log(log) local normalized_log = {} @@ -173,6 +177,7 @@ local function normalize_log(log) return normalized_log, log_size end + local function send_to_cls(secret_id, secret_key, host, topic_id, logs) clear_tab(log_group_list) local now = ngx_now() * 1000