From 4a461fd9dc1c8c3e4772006e761d5369f468aeac Mon Sep 17 00:00:00 2001 From: samugi Date: Thu, 4 Jul 2024 10:12:28 +0200 Subject: [PATCH] fixup! feat(observability): add OpenTelemetry logs --- kong/observability/logs.lua | 42 ++++++++++++++++++++-------- kong/plugins/opentelemetry/logs.lua | 6 ++-- kong/plugins/opentelemetry/otlp.lua | 6 ++-- kong/plugins/opentelemetry/utils.lua | 4 +-- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/kong/observability/logs.lua b/kong/observability/logs.lua index f936b2e23c0c..64e7fac679cf 100644 --- a/kong/observability/logs.lua +++ b/kong/observability/logs.lua @@ -11,13 +11,12 @@ 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 @@ -25,18 +24,29 @@ 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 @@ -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() @@ -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 @@ -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 diff --git a/kong/plugins/opentelemetry/logs.lua b/kong/plugins/opentelemetry/logs.lua index ac254326205e..7e64c12e2043 100644 --- a/kong/plugins/opentelemetry/logs.lua +++ b/kong/plugins/opentelemetry/logs.lua @@ -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 diff --git a/kong/plugins/opentelemetry/otlp.lua b/kong/plugins/opentelemetry/otlp.lua index 6df70d30680a..ded49eb3ed2c 100644 --- a/kong/plugins/opentelemetry/otlp.lua +++ b/kong/plugins/opentelemetry/otlp.lua @@ -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 @@ -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 @@ -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 diff --git a/kong/plugins/opentelemetry/utils.lua b/kong/plugins/opentelemetry/utils.lua index 457607ff945b..5802ceeadc95 100644 --- a/kong/plugins/opentelemetry/utils.lua +++ b/kong/plugins/opentelemetry/utils.lua @@ -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" @@ -52,4 +52,4 @@ return { http_export_request = http_export_request, get_headers = get_headers, _log_prefix = _log_prefix, -} \ No newline at end of file +}