Skip to content

Commit

Permalink
feat(pdk/log): Add upstream status code (#10296)
Browse files Browse the repository at this point in the history
* feat(pdk/log): Add upstream status code

* test: add assertion

* make the case a bit more complex

* change log

* remove pdk fix changelog

* Update spec/01-unit/10-log_serializer_spec.lua

Co-authored-by: Vinicius Mignot <vinicius.mignot@gmail.com>

---------

Co-authored-by: Vinicius Mignot <vinicius.mignot@gmail.com>
  • Loading branch information
pluveto and locao authored Feb 15, 2023
1 parent e6d544a commit bb82580
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

1 comment on commit bb82580

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:bb8258007e3bbf6fbc818eed0f9f1900853c13ef
Artifacts available https://github.com/Kong/kong/actions/runs/4184479677

Please sign in to comment.