From e29c4436048058f71a0db2c4eee5f9eca32199ef Mon Sep 17 00:00:00 2001 From: Chrono Date: Fri, 13 Oct 2023 15:09:09 +0800 Subject: [PATCH] perf(request-id): use `$kong_request_id` for `$request_id` for better performance (#11725) Bumped lua-kong-nginx-module to `0.8.0`. This has better performance than `$request_id`. KAG-2734 --- kong/pdk/log.lua | 6 +++--- kong/templates/nginx_kong.lua | 4 ++-- kong/tracing/request_id.lua | 8 +++++--- spec/01-unit/10-log_serializer_spec.lua | 2 +- spec/01-unit/26-tracing/03-request-id_spec.lua | 13 +++++++++++-- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index a7ee98b9915..42ea97c973e 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -918,14 +918,14 @@ local function new_log(namespace, format) if not buf then error(err, 2) end - + for log_lvl_name, log_lvl in pairs(_LEVELS) do - self[log_lvl_name] = gen_log_func(log_lvl, buf) + self[log_lvl_name] = gen_log_func(log_lvl, buf) end self.deprecation = new_deprecation(gen_log_func(_LEVELS.warn, buf, nil, 5)) end - + self.set_format(format) self.inspect = new_inspect(namespace) diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index d2614ac2c56..84330518720 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -58,7 +58,7 @@ init_worker_by_lua_block { log_format kong_log_format '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' - 'kong_request_id: "$request_id"'; + 'kong_request_id: "$kong_request_id"'; # Load variable indexes lua_kong_load_var_index $args; @@ -128,7 +128,7 @@ server { # Append the kong request id to the error log # https://github.com/Kong/lua-kong-nginx-module#lua_kong_error_log_request_id - lua_kong_error_log_request_id $request_id; + lua_kong_error_log_request_id $kong_request_id; access_log ${{PROXY_ACCESS_LOG}} kong_log_format; error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}}; diff --git a/kong/tracing/request_id.lua b/kong/tracing/request_id.lua index c16f7a5d705..bab196df1bb 100644 --- a/kong/tracing/request_id.lua +++ b/kong/tracing/request_id.lua @@ -1,4 +1,6 @@ local ngx = ngx +local var = ngx.var +local get_phase = ngx.get_phase local NGX_VAR_PHASES = { set = true, @@ -21,14 +23,14 @@ local function get() local rid = get_ctx_request_id() if not rid then - local phase = ngx.get_phase() + local phase = get_phase() if not NGX_VAR_PHASES[phase] then return nil, "cannot access ngx.var in " .. phase .. " phase" end -- first access to the request id for this request: - -- initialize with the value of $request_id - rid = ngx.var.request_id + -- initialize with the value of $kong_request_id + rid = var.kong_request_id ngx.ctx.request_id = rid end diff --git a/spec/01-unit/10-log_serializer_spec.lua b/spec/01-unit/10-log_serializer_spec.lua index dfd7ec108d1..23295579b60 100644 --- a/spec/01-unit/10-log_serializer_spec.lua +++ b/spec/01-unit/10-log_serializer_spec.lua @@ -20,7 +20,7 @@ describe("kong.log.serialize", function() }, }, var = { - request_id = "1234", + kong_request_id = "1234", request_uri = "/request_uri", upstream_uri = "/upstream_uri", scheme = "http", diff --git a/spec/01-unit/26-tracing/03-request-id_spec.lua b/spec/01-unit/26-tracing/03-request-id_spec.lua index 7b6bf3537f4..e4b85be593d 100644 --- a/spec/01-unit/26-tracing/03-request-id_spec.lua +++ b/spec/01-unit/26-tracing/03-request-id_spec.lua @@ -1,9 +1,13 @@ -local request_id = require "kong.tracing.request_id" +local function reload_module(name) + package.loaded[name] = nil + return require(name) +end + local function reset_globals(id) _G.ngx.ctx = {} _G.ngx.var = { - request_id = id, + kong_request_id = id, } _G.ngx.get_phase = function() -- luacheck: ignore return "access" @@ -43,6 +47,9 @@ describe("Request ID unit tests", function() end) it("returns the expected Request ID and caches it in ctx", function() + + local request_id = reload_module("kong.tracing.request_id") + local id, err = request_id.get() assert.is_nil(err) assert.equal(ngx_var_request_id, id) @@ -54,6 +61,8 @@ describe("Request ID unit tests", function() it("fails if accessed from phase that cannot read ngx.var", function() _G.ngx.get_phase = function() return "init" end + local request_id = reload_module("kong.tracing.request_id") + local id, err = request_id.get() assert.is_nil(id) assert.equal("cannot access ngx.var in init phase", err)