Skip to content

Commit

Permalink
chore(*): new dns client consistent with EE
Browse files Browse the repository at this point in the history
Cherry-picks the changes to the new dns client that bring
Active Tracing support in EE.

(cherry picked from commit 39d381b0a9523ffb2fda2cdea315f1ee47a801bf)
  • Loading branch information
samugi authored and bungle committed Jan 28, 2025
1 parent 8e441df commit 71e7019
Showing 1 changed file with 71 additions and 6 deletions.
77 changes: 71 additions & 6 deletions kong/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,73 @@ local function process_answers(self, qname, qtype, answers)
end


local function set_resolver_for_curr_try(tries, server)
if not server or not tries then
return
end

-- server is a table with { host, port? }
local server_str = #server == 1 and server[1] or server[1] .. ":" .. server[2]
tries[#tries].resolver = server_str
end


local function set_for_curr_try(tries, key, value)
local current_try = tries[#tries]
if not current_try then
return
end

current_try[key] = value
end


local function add_for_curr_try(tries, key, value)
local current_try = tries[#tries]
if not current_try then
return
end

if not current_try[key] then
current_try[key] = {}
end

table_insert(current_try[key], value)
end


local function resolve_query(self, name, qtype, tries)
local key = name .. ":" .. qtype

local stats = self.stats

stats:incr(key, "query")
-- initialize try
table_insert(tries, {
qname = name,
qtype = qtype,
cache_hit = false,
})

local r, err = resolver:new(self.r_opts)
if not r then
return nil, "failed to instantiate the resolver: " .. err
end

-- set resolver address for current try
if r.servers and r.cur then
local current = r.cur == 1 and #r.servers or r.cur

-- validate undocumented fields
if type(current) ~= "number" and type(r.servers) ~= "table" then
log(ERR, PREFIX, "cannot read resolver info")

else
local current_server = r.servers[current]
set_resolver_for_curr_try(tries, current_server)
end
end

local start = now()

local answers, err = r:query(name, { qtype = qtype })
Expand All @@ -369,7 +424,7 @@ local function resolve_query(self, name, qtype, tries)

-- TODO: make the error more structured, like:
-- { qname = name, qtype = qtype, error = err, } or something similar
table_insert(tries, { name .. ":" .. TYPE_TO_NAME[qtype], err })
add_for_curr_try(tries, "error", err)

return nil, err
end
Expand All @@ -385,7 +440,8 @@ local function resolve_query(self, name, qtype, tries)
err = ("dns %s error: %s %s"):format(
answers.errcode < CACHE_ONLY_ERROR_CODE and "server" or "client",
answers.errcode, answers.errstr)
table_insert(tries, { name .. ":" .. TYPE_TO_NAME[qtype], err })
add_for_curr_try(tries, "error", err)
set_for_curr_try(tries, "errcode", answers.errcode)
end

return answers
Expand Down Expand Up @@ -537,7 +593,7 @@ local function resolve_callback(self, name, qtype, cache_only, tries)
end


local function resolve_all(self, name, qtype, cache_only, tries, has_timing)
function _M:resolve_all(name, qtype, cache_only, tries, has_timing)
name = string_lower(name)
tries = setmetatable(tries or {}, _TRIES_MT)

Expand All @@ -563,6 +619,15 @@ local function resolve_all(self, name, qtype, cache_only, tries, has_timing)

local hit_str = hit_level and HIT_LEVEL_TO_NAME[hit_level] or "fail"
stats:incr(key, hit_str)
if #tries == 0 then
-- initialize try
table_insert(tries, {
qname = name,
qtype = qtype,
cache_hit = true,
resolver = "cache",
})
end

log(DEBUG, PREFIX, "cache lookup ", key, " ans:", answers and #answers or "-",
" hlv:", hit_str)
Expand All @@ -584,21 +649,21 @@ end


function _M:resolve(name, qtype, cache_only, tries)
return resolve_all(self, name, qtype, cache_only, tries,
return self:resolve_all(name, qtype, cache_only, tries,
ngx.ctx and ngx.ctx.has_timing)
end


function _M:resolve_address(name, port, cache_only, tries)
local has_timing = ngx.ctx and ngx.ctx.has_timing

local answers, err, tries = resolve_all(self, name, nil, cache_only, tries,
local answers, err, tries = self:resolve_all(name, nil, cache_only, tries,
has_timing)

if answers and answers[1] and answers[1].type == TYPE_SRV then
local answer = get_next_weighted_round_robin_answer(answers)
port = answer.port ~= 0 and answer.port or port
answers, err, tries = resolve_all(self, answer.target, TYPE_A_OR_AAAA,
answers, err, tries = self:resolve_all(answer.target, TYPE_A_OR_AAAA,
cache_only, tries, has_timing)
end

Expand Down

1 comment on commit 71e7019

@github-actions
Copy link
Contributor

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-dev:71e701960098b5ac655b82e688ed5f43cb1b3fd3
Artifacts available https://github.com/Kong/kong/actions/runs/13016895898

Please sign in to comment.