diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ca63519d49c..d3e61cbdb0b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 55da5054b6b1..cf24b5b7f1f5 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -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, @@ -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(), @@ -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), diff --git a/spec/01-unit/10-log_serializer_spec.lua b/spec/01-unit/10-log_serializer_spec.lua index e43783e6dfac..a23341b63acc 100644 --- a/spec/01-unit/10-log_serializer_spec.lua +++ b/spec/01-unit/10-log_serializer_spec.lua @@ -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, @@ -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)