From 30756e820aa00f08ceb4b2fdad9df5f74de502c6 Mon Sep 17 00:00:00 2001 From: Shashi Ranjan Date: Tue, 19 Jan 2016 14:58:29 -0800 Subject: [PATCH] enhancement(plugin/datadog) Added response size as metric New metric response_size added to log request size to statsd server --- kong/plugins/datadog/handler.lua | 55 ++++++++++++++----------------- kong/plugins/datadog/schema.lua | 2 +- spec/plugins/datadog/log_spec.lua | 31 ++++++++++++++--- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/kong/plugins/datadog/handler.lua b/kong/plugins/datadog/handler.lua index 9fbc85bc9254..61d70c412b15 100644 --- a/kong/plugins/datadog/handler.lua +++ b/kong/plugins/datadog/handler.lua @@ -12,25 +12,28 @@ local string_gsub = string.gsub local pairs = pairs local NGX_ERR = ngx.ERR -local function request_counter(api_name, logger) - local stat = api_name..".request.count" - logger:counter(stat, 1, 1) -end - -local function status_counter(api_name, message, logger) - local stat = api_name..".request.status."..message.response.status - logger:counter(stat, 1, 1) -end - -local function request_size_gauge(api_name, message, logger) - local stat = api_name..".request.size" - logger:gauge(stat, message.request.size, 1) -end - -local function latency_gauge(api_name, message, logger) - local stat = api_name..".latency" - logger:gauge(stat, message.latencies.request, 1) -end +local gauges = { + request_size = function (api_name, message, logger) + local stat = api_name..".request.size" + logger:gauge(stat, message.request.size, 1) + end, + response_size = function (api_name, message, logger) + local stat = api_name..".response.size" + logger:gauge(stat, message.response.size, 1) + end, + status_count = function (api_name, message, logger) + local stat = api_name..".request.status."..message.response.status + logger:counter(stat, 1, 1) + end, + latency = function (api_name, message, logger) + local stat = api_name..".latency" + logger:gauge(stat, message.latencies.request, 1) + end, + request_count = function (api_name, message, logger) + local stat = api_name..".request.count" + logger:counter(stat, 1, 1) + end +} local function log(premature, conf, message) if premature then return end @@ -43,17 +46,9 @@ local function log(premature, conf, message) local api_name = string_gsub(message.api.name, "%.", "_") for _, metric in pairs(conf.metrics) do - if metric == "request_size" then - request_size_gauge(api_name, message, logger) - end - if metric == "status_count" then - status_counter(api_name, message, logger) - end - if metric == "latency" then - latency_gauge(api_name, message, logger) - end - if metric == "request_count" then - request_counter(api_name, logger) + local gauge = gauges[metric] + if gauge ~= nil then + gauge(api_name, message, logger) end end diff --git a/kong/plugins/datadog/schema.lua b/kong/plugins/datadog/schema.lua index e3a7a82f758d..e0c7e915c3e0 100644 --- a/kong/plugins/datadog/schema.lua +++ b/kong/plugins/datadog/schema.lua @@ -2,7 +2,7 @@ return { fields = { host = {required = true, type = "string", default = "localhost"}, port = {required = true, type = "number", default = 8125}, - metrics = {required = true, type = "array", enum = {"request_count", "latency", "request_size", "status_count"}, default = {"request_count", "latency", "request_size", "status_count"}}, + metrics = {required = true, type = "array", enum = {"request_count", "latency", "request_size", "status_count", "response_size"}, default = {"request_count", "latency", "request_size", "status_count", "response_size"}}, timeout = {type = "number", default = 10000} } } diff --git a/spec/plugins/datadog/log_spec.lua b/spec/plugins/datadog/log_spec.lua index 56ebe77f7f26..5531516bf948 100644 --- a/spec/plugins/datadog/log_spec.lua +++ b/spec/plugins/datadog/log_spec.lua @@ -15,14 +15,16 @@ describe("Datadog Plugin", function() {request_host = "logging2.com", upstream_url = "http://mockbin.com"}, {request_host = "logging3.com", upstream_url = "http://mockbin.com"}, {request_host = "logging4.com", upstream_url = "http://mockbin.com"}, - {request_host = "logging5.com", upstream_url = "http://mockbin.com"} + {request_host = "logging5.com", upstream_url = "http://mockbin.com"}, + {request_host = "logging6.com", upstream_url = "http://mockbin.com"}, }, plugin = { {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT, metrics = {"request_count"}}, __api = 1}, {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT, metrics = {"latency"}}, __api = 2}, {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT, metrics = {"status_count"}}, __api = 3}, {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT, metrics = {"request_size"}}, __api = 4}, - {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT}, __api = 5} + {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT}, __api = 5}, + {name = "datadog", config = {host = "127.0.0.1", port = UDP_PORT, metrics = {"response_size"}}, __api = 6}, } } spec_helper.start_kong() @@ -56,7 +58,7 @@ describe("Datadog Plugin", function() assert.equal("kong.logging3_com.request.status.200:1|c", res) end) - pending("should log to UDP when metrics is request_size", function() + it("should log to UDP when metrics is request_size", function() local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock UDP server local _, status = http_client.get(STUB_GET_URL, nil, {host = "logging4.com"}) @@ -65,8 +67,11 @@ describe("Datadog Plugin", function() local ok, res = thread:join() assert.True(ok) assert.truthy(res) - -- 113 locally but 111 on travis - assert.equal("kong.logging4_com.request.size:113|g", res) + local message = {} + for w in string.gmatch(res,"kong.logging4_com.request.size:%d*|g") do + table.insert(message, w) + end + assert.equal(1, #message) end) it("should log to UDP when metrics is latency", function() @@ -98,4 +103,20 @@ describe("Datadog Plugin", function() assert.truthy(res) assert.equal("kong.logging5_com.request.count:1|c", res) end) + + it("should log to UDP when metrics is response_size", function() + local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock UDP server + + local _, status = http_client.get(STUB_GET_URL, nil, {host = "logging6.com"}) + assert.equal(200, status) + + local ok, res = thread:join() + assert.True(ok) + assert.truthy(res) + local message = {} + for w in string.gmatch(res,"kong.logging6_com.response.size:%d*|g") do + table.insert(message, w) + end + assert.equal(1, #message) + end) end)