From 2a056078dd15422443714c3c57df2d06d71e8729 Mon Sep 17 00:00:00 2001 From: samugi Date: Sat, 15 Apr 2023 21:24:45 +0200 Subject: [PATCH] fix(tracing): balancer span precision use nanoseconds to track balancer span's start and end times --- kong/init.lua | 4 ++++ kong/tracing/instrumentation.lua | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kong/init.lua b/kong/init.lua index 818423ebb85..b9e7c6b5c1c 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -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 @@ -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 @@ -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` @@ -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. diff --git a/kong/tracing/instrumentation.lua b/kong/tracing/instrumentation.lua index 8f194a59352..3f10f496b6d 100644 --- a/kong/tracing/instrumentation.lua +++ b/kong/tracing/instrumentation.lua @@ -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, @@ -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