From ebf2d381fb10038d95855f37521d1c7f98c50a08 Mon Sep 17 00:00:00 2001 From: samugi Date: Tue, 2 Jul 2024 12:14:03 +0200 Subject: [PATCH] fixup! feat(observability): add OpenTelemetry logs --- kong/globalpatches.lua | 11 +++++------ kong/observability/logs.lua | 26 +++++++++++++------------- kong/plugins/opentelemetry/otlp.lua | 14 +++++++------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 5e7461057613..99c128560df1 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -593,15 +593,13 @@ 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 @@ -609,8 +607,9 @@ return function(options) -- 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 diff --git a/kong/observability/logs.lua b/kong/observability/logs.lua index e824426a79ad..f936b2e23c0c 100644 --- a/kong/observability/logs.lua +++ b/kong/observability/logs.lua @@ -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() @@ -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 @@ -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) diff --git a/kong/plugins/opentelemetry/otlp.lua b/kong/plugins/opentelemetry/otlp.lua index d2565f535096..6df70d30680a 100644 --- a/kong/plugins/opentelemetry/otlp.lua +++ b/kong/plugins/opentelemetry/otlp.lua @@ -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)