From 41e7b28249eafa9ede2dac96bdb6f73e8d8482ee Mon Sep 17 00:00:00 2001 From: samugi Date: Tue, 25 Jun 2024 12:52:35 +0200 Subject: [PATCH] fixup! feat(observability): add OpenTelemetry logs --- kong/globalpatches.lua | 13 ++++++++++++- kong/observability/logs.lua | 12 +++--------- kong/plugins/opentelemetry/handler.lua | 2 +- kong/plugins/opentelemetry/logs.lua | 7 ------- kong/runloop/plugins_iterator.lua | 6 ++++++ 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 784a272006da..ada730c64b3c 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -593,7 +593,15 @@ return function(options) -- OTel-formatted logs feature local dynamic_hook = require "kong.dynamic_hook" + local log_called = false _G.ngx.log = function(...) + if log_called then + -- avoid recursive loops + -- relies on the patch to NOT yield + return old_ngx_log(...) + end + log_called = true + -- stack level = 5: -- 1: maybe_push -- 2: dynamic_hook.pcall @@ -601,9 +609,12 @@ return function(options) -- 4: patched function -- 5: caller dynamic_hook.run_hook("observability_logs", "push", 5, ...) - + log_called = false return old_ngx_log(...) end + -- export native ngx.log to be used where + -- the patched code must not be executed + _G.native_ngx_log = old_ngx_log if not options.cli and not options.rbusted then local timing = require "kong.timing" diff --git a/kong/observability/logs.lua b/kong/observability/logs.lua index d0b715c5e7d1..11a84f046208 100644 --- a/kong/observability/logs.lua +++ b/kong/observability/logs.lua @@ -104,18 +104,12 @@ end function _M.maybe_push(stack_level, log_level, ...) - -- !WARNING! no logging here, to avoid infinite recursion. - -- - -- Check if this log entry is eligible to go in the log buffer. + -- WARNING: do not yield in this function, as it is called from ngx.log + -- Early return cases: - -- no log line + -- no (or empty) log line local args = { ... } - if #args == 0 then - return - end - - -- empty log line local log_str = concat_tostring(args) if log_str == "" then return diff --git a/kong/plugins/opentelemetry/handler.lua b/kong/plugins/opentelemetry/handler.lua index 49d8b49cbffb..7d7d19adfd3d 100644 --- a/kong/plugins/opentelemetry/handler.lua +++ b/kong/plugins/opentelemetry/handler.lua @@ -17,7 +17,7 @@ function OpenTelemetryHandler:configure(configs) for _, config in ipairs(configs) do if config.logs_endpoint then dynamic_hook.hook("observability_logs", "push", o11y_logs.maybe_push) - dynamic_hook.always_enable("observability_logs") + dynamic_hook.enable_by_default("observability_logs") end end end diff --git a/kong/plugins/opentelemetry/logs.lua b/kong/plugins/opentelemetry/logs.lua index 0fc1e34d6afd..ac254326205e 100644 --- a/kong/plugins/opentelemetry/logs.lua +++ b/kong/plugins/opentelemetry/logs.lua @@ -1,10 +1,3 @@ --- This software is copyright Kong Inc. and its licensors. --- Use of the software is subject to the agreement between your organization --- and Kong Inc. If there is no such agreement, use is governed by and --- subject to the terms of the Kong Master Software License Agreement found --- at https://konghq.com/enterprisesoftwarelicense/. --- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ] - local Queue = require "kong.tools.queue" local o11y_logs = require "kong.observability.logs" local otlp = require "kong.plugins.opentelemetry.otlp" diff --git a/kong/runloop/plugins_iterator.lua b/kong/runloop/plugins_iterator.lua index 6fe558004035..78a6421011a6 100644 --- a/kong/runloop/plugins_iterator.lua +++ b/kong/runloop/plugins_iterator.lua @@ -1,6 +1,7 @@ local workspaces = require "kong.workspaces" local constants = require "kong.constants" local tablepool = require "tablepool" +local req_dyn_hook = require "kong.dynamic_hook" local kong = kong @@ -17,6 +18,7 @@ local fetch_table = tablepool.fetch local release_table = tablepool.release local uuid = require("kong.tools.uuid").uuid local get_updated_monotonic_ms = require("kong.tools.time").get_updated_monotonic_ms +local req_dyn_hook_disable_by_default = req_dyn_hook.disable_by_default local TTL_ZERO = { ttl = 0 } @@ -428,6 +430,10 @@ end local function configure(configurable, ctx) + -- Disable hooks that are selectively enabled by plugins + -- in their :configure handler + req_dyn_hook_disable_by_default("observability_logs") + ctx = ctx or ngx.ctx local kong_global = require "kong.global" for _, plugin in ipairs(CONFIGURABLE_PLUGINS) do