Skip to content

Commit

Permalink
feat(log) add balancer tries and failure information
Browse files Browse the repository at this point in the history
The number of tries will be logged in the basic serializer.
If any retries occurred, then failure information will be added
as well.
  • Loading branch information
Tieske committed Apr 21, 2017
1 parent bd3858f commit adcc925
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
configured log target-url includes credentials. Thanks to
[Amir M. Saeid](https://github.com/amir) for the contribution.
[#2430](https://github.com/Mashape/kong/pull/2430)
- Logging retries and failure information.
[#2429](https://github.com/Mashape/kong/pull/2429).
- Plugins:
- :fireworks: **New Request termination plugin**. This plugin allows to
temporarily disable an API and return a pre-configured response status and
Expand Down
9 changes: 7 additions & 2 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,14 @@ function Kong.balancer()
-- where the retries are executed

-- record failure data
addr.failures = addr.failures or {}
local state, code = get_last_failure()
addr.failures[addr.tries-1] = { name = state, code = code }
addr.failures = addr.failures or {}
addr.failures[addr.tries-1] = {
state = state,
code = code,
ip = addr.ip,
port = addr.port,
}

local ok, err = balancer_execute(addr)
if not ok then
Expand Down
5 changes: 5 additions & 0 deletions kong/plugins/log-serializers/basic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function _M.serialize(ngx)
consumer_id = ngx.ctx.authenticated_credential.consumer_id
}
end

local addr = ngx.ctx.balancer_address
local tries = addr.failures or {}
tries.count = addr.tries

return {
request = {
Expand All @@ -23,6 +27,7 @@ function _M.serialize(ngx)
headers = ngx.resp.get_headers(),
size = ngx.var.bytes_sent
},
tries = tries,
latencies = {
kong = (ngx.ctx.KONG_ACCESS_TIME or 0) +
(ngx.ctx.KONG_RECEIVE_TIME or 0),
Expand Down
35 changes: 34 additions & 1 deletion spec/01-unit/13-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ describe("Log Serializer", function()

before_each(function()
ngx = {
ctx = {},
ctx = {
balancer_address = {
tries = 1,
},
},
var = {
request_uri = "/request_uri",
scheme = "http",
Expand Down Expand Up @@ -60,6 +64,10 @@ describe("Log Serializer", function()
assert.is_nil(res.api)
assert.is_nil(res.consumer)
assert.is_nil(res.authenticated_entity)

-- Tries
assert.is_table(res.tries)
assert.equal(1, res.tries.count)
end)

it("serializes the API object", function()
Expand Down Expand Up @@ -96,5 +104,30 @@ describe("Log Serializer", function()
assert.is_nil(res.consumer)
assert.is_nil(res.api)
end)

it("serializes the tries and failure information", function()
ngx.ctx.balancer_address.tries = 3
ngx.ctx.balancer_address.failures = {
{ state = "next", code = 502, ip = "127.0.0.1", port = 1234 },
{ state = "failed", code = nil, ip = "127.0.0.1", port = 1234 },
}

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

assert.same({
count = 3,
{
code = 502,
ip = '127.0.0.1',
port = 1234,
state = 'next',
}, {
ip = '127.0.0.1',
port = 1234,
state = 'failed',
}
}, res.tries)
end)
end)
end)

0 comments on commit adcc925

Please sign in to comment.