Skip to content

Commit

Permalink
refactor(pdk/log): removal and refactor of redundant code (#10259)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluveto authored Feb 10, 2023
1 parent d9bdd82 commit 4205881
Showing 1 changed file with 58 additions and 61 deletions.
119 changes: 58 additions & 61 deletions kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local type = type
local find = string.find
local select = select
local concat = table.concat
local insert = table.insert
local getinfo = debug.getinfo
local reverse = string.reverse
local tostring = tostring
Expand Down Expand Up @@ -109,13 +110,13 @@ local function parse_modifiers(format)
if mod then
if mod.message then
buf.message_idxs = buf.message_idxs or {}
table.insert(buf.message_idxs, i)
insert(buf.message_idxs, i)

else
buf.debug_flags = (buf.debug_flags or "") .. mod.flag

buf.modifiers = buf.modifiers or {}
table.insert(buf.modifiers, {
insert(buf.modifiers, {
idx = i,
info = mod.info,
info_key = mod.info_key,
Expand Down Expand Up @@ -272,6 +273,9 @@ local serializers = {
-- kong.log.err("something failed: ", err)
-- kong.log.alert("something requires immediate action")
local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
local get_sys_filter_level = errlog.get_sys_filter_level
local get_phase = ngx.get_phase

to_string = to_string or tostring
stack_level = stack_level or 2

Expand All @@ -280,10 +284,10 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
return function(...)
local sys_log_level = nil

if not sys_log_level and ngx.get_phase() ~= "init" then
if get_phase() ~= "init" then
-- only grab sys_log_level after init_by_lua, where it is
-- hard-coded
sys_log_level = errlog.get_sys_filter_level()
sys_log_level = get_sys_filter_level()
end

if sys_log_level and lvl_const > sys_log_level then
Expand Down Expand Up @@ -339,11 +343,11 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
errlog.raw_log(lvl_const, header)

while i <= fullmsg_len do
local part = string.sub(fullmsg, i, i + WRAP - 1)
local part = sub(fullmsg, i, i + WRAP - 1)
local nl = part:match("()\n")

if nl then
part = string.sub(fullmsg, i, i + nl - 2)
part = sub(fullmsg, i, i + nl - 2)
i = i + nl

else
Expand Down Expand Up @@ -604,7 +608,7 @@ local function set_serialize_value(key, value, options)
error("mode must be 'set', 'add' or 'replace'", 2)
end

local ongx = (options or {}).ngx or ngx
local ongx = options.ngx or ngx
local ctx = ongx.ctx
ctx.serialize_values = ctx.serialize_values or get_default_serialize_values()
ctx.serialize_values[#ctx.serialize_values + 1] =
Expand Down Expand Up @@ -690,6 +694,30 @@ do
return root
end

local function build_authenticated_entity(ctx)
local authenticated_entity
if ctx.authenticated_credential ~= nil then
authenticated_entity = {
id = ctx.authenticated_credential.id,
consumer_id = ctx.authenticated_credential.consumer_id,
}
end

return authenticated_entity
end

local function build_tls_info(var, override)
local tls_info
local tls_info_ver = ngx_ssl.get_tls1_version_str()
if tls_info_ver then
tls_info = {
version = tls_info_ver,
cipher = var.ssl_cipher,
client_verify = override or var.ssl_client_verify,
}
end
return tls_info
end

---
-- Generates a table with useful information for logging.
Expand Down Expand Up @@ -741,34 +769,18 @@ do
-- @treturn table the request information table
-- @usage
-- kong.log.serialize()

if ngx.config.subsystem == "http" then
function serialize(options)
check_phase(PHASES_LOG)

local ongx = (options or {}).ngx or ngx
local okong = (options or {}).kong or kong
options = options or {}
local ongx = options.ngx or ngx
local okong = options.kong or kong

local ctx = ongx.ctx
local var = ongx.var

local authenticated_entity
if ctx.authenticated_credential ~= nil then
authenticated_entity = {
id = ctx.authenticated_credential.id,
consumer_id = ctx.authenticated_credential.consumer_id
}
end

local request_tls
local request_tls_ver = ngx_ssl.get_tls1_version_str()
if request_tls_ver then
request_tls = {
version = request_tls_ver,
cipher = var.ssl_cipher,
client_verify = ctx.CLIENT_VERIFY_OVERRIDE or var.ssl_client_verify,
}
end

local request_uri = var.request_uri or ""

local host_port = ctx.host_port or var.server_port
Expand All @@ -791,72 +803,56 @@ do
upstream_uri = upstream_uri .. "?" .. (var.args or "")
end
end

return edit_result(ctx, {
local root = {
request = {
uri = request_uri,
url = var.scheme .. "://" .. var.host .. ":" .. host_port .. request_uri,
querystring = okong.request.get_query(), -- parameters, as a table
method = okong.request.get_method(), -- http method
headers = okong.request.get_headers(),
size = request_size,
tls = request_tls
tls = build_tls_info(var, ctx.CLIENT_VERIFY_OVERRIDE),
},
upstream_uri = upstream_uri,
response = {
status = ongx.status,
headers = ongx.resp.get_headers(),
size = response_size,
},
tries = (ctx.balancer_data or {}).tries,
latencies = {
kong = (ctx.KONG_PROXY_LATENCY or ctx.KONG_RESPONSE_LATENCY or 0) +
(ctx.KONG_RECEIVE_TIME or 0),
proxy = ctx.KONG_WAITING_TIME or -1,
request = var.request_time * 1000
},
authenticated_entity = authenticated_entity,
tries = (ctx.balancer_data or {}).tries,
authenticated_entity = build_authenticated_entity(ctx),
route = ctx.route,
service = ctx.service,
consumer = ctx.authenticated_consumer,
client_ip = var.remote_addr,
started_at = okong.request.get_start_time(),
})
}

return edit_result(ctx, root)
end

else
function serialize(options)
check_phase(PHASES_LOG)

local ongx = (options or {}).ngx or ngx
local okong = (options or {}).kong or kong

options = options or {}
local ongx = options.ngx or ngx
local okong = options.kong or kong

local ctx = ongx.ctx
local var = ongx.var

local authenticated_entity
if ctx.authenticated_credential ~= nil then
authenticated_entity = {
id = ctx.authenticated_credential.id,
consumer_id = ctx.authenticated_credential.consumer_id
}
end

local session_tls
local session_tls_ver = ngx_ssl.get_tls1_version_str()
if session_tls_ver then
session_tls = {
version = session_tls_ver,
cipher = var.ssl_cipher,
client_verify = ctx.CLIENT_VERIFY_OVERRIDE or var.ssl_client_verify,
}
end


local host_port = ctx.host_port or var.server_port

return edit_result(ctx, {
local root = {
session = {
tls = session_tls,
tls = build_tls_info(var, ctx.CLIENT_VERIFY_OVERRIDE),
received = tonumber(var.bytes_received, 10),
sent = tonumber(var.bytes_sent, 10),
status = ongx.status,
Expand All @@ -866,18 +862,19 @@ do
received = tonumber(var.upstream_bytes_received, 10),
sent = tonumber(var.upstream_bytes_sent, 10),
},
tries = (ctx.balancer_data or {}).tries,
latencies = {
kong = ctx.KONG_PROXY_LATENCY or ctx.KONG_RESPONSE_LATENCY or 0,
session = var.session_time * 1000,
},
authenticated_entity = authenticated_entity,
tries = (ctx.balancer_data or {}).tries,
authenticated_entity = build_authenticated_entity(ctx),
route = ctx.route,
service = ctx.service,
consumer = ctx.authenticated_consumer,
client_ip = var.remote_addr,
started_at = okong.request.get_start_time(),
})
}
return edit_result(ctx, root)
end
end
end
Expand Down

1 comment on commit 4205881

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:4205881b933b23f01538a92ff8fec7bf328bdf6e
Artifacts available https://github.com/Kong/kong/actions/runs/4141768752

Please sign in to comment.