Skip to content

Commit

Permalink
change random
Browse files Browse the repository at this point in the history
  • Loading branch information
ychensha committed Aug 5, 2022
1 parent e969b5e commit b9ee01c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
24 changes: 13 additions & 11 deletions apisix/plugins/tencent-cloud-cls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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" },
Expand Down Expand Up @@ -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


Expand All @@ -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)
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions apisix/plugins/tencent-cloud-cls/cls-sdk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())))
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9ee01c

Please sign in to comment.