From d82139766a0da8521005f4470e4b2a97d268448e Mon Sep 17 00:00:00 2001 From: Shashi Ranjan Date: Wed, 27 Apr 2016 18:09:23 -0700 Subject: [PATCH] fix (plugin/loggly) fix for issue 1183 loggly plugin was failing for 400 and 500 errors. This commit fix the issue 1183 --- kong/plugins/loggly/handler.lua | 6 +-- spec/plugins/loggly/log_spec.lua | 65 +++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/kong/plugins/loggly/handler.lua b/kong/plugins/loggly/handler.lua index d05c2bbcec5..30b718b6d10 100644 --- a/kong/plugins/loggly/handler.lua +++ b/kong/plugins/loggly/handler.lua @@ -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 @@ -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 diff --git a/spec/plugins/loggly/log_spec.lua b/spec/plugins/loggly/log_spec.lua index 945a8ef48cf..d9a556b0003 100644 --- a/spec/plugins/loggly/log_spec.lua +++ b/spec/plugins/loggly/log_spec.lua @@ -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() @@ -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", @@ -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)