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

fix (plugin/loggly) fix for issue 1183 #1184

Merged
merged 1 commit into from
May 24, 2016
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
6 changes: 2 additions & 4 deletions kong/plugins/loggly/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ local BasePlugin = require "kong.plugins.base_plugin"
local basic_serializer = require "kong.plugins.log-serializers.basic"
local cjson = require "cjson"


local LogglyLogHandler = BasePlugin:extend()

LogglyLogHandler.PRIORITY = 1


local os_date = os.date
local tostring = tostring
local ngx_log = ngx.log
Expand Down Expand Up @@ -94,9 +92,9 @@ local function log(premature, conf, message)
if premature then return end

if message.response.status >= 500 then
return decide_severity(conf.log_level, conf.server_errors_severity, message)
return decide_severity(conf, conf.server_errors_severity, message)
elseif message.response.status >= 400 then
return decide_severity(conf.log_level, conf.client_errors_severity, message)
return decide_severity(conf, conf.client_errors_severity, message)
else
return decide_severity(conf, conf.successful_severity, message)
end
Expand Down
65 changes: 63 additions & 2 deletions spec/plugins/loggly/log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ local cjson = require "cjson"
local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"

local STUB_GET_URL = spec_helper.STUB_GET_URL
local PROXY_URL = spec_helper.PROXY_URL
local STUB_GET_URL = PROXY_URL.."/request"

local UDP_PORT = spec_helper.find_port()

Expand All @@ -15,7 +16,7 @@ describe("Logging Plugins", function()
{ request_host = "logging.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging1.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging2.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging3.com", upstream_url = "http://mockbin.com" }
{ request_host = "logging3.com", upstream_url = "http://mockbin.com" },
},
plugin = {
{ name = "loggly", config = { host = "127.0.0.1", port = UDP_PORT, key = "123456789", log_level = "info",
Expand Down Expand Up @@ -111,6 +112,66 @@ describe("Logging Plugins", function()
local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 200", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL, nil, { host = "logging3.com" })
assert.are.equal(200, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 401", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL.."/status/401/", nil, { host = "logging3.com" })
assert.are.equal(401, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 500", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL.."/status/500/", nil, { host = "logging3.com" })
assert.are.equal(500, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
Expand Down