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

feat(log-serializer): add source property to log-serializer #12052

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/log-serializer-source-property.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: 'Add `source` property to log serializer, indicating the response is generated by `kong` or `upstream`.'
type: feature
scope: Core
8 changes: 7 additions & 1 deletion kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
gszr marked this conversation as resolved.
Show resolved Hide resolved
kong_source = "kong"
elseif kong_source == "service" then
kong_source = "upstream"
end
local root = {
request = {
id = request_id_get() or "",
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("kong.log.serialize", function()
},
},
},
KONG_PROXIED = true,
},
var = {
kong_request_id = "1234",
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = nil
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

Expand Down
3 changes: 3 additions & 0 deletions t/01-pdk/02-log/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ qq{
get_headers = function() return {} end,
get_start_time = function() return 1 end,
},
response = {
get_source = function() return "service" end,
},
}
}
},
Expand Down