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(pdk/log): Add upstream status code #10296

Merged
merged 7 commits into from
Feb 15, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
errors to a single array via the optional `flatten_errors` query parameter.
[#10161](https://github.com/Kong/kong/pull/10161)

#### PDK

- Support for `upstream_status` field in log serializer.
[#10296](https://github.com/Kong/kong/pull/10296)

### Fixes

#### Core
Expand Down
8 changes: 7 additions & 1 deletion kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ do
end
end

-- The value of upstream_status is a string, and status codes may be
-- 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 root = {
request = {
uri = request_uri,
Expand All @@ -813,6 +818,7 @@ do
tls = build_tls_info(var, ctx.CLIENT_VERIFY_OVERRIDE),
},
upstream_uri = upstream_uri,
upstream_status = upstream_status,
response = {
status = ongx.status,
headers = ongx.resp.get_headers(),
Expand All @@ -822,7 +828,7 @@ do
kong = (ctx.KONG_PROXY_LATENCY or ctx.KONG_RESPONSE_LATENCY or 0) +
(ctx.KONG_RECEIVE_TIME or 0),
proxy = ctx.KONG_WAITING_TIME or -1,
request = var.request_time * 1000
request = tonumber(var.request_time) * 1000,
},
tries = (ctx.balancer_data or {}).tries,
authenticated_entity = build_authenticated_entity(ctx),
Expand Down
6 changes: 5 additions & 1 deletion spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe("kong.log.serialize", function()
request_length = "200",
bytes_sent = "99",
request_time = "2",
remote_addr = "1.1.1.1"
remote_addr = "1.1.1.1",
-- may be a non-numeric string,
-- see http://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_addr
upstream_status = "500, 200 : 200, 200",
},
update_time = ngx.update_time,
sleep = ngx.sleep,
Expand Down Expand Up @@ -74,6 +77,7 @@ describe("kong.log.serialize", function()
assert.same({"arg1", "arg2"}, res.request.querystring)
assert.equal("http://test.com:80/request_uri", res.request.url)
assert.equal("/upstream_uri", res.upstream_uri)
assert.equal("500, 200 : 200, 200", res.upstream_status)
assert.equal(200, res.request.size)
assert.equal("/request_uri", res.request.uri)

Expand Down