Skip to content

Commit

Permalink
Merge pull request #1179 from Mashape/enhance/datadog
Browse files Browse the repository at this point in the history
added metrics for consumer
  • Loading branch information
subnetmarco committed Apr 26, 2016
2 parents 8e059e0 + 7aadc1b commit 57cc5d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions kong/plugins/datadog/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ local gauges = {
request_count = function (api_name, message, logger)
local stat = api_name..".request.count"
logger:counter(stat, 1, 1)
end,
unique_users = function (api_name, message, logger)
if message.authenticated_entity ~= nil and message.authenticated_entity.consumer_id ~= nil then
local stat = api_name..".user.uniques"
logger:set(stat, message.authenticated_entity.consumer_id)
end
end,
request_per_user = function (api_name, message, logger)
if message.authenticated_entity ~= nil and message.authenticated_entity.consumer_id ~= nil then
local stat = api_name.."."..string_gsub(message.authenticated_entity.consumer_id, "-", "_")..".request.count"
logger:counter(stat, 1, 1)
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", "response_size"}, default = {"request_count", "latency", "request_size", "status_count", "response_size"}},
metrics = {required = true, type = "array", enum = {"request_count", "latency", "request_size", "status_count", "response_size", "unique_users", "request_per_user"}, default = {"request_count", "latency", "request_size", "status_count", "response_size", "unique_users", "request_per_user"}},
timeout = {type = "number", default = 10000}
}
}
3 changes: 3 additions & 0 deletions kong/plugins/datadog/statsd_logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local ngx_log = ngx.log
local table_concat = table.concat
local setmetatable = setmetatable
local NGX_ERR = ngx.ERR
local NGX_DEBUG = ngx.DEBUG
local tostring = tostring

local statsd_mt = {}
statsd_mt.__index = statsd_mt
Expand Down Expand Up @@ -51,6 +53,7 @@ end

function statsd_mt:send_statsd(stat, delta, kind, sample_rate)
local udp_message = self:create_statsd_message(stat, delta, kind, sample_rate)
ngx_log(NGX_DEBUG, "Sending data to statsd server: "..udp_message)
local ok, err = self.socket:send(udp_message)
if not ok then
ngx_log(NGX_ERR, "failed to send data to "..self.host..":"..tostring(self.port)..": ", err)
Expand Down

0 comments on commit 57cc5d8

Please sign in to comment.