Skip to content

Commit

Permalink
perf(request-id): use $kong_request_id for $request_id for better…
Browse files Browse the repository at this point in the history
… performance (#11725)

Bumped lua-kong-nginx-module to `0.8.0`.

This has better performance than `$request_id`.

KAG-2734
  • Loading branch information
chronolaw authored and samugi committed Nov 15, 2023
1 parent 1994d79 commit e29c443
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}};
Expand Down
8 changes: 5 additions & 3 deletions kong/tracing/request_id.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local ngx = ngx
local var = ngx.var
local get_phase = ngx.get_phase

local NGX_VAR_PHASES = {
set = true,
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions spec/01-unit/26-tracing/03-request-id_spec.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit e29c443

Please sign in to comment.