diff --git a/changelog/unreleased/kong/perf-tracing-from-timers.yml b/changelog/unreleased/kong/perf-tracing-from-timers.yml new file mode 100644 index 00000000000..bc081ed674b --- /dev/null +++ b/changelog/unreleased/kong/perf-tracing-from-timers.yml @@ -0,0 +1,3 @@ +message: "Performance optimization to avoid unnecessary creations and garbage-collections of spans" +type: "performance" +scope: "PDK" diff --git a/kong/pdk/tracing.lua b/kong/pdk/tracing.lua index ef9d81e0db9..6337e1fddc0 100644 --- a/kong/pdk/tracing.lua +++ b/kong/pdk/tracing.lua @@ -9,7 +9,6 @@ local require = require local ffi = require "ffi" local tablepool = require "tablepool" local new_tab = require "table.new" -local base = require "resty.core.base" local utils = require "kong.tools.utils" local phase_checker = require "kong.pdk.private.phases" @@ -421,6 +420,15 @@ noop_tracer.set_active_span = NOOP noop_tracer.process_span = NOOP noop_tracer.set_should_sample = NOOP +local VALID_TRACING_PHASES = { + rewrite = true, + access = true, + header_filter = true, + body_filter = true, + log = true, + content = true, +} + --- New Tracer local function new_tracer(name, options) name = name or "default" @@ -450,7 +458,7 @@ local function new_tracer(name, options) -- @phases rewrite, access, header_filter, response, body_filter, log, admin_api -- @treturn table span function self.active_span() - if not base.get_request() then + if not VALID_TRACING_PHASES[ngx.get_phase()] then return end @@ -463,7 +471,7 @@ local function new_tracer(name, options) -- @phases rewrite, access, header_filter, response, body_filter, log, admin_api -- @tparam table span function self.set_active_span(span) - if not base.get_request() then + if not VALID_TRACING_PHASES[ngx.get_phase()] then return end @@ -482,7 +490,7 @@ local function new_tracer(name, options) -- @tparam table options TODO(mayo) -- @treturn table span function self.start_span(...) - if not base.get_request() then + if not VALID_TRACING_PHASES[ngx.get_phase()] then return noop_span end diff --git a/spec/01-unit/26-tracing/01-tracer_pdk_spec.lua b/spec/01-unit/26-tracing/01-tracer_pdk_spec.lua index 285c980adf8..2cd05a72a0f 100644 --- a/spec/01-unit/26-tracing/01-tracer_pdk_spec.lua +++ b/spec/01-unit/26-tracing/01-tracer_pdk_spec.lua @@ -49,7 +49,7 @@ end local unhook_log_spy = debug.sethook describe("Tracer PDK", function() - local ok, err, _ + local ok, err, old_ngx_get_phase, _ local log_spy lazy_setup(function() @@ -57,9 +57,15 @@ describe("Tracer PDK", function() _G.kong = kong_global.new() kong_global.init_pdk(kong) log_spy = hook_log_spy() + old_ngx_get_phase = ngx.get_phase + -- trick the pdk into thinking we are not in the timer context + _G.ngx.get_phase = function() return "access" end -- luacheck: ignore end) - lazy_teardown(unhook_log_spy) + lazy_teardown(function() + unhook_log_spy() + _G.ngx.get_phase = old_ngx_get_phase -- luacheck: ignore + end) describe("initialize tracer", function() diff --git a/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua b/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua index eead16142b2..754743ffe60 100644 --- a/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua +++ b/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua @@ -44,16 +44,22 @@ local pb_decode_span = function(data) end describe("Plugin: opentelemetry (otlp)", function() + local old_ngx_get_phase + lazy_setup(function () -- overwrite for testing pb.option("enum_as_value") pb.option("auto_default_values") + old_ngx_get_phase = ngx.get_phase + -- trick the pdk into thinking we are not in the timer context + _G.ngx.get_phase = function() return "access" end -- luacheck: ignore end) lazy_teardown(function() -- revert it back pb.option("enum_as_name") pb.option("no_default_values") + _G.ngx.get_phase = old_ngx_get_phase -- luacheck: ignore end) after_each(function ()