Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): balancer span precision #10681

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ local instrumentation = require "kong.tracing.instrumentation"
local tablepool = require "tablepool"
local table_new = require "table.new"
local get_ctx_table = require("resty.core.ctx").get_ctx_table
local time_ns = require "kong.tools.utils".time_ns


local kong = kong
Expand Down Expand Up @@ -1039,6 +1040,7 @@ end
function Kong.balancer()
-- This may be called multiple times, and no yielding here!
local now_ms = now() * 1000
local now_ns = time_ns()

local ctx = ngx.ctx
if not ctx.KONG_BALANCER_START then
Expand Down Expand Up @@ -1079,6 +1081,7 @@ function Kong.balancer()
tries[try_count] = current_try

current_try.balancer_start = now_ms
current_try.balancer_start_ns = now_ns

if try_count > 1 then
-- only call balancer on retry, first one is done in `runloop.access.after`
Expand Down Expand Up @@ -1202,6 +1205,7 @@ function Kong.balancer()
-- record try-latency
local try_latency = ctx.KONG_BALANCER_ENDED_AT - current_try.balancer_start
current_try.balancer_latency = try_latency
current_try.balancer_latency_ns = time_ns() - current_try.balancer_start_ns

-- time spent in Kong before sending the request to upstream
-- start_time() is kept in seconds with millisecond resolution.
Expand Down
6 changes: 3 additions & 3 deletions kong/tracing/instrumentation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function _M.balancer(ctx)
local span_name = "balancer try #" .. i
local span_options = {
span_kind = 3, -- client
start_time_ns = try.balancer_start * 1e6,
start_time_ns = try.balancer_start_ns,
attributes = {
["net.peer.ip"] = try.ip,
["net.peer.port"] = try.port,
Expand All @@ -103,9 +103,9 @@ function _M.balancer(ctx)
span:set_status(2)
end

if try.balancer_latency ~= nil then
if try.balancer_latency_ns ~= nil then
local try_upstream_connect_time = (tonumber(upstream_connect_time[i], 10) or 0) * 1000
span:finish((try.balancer_start + try.balancer_latency + try_upstream_connect_time) * 1e6)
span:finish(try.balancer_start_ns + try.balancer_latency_ns + try_upstream_connect_time * 1e6)
else
span:finish()
end
Expand Down