Skip to content

Commit

Permalink
fixup! feat(observability): add OpenTelemetry logs
Browse files Browse the repository at this point in the history
  • Loading branch information
samugi committed Jun 25, 2024
1 parent f023ff4 commit 41e7b28
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
13 changes: 12 additions & 1 deletion kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,28 @@ 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
-- 3: dynamic_hook.run_hook
-- 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"
Expand Down
12 changes: 3 additions & 9 deletions kong/observability/logs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/opentelemetry/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions kong/plugins/opentelemetry/logs.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 6 additions & 0 deletions kong/runloop/plugins_iterator.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 41e7b28

Please sign in to comment.