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 Jul 3, 2024
1 parent 9b5d65b commit ad0b93c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
11 changes: 5 additions & 6 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -593,24 +593,23 @@ return function(options)

-- OTel-formatted logs feature
local dynamic_hook = require "kong.dynamic_hook"
local log_called = false
local hook_called = false
_G.ngx.log = function(...)
if log_called then
-- avoid recursive loops
-- relies on the patch to NOT yield
if hook_called then
-- avoid recursive loops: relies on the hook handler to NOT yield
old_ngx_log(ngx.ERR, debug.traceback("concurrent execution detected for: ngx.log", 2))
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
hook_called = true
dynamic_hook.run_hook("observability_logs", "push", 5, ...)
log_called = false
hook_called = false
return old_ngx_log(...)
end
-- export native ngx.log to be used where
Expand Down
26 changes: 13 additions & 13 deletions kong/observability/logs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ local function generate_log_entry(request_scoped, log_level, log_str, request_id
end

local attributes = {
request_id = request_id,
introspection_current_line = debug_info.currentline,
introspection_name = debug_info.name,
introspection_namewhat = debug_info.namewhat,
introspection_source = debug_info.source,
introspection_what = debug_info.what,
["request.id"] = request_id,
["introspection.current.line"] = debug_info.currentline,
["introspection.name"] = debug_info.name,
["introspection.namewhat"] = debug_info.namewhat,
["introspection.source"] = debug_info.source,
["introspection.what"] = debug_info.what,
}

local now_ns = time_ns()
Expand Down Expand Up @@ -110,13 +110,6 @@ function _M.maybe_push(stack_level, log_level, ...)
return
end

-- no (or empty) log line
local args = { ... }
local log_str = concat_tostring(args)
if log_str == "" then
return
end

local log_buffer, max_logs
local request_id = request_id_get()
local request_scoped = request_id ~= nil
Expand All @@ -136,6 +129,13 @@ function _M.maybe_push(stack_level, log_level, ...)
return
end

-- no (or empty) log line
local args = { ... }
local log_str = concat_tostring(args)
if log_str == "" then
return
end

-- generate & push log entry
local debug_info = debug.getinfo(stack_level, "nSl")
local log_entry = generate_log_entry(request_scoped, log_level, log_str, request_id, debug_info)
Expand Down
14 changes: 7 additions & 7 deletions kong/plugins/opentelemetry/otlp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ do

-- see: kong/include/opentelemetry/proto/logs/v1/logs.proto
local map_severity = {
[ngx.DEBUG] = { 5, "DEBUG" },
[ngx.INFO] = { 9, "INFO" },
[ngx.DEBUG] = { 5, "DEBUG" },
[ngx.INFO] = { 9, "INFO" },
[ngx.NOTICE] = { 11, "NOTICE" },
[ngx.WARN] = { 13, "WARN" },
[ngx.ERR] = { 17, "ERR" },
[ngx.CRIT] = { 19, "CRIT" },
[ngx.ALERT] = { 21, "ALERT" },
[ngx.EMERG] = { 23, "EMERG" },
[ngx.WARN] = { 13, "WARN" },
[ngx.ERR] = { 17, "ERR" },
[ngx.CRIT] = { 19, "CRIT" },
[ngx.ALERT] = { 21, "ALERT" },
[ngx.EMERG] = { 23, "EMERG" },
}

prepare_logs = function(logs, trace_id, flags)
Expand Down

0 comments on commit ad0b93c

Please sign in to comment.