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 4, 2024
1 parent ebf2d38 commit 4a461fd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
42 changes: 30 additions & 12 deletions kong/observability/logs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,42 @@ end

local request_id_get = require "kong.observability.tracing.request_id".get
local time_ns = require "kong.tools.time".time_ns
local cycle_aware_deep_copy = require "kong.tools.utils".cycle_aware_deep_copy
local deep_copy = require "kong.tools.utils".deep_copy

local get_log_level = require "resty.kong.log".get_log_level
local constants_log_levels = require "kong.constants".LOG_LEVELS

local table_new = require "table.new"
local table_clear = require "table.clear"
local string_buffer = require "string.buffer"

local ngx = ngx
local kong = kong
local table = table
local tostring = tostring

local ngx_null = ngx.null
local table_pack = table.pack -- luacheck: ignore

local MAX_WORKER_LOGS = 1000
local MAX_REQUEST_LOGS = 1000
local INITIAL_SIZE_WORKER_LOGS = 100
local NGX_CTX_REQUEST_LOGS_KEY = "o11y_logs_request_scoped"

local worker_logs = table_new(100, 0)
local worker_logs = table_new(INITIAL_SIZE_WORKER_LOGS, 0)
local logline_buf = string_buffer.new()


-- WARNING: avoid using `ngx.log` in this function to prevent recursive loops
local function configured_log_level()
local ok, level = pcall(get_log_level)
if not ok then
level = constants_log_levels[kong.configuration.log_level]
-- This is unexpected outside of the context of unit tests
local level_str = kong.configuration.log_level
_G.native_ngx_log(ngx.WARN,
"[observability] OpenTelemetry logs failed reading dynamic log level. " ..
"Using log level: " .. level_str .. " from configuration."
)
level = constants_log_levels[level_str]
end

return level
Expand All @@ -45,12 +55,21 @@ end

-- needed because table.concat doesn't like booleans
local function concat_tostring(tab)
if #tab == 0 then
local tab_len = #tab
if tab_len == 0 then
return ""
end

for i = 1, #tab do
logline_buf:put(tostring(tab[i]))
for i = 1, tab_len do
local value = tab[i]

if value == ngx_null then
value = "nil"
else
value = tostring(value)
end

logline_buf:put(value)
end

return logline_buf:get()
Expand Down Expand Up @@ -130,7 +149,7 @@ function _M.maybe_push(stack_level, log_level, ...)
end

-- no (or empty) log line
local args = { ... }
local args = table_pack(...)
local log_str = concat_tostring(args)
if log_str == "" then
return
Expand All @@ -144,16 +163,15 @@ end


function _M.get_worker_logs()
local wl = cycle_aware_deep_copy(worker_logs)

table_clear(worker_logs)
local wl = worker_logs
worker_logs = table_new(INITIAL_SIZE_WORKER_LOGS, 0)
return wl
end


function _M.get_request_logs()
local request_logs = get_request_log_buffer()
return cycle_aware_deep_copy(request_logs)
return deep_copy(request_logs)
end


Expand Down
6 changes: 4 additions & 2 deletions kong/plugins/opentelemetry/logs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ local function log(conf)
local worker_logs = o11y_logs.get_worker_logs()
local request_logs = o11y_logs.get_request_logs()

local worker_logs_len = #worker_logs
local request_logs_len = #request_logs
ngx_log(ngx_DEBUG, _log_prefix, "total request_logs in current request: ",
#request_logs, " total worker_logs in current request: ", #worker_logs)
request_logs_len, " total worker_logs in current request: ", worker_logs_len)

if #request_logs + #worker_logs == 0 then
if request_logs_len + worker_logs_len == 0 then
return
end

Expand Down
6 changes: 3 additions & 3 deletions kong/plugins/opentelemetry/otlp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local pb = require "pb"
local new_tab = require "table.new"
local nkeys = require "table.nkeys"
local tablepool = require "tablepool"
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
local deep_copy = require("kong.tools.table").deep_copy

local kong = kong
local insert = table.insert
Expand Down Expand Up @@ -169,7 +169,7 @@ do
encode_traces = function(spans, resource_attributes)
local tab = tablepool_fetch(POOL_OTLP, 0, 2)
if not tab.resource_spans then
tab.resource_spans = cycle_aware_deep_copy(pb_memo_trace.resource_spans)
tab.resource_spans = deep_copy(pb_memo_trace.resource_spans)
end

local resource = tab.resource_spans[1].resource
Expand Down Expand Up @@ -204,7 +204,7 @@ do
encode_logs = function(log_batch, resource_attributes)
local tab = tablepool_fetch(POOL_OTLP, 0, 3)
if not tab.resource_logs then
tab.resource_logs = cycle_aware_deep_copy(pb_memo_log.resource_logs)
tab.resource_logs = deep_copy(pb_memo_log.resource_logs)
end

local resource = tab.resource_logs[1].resource
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/opentelemetry/utils.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local http = require "resty.http"
local clone = require "table.clone"
local null = ngx.null

local tostring = tostring
local null = ngx.null


local CONTENT_TYPE_HEADER_NAME = "Content-Type"
Expand Down Expand Up @@ -52,4 +52,4 @@ return {
http_export_request = http_export_request,
get_headers = get_headers,
_log_prefix = _log_prefix,
}
}

0 comments on commit 4a461fd

Please sign in to comment.