Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): respect custom proxy_access_log #12073

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/respect-custom-proxy_access_log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "respect custom `proxy_access_log`"
type: bugfix
scope: Configuration
13 changes: 12 additions & 1 deletion kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ local function compile_conf(kong_config, conf_template, template_env_inject)
-- computed config properties for templating
local compile_env = {
_escape = ">",
proxy_access_log_enabled = kong_config.proxy_access_log ~= "off",
pairs = pairs,
ipairs = ipairs,
tostring = tostring,
Expand All @@ -248,6 +247,18 @@ local function compile_conf(kong_config, conf_template, template_env_inject)
}
}

local kong_proxy_access_log = kong_config.proxy_access_log
if kong_proxy_access_log ~= "off" then
compile_env.proxy_access_log_enabled = true
end
if kong_proxy_access_log then
-- example: proxy_access_log = 'logs/some-file.log apigw_json'
local _, custom_format_name = string.match(kong_proxy_access_log, "^(%S+)%s(%S+)")
if custom_format_name then
compile_env.custom_proxy_access_log = true
end
end

compile_env = pl_tablex.merge(compile_env, template_env_inject or {}, true)

do
Expand Down
4 changes: 4 additions & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ server {
lua_kong_error_log_request_id $kong_request_id;

> if proxy_access_log_enabled then
> if custom_proxy_access_log then
access_log ${{PROXY_ACCESS_LOG}};
> else
access_log ${{PROXY_ACCESS_LOG}} kong_log_format;
> end
> else
access_log off;
> end
Expand Down
56 changes: 47 additions & 9 deletions spec/01-unit/04-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -486,34 +486,72 @@ describe("NGINX conf compiler", function()

describe("injected NGINX directives", function()
it("injects proxy_access_log directive", function()
local conf = assert(conf_loader(nil, {
local conf, nginx_conf
conf = assert(conf_loader(nil, {
proxy_access_log = "/dev/stdout",
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("access_log%s/dev/stdout%skong_log_format;", nginx_conf)
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)

local conf = assert(conf_loader(nil, {
conf = assert(conf_loader(nil, {
proxy_access_log = "off",
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("access_log%soff;", nginx_conf)
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)

local conf = assert(conf_loader(nil, {
conf = assert(conf_loader(nil, {
proxy_access_log = "/dev/stdout apigw-json",
nginx_http_log_format = 'apigw-json "$kong_request_id"',
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("access_log%s/dev/stdout%sapigw%-json;", nginx_conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)

-- configure an undefined log format will error
-- on kong start. This is expected
conf = assert(conf_loader(nil, {
proxy_access_log = "/dev/stdout not-exist",
nginx_http_log_format = 'apigw-json "$kong_request_id"',
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
nginx_conf = prefix_handler.compile_kong_conf(conf)
samugi marked this conversation as resolved.
Show resolved Hide resolved
assert.matches("access_log%s/dev/stdout%snot%-exist;", nginx_conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)

conf = assert(conf_loader(nil, {
proxy_access_log = "/tmp/not-exist.log",
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("access_log%s/tmp/not%-exist.log%skong_log_format;", nginx_conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)

conf = assert(conf_loader(nil, {
prefix = "servroot_tmp",
nginx_stream_log_format = "custom '$protocol $status'",
proxy_stream_access_log = "/dev/stdout custom",
stream_listen = "0.0.0.0:9100",
nginx_stream_tcp_nodelay = "on",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert(prefix_handler.prepare_prefix(conf))
nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("access_log%slogs/access.log%skong_log_format;", nginx_conf)
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
assert.matches("access_log%s/dev/stdout%scustom;", nginx_conf)
end)

Expand Down
Loading