Skip to content

Commit

Permalink
Merge pull request Kong#923 from Mashape/feature/datadog-resp-size
Browse files Browse the repository at this point in the history
feature(datadog) Add resp-size metric
  • Loading branch information
thibaultcha committed Feb 5, 2016
2 parents 4f01a95 + 4554eed commit b9d61e7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
55 changes: 25 additions & 30 deletions kong/plugins/datadog/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/datadog/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
31 changes: 26 additions & 5 deletions spec/plugins/datadog/log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -56,7 +58,7 @@ describe("Datadog Plugin", function()
assert.equal("kong.logging3_com.request.status.200:1|c", res)
end)

it("#ci 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"})
Expand All @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit b9d61e7

Please sign in to comment.