Skip to content

Commit

Permalink
Merge pull request Kong#2513 from Mashape/fix/serializer
Browse files Browse the repository at this point in the history
fix(serializer) stop failing on missing structure
  • Loading branch information
Tieske authored May 11, 2017
2 parents b2511de + eee733f commit b78d1f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kong/plugins/log-serializers/basic.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local tablex = require "pl.tablex"

local _M = {}

local EMPTY = tablex.readonly({})

function _M.serialize(ngx)
local authenticated_entity
if ngx.ctx.authenticated_credential ~= nil then
Expand All @@ -9,8 +13,6 @@ function _M.serialize(ngx)
}
end

local addr = ngx.ctx.balancer_address

return {
request = {
uri = ngx.var.request_uri,
Expand All @@ -25,7 +27,7 @@ function _M.serialize(ngx)
headers = ngx.resp.get_headers(),
size = ngx.var.bytes_sent
},
tries = addr.tries,
tries = (ngx.ctx.balancer_address or EMPTY).tries,
latencies = {
kong = (ngx.ctx.KONG_ACCESS_TIME or 0) +
(ngx.ctx.KONG_RECEIVE_TIME or 0) +
Expand Down
9 changes: 9 additions & 0 deletions spec/01-unit/13-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,14 @@ describe("Log Serializer", function()
},
}, res.tries)
end)

it("does not fail when the 'balancer_address' structure is missing", function()
ngx.ctx.balancer_address = nil

local res = basic.serialize(ngx)
assert.is_table(res)

assert.is_nil(res.tries)
end)
end)
end)

0 comments on commit b78d1f8

Please sign in to comment.