From 0b6bd16a5d5645467a578cbe9941656b8c26081a Mon Sep 17 00:00:00 2001 From: Douglas Lee Date: Fri, 17 Nov 2023 14:57:50 +0800 Subject: [PATCH 1/5] feat(log-serializer): add `source` property to log-serializer --- .../kong/log-serializer-source-property.yml | 3 +++ kong/pdk/log.lua | 8 ++++++- spec/01-unit/10-log_serializer_spec.lua | 23 ++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/kong/log-serializer-source-property.yml diff --git a/changelog/unreleased/kong/log-serializer-source-property.yml b/changelog/unreleased/kong/log-serializer-source-property.yml new file mode 100644 index 000000000000..326950c22ab4 --- /dev/null +++ b/changelog/unreleased/kong/log-serializer-source-property.yml @@ -0,0 +1,3 @@ +message: 'Add `source` property to log serializer, indicating the response is generated by `kong` or `upstream`.' +type: feature +scope: Core diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index e1cf4892cd8d..022da0203d89 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -816,7 +816,12 @@ do -- seperated by comma or grouped by colon, according to -- the nginx doc: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream_status local upstream_status = var.upstream_status or "" - + local kong_source = okong.response.get_source(ongx.ctx) + if kong_source == "exit" or kong_source == "error" then + kong_source = "kong" + elseif kong_source == "service" then + kong_source = "upstream" + end local root = { request = { id = request_id_get() or "", @@ -848,6 +853,7 @@ do consumer = cycle_aware_deep_copy(ctx.authenticated_consumer), client_ip = var.remote_addr, started_at = okong.request.get_start_time(), + source = kong_source, } return edit_result(ctx, root) diff --git a/spec/01-unit/10-log_serializer_spec.lua b/spec/01-unit/10-log_serializer_spec.lua index bd465d22805e..4218a5d41e72 100644 --- a/spec/01-unit/10-log_serializer_spec.lua +++ b/spec/01-unit/10-log_serializer_spec.lua @@ -20,6 +20,7 @@ describe("kong.log.serialize", function() }, }, }, + KONG_PROXIED = true, }, var = { kong_request_id = "1234", @@ -43,7 +44,7 @@ describe("kong.log.serialize", function() get_uri_args = function() return {"arg1", "arg2"} end, get_method = function() return "POST" end, get_headers = function() return {header1 = "header1", header2 = "header2", authorization = "authorization"} end, - start_time = function() return 3 end + start_time = function() return 3 end, }, resp = { get_headers = function() return {header1 = "respheader1", header2 = "respheader2", ["set-cookie"] = "delicious=delicacy"} end @@ -99,6 +100,8 @@ describe("kong.log.serialize", function() -- Tries assert.is_table(res.tries) + + assert.equal("upstream", res.source) end) it("uses port map (ngx.ctx.host_port) for request url ", function() @@ -173,6 +176,24 @@ describe("kong.log.serialize", function() }, res.tries) end) + it("serializes the response.source", function() + ngx.ctx.KONG_EXITED = true + ngx.ctx.KONG_PROXIED = nil + ngx.ctx.KONG_UNEXPECTED = nil + + local res = kong.log.serialize({ngx = ngx, kong = kong, }) + assert.is_table(res) + assert.same("kong", res.source) + + ngx.ctx.KONG_UNEXPECTED = true + ngx.ctx.KONG_EXITED = nil + ngx.ctx.KONG_PROXIED = nil + + local res = kong.log.serialize({ngx = ngx, kong = kong, }) + assert.is_table(res) + assert.same("kong", res.source) + end) + it("does not fail when the 'balancer_data' structure is missing", function() ngx.ctx.balancer_data = nil From eaa7e589722011f7c9066eb6f0735f98160daa56 Mon Sep 17 00:00:00 2001 From: Douglas Lee Date: Fri, 17 Nov 2023 15:11:57 +0800 Subject: [PATCH 2/5] test --- t/01-pdk/02-log/00-phase_checks.t | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/01-pdk/02-log/00-phase_checks.t b/t/01-pdk/02-log/00-phase_checks.t index 2bc16e1d3444..ecea2458341d 100644 --- a/t/01-pdk/02-log/00-phase_checks.t +++ b/t/01-pdk/02-log/00-phase_checks.t @@ -64,6 +64,9 @@ qq{ get_headers = function() return {} end, get_start_time = function() return 1 end, }, + response = { + get_source = function() return "service" end, + }, } } }, From e79c615daf780f45d9470bef0a989105b41575d6 Mon Sep 17 00:00:00 2001 From: Douglas Lee Date: Fri, 17 Nov 2023 15:11:57 +0800 Subject: [PATCH 3/5] test --- spec/01-unit/10-log_serializer_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/01-unit/10-log_serializer_spec.lua b/spec/01-unit/10-log_serializer_spec.lua index 4218a5d41e72..005772ca8b01 100644 --- a/spec/01-unit/10-log_serializer_spec.lua +++ b/spec/01-unit/10-log_serializer_spec.lua @@ -185,7 +185,7 @@ describe("kong.log.serialize", function() assert.is_table(res) assert.same("kong", res.source) - ngx.ctx.KONG_UNEXPECTED = true + ngx.ctx.KONG_UNEXPECTED = nil ngx.ctx.KONG_EXITED = nil ngx.ctx.KONG_PROXIED = nil From 9feaf291a20248e29a098c215558b62f7c8ac3d5 Mon Sep 17 00:00:00 2001 From: Douglas Lee Date: Fri, 24 Nov 2023 14:17:23 +0800 Subject: [PATCH 4/5] adjust code style --- kong/pdk/log.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 022da0203d89..0c38ffbf88e6 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -816,12 +816,14 @@ do -- seperated by comma or grouped by colon, according to -- the nginx doc: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream_status local upstream_status = var.upstream_status or "" - local kong_source = okong.response.get_source(ongx.ctx) - if kong_source == "exit" or kong_source == "error" then - kong_source = "kong" - elseif kong_source == "service" then - kong_source = "upstream" + + local response_source = okong.response.get_source(ongx.ctx) + if response_source == "exit" or response_source == "error" then + response_source = "kong" + elseif response_source == "service" then + response_source = "upstream" end + local root = { request = { id = request_id_get() or "", @@ -853,7 +855,7 @@ do consumer = cycle_aware_deep_copy(ctx.authenticated_consumer), client_ip = var.remote_addr, started_at = okong.request.get_start_time(), - source = kong_source, + source = response_source, } return edit_result(ctx, root) From 436ff7d09f91eb1798fbb07c9b414183747afb63 Mon Sep 17 00:00:00 2001 From: Douglas Lee Date: Wed, 29 Nov 2023 10:47:09 +0800 Subject: [PATCH 5/5] move vars into constants module --- kong/constants.lua | 13 +++++++++++++ kong/pdk/log.lua | 10 ++++------ kong/pdk/response.lua | 8 +++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/kong/constants.lua b/kong/constants.lua index 46a16fcac2a1..fc3b8a18a3b2 100644 --- a/kong/constants.lua +++ b/kong/constants.lua @@ -253,6 +253,19 @@ local constants = { SCHEMA_NAMESPACES = { PROXY_WASM_FILTERS = "proxy-wasm-filters", }, + + RESPONSE_SOURCE = { + TYPES = { + ERROR = "error", + EXIT = "exit", + SERVICE = "service", + }, + NAMES = { + error = "kong", + exit = "kong", + service = "upstream", + } + } } for _, v in ipairs(constants.CLUSTERING_SYNC_STATUS) do diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 0c38ffbf88e6..7fbaf168f7c1 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -18,6 +18,7 @@ local ngx_ssl = require "ngx.ssl" local phase_checker = require "kong.pdk.private.phases" local utils = require "kong.tools.utils" local cycle_aware_deep_copy = utils.cycle_aware_deep_copy +local constants = require "kong.constants" local sub = string.sub local type = type @@ -46,6 +47,7 @@ local _DEFAULT_NAMESPACED_FORMAT = "%file_src:%line_src [%namespace] %message" local PHASES = phase_checker.phases local PHASES_LOG = PHASES.log local QUESTION_MARK = byte("?") +local TYPE_NAMES = constants.RESPONSE_SOURCE.NAMES local phases_with_ctx = phase_checker.new(PHASES.rewrite, @@ -818,11 +820,7 @@ do local upstream_status = var.upstream_status or "" local response_source = okong.response.get_source(ongx.ctx) - if response_source == "exit" or response_source == "error" then - response_source = "kong" - elseif response_source == "service" then - response_source = "upstream" - end + local response_source_name = TYPE_NAMES[response_source] local root = { request = { @@ -855,7 +853,7 @@ do consumer = cycle_aware_deep_copy(ctx.authenticated_consumer), client_ip = var.remote_addr, started_at = okong.request.get_start_time(), - source = response_source, + source = response_source_name, } return edit_result(ctx, root) diff --git a/kong/pdk/response.lua b/kong/pdk/response.lua index 228626b62943..dd83b2a8270a 100644 --- a/kong/pdk/response.lua +++ b/kong/pdk/response.lua @@ -18,6 +18,7 @@ local checks = require "kong.pdk.private.checks" local phase_checker = require "kong.pdk.private.phases" local utils = require "kong.tools.utils" local request_id = require "kong.tracing.request_id" +local constants = require "kong.constants" local ngx = ngx @@ -40,6 +41,7 @@ local is_http_subsystem = ngx and ngx.config.subsystem == "http" if is_http_subsystem then add_header = require("ngx.resp").add_header end +local RESPONSE_SOURCE_TYPES = constants.RESPONSE_SOURCE.TYPES local PHASES = phase_checker.phases @@ -349,15 +351,15 @@ local function new(self, major_version) end if ctx.KONG_UNEXPECTED then - return "error" + return RESPONSE_SOURCE_TYPES.ERROR end if ctx.KONG_EXITED then - return "exit" + return RESPONSE_SOURCE_TYPES.EXIT end if ctx.KONG_PROXIED then - return "service" + return RESPONSE_SOURCE_TYPES.SERVICE end return "error"