Skip to content

Commit

Permalink
Reports
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jun 4, 2015
1 parent c63033a commit 0465806
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 10 deletions.
4 changes: 4 additions & 0 deletions kong-0.3.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ build = {
["kong.resolver.header_filter"] = "kong/resolver/header_filter.lua",
["kong.resolver.certificate"] = "kong/resolver/certificate.lua",

["kong.reports.handler"] = "kong/reports/handler.lua",
["kong.reports.init_worker"] = "kong/reports/init_worker.lua",
["kong.reports.log"] = "kong/reports/log.lua",

["kong.dao.error"] = "kong/dao/error.lua",
["kong.dao.schemas_validation"] = "kong/dao/schemas_validation.lua",
["kong.dao.schemas.apis"] = "kong/dao/schemas/apis.lua",
Expand Down
3 changes: 3 additions & 0 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ nginx: |
lua_code_cache on;
lua_max_running_timers 4096;
lua_max_pending_timers 16384;
lua_shared_dict locks 100k;
lua_shared_dict cache {{memory_cache_size}}m;
lua_socket_log_errors off;
Expand All @@ -118,6 +119,8 @@ nginx: |
end
';
init_worker_by_lua 'kong.exec_plugins_init_worker()';
server {
server_name _;
listen {{proxy_port}};
Expand Down
4 changes: 3 additions & 1 deletion kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local responses = require "kong.tools.responses"
local validations = require "kong.dao.schemas_validation"
local app_helpers = require "lapis.application"
local utils = require "kong.tools.utils"

local _M = {}

Expand Down Expand Up @@ -93,11 +94,12 @@ function _M.put(params, dao_collection)
end
end

function _M.post(params, dao_collection)
function _M.post(params, dao_collection, success)
local data, err = dao_collection:insert(params)
if err then
return app_helpers.yield_error(err)
else
if success then success(utils.table_copy(data)) end
return responses.send_HTTP_CREATED(data)
end
end
Expand Down
9 changes: 8 additions & 1 deletion kong/api/routes/plugins_configurations.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local crud = require "kong.api.crud_helpers"
local syslog = require "kong.tools.syslog"
local constants = require "kong.constants"

return {
["/plugins_configurations"] = {
Expand All @@ -11,7 +13,12 @@ return {
end,

POST = function(self, dao_factory)
crud.post(self.params, dao_factory.plugins_configurations)
crud.post(self.params, dao_factory.plugins_configurations, function(data)
if configuration.send_anonymous_reports then
data.signal = constants.SYSLOG.API
syslog.log(syslog.format_entity(data))
end
end)
end
},

Expand Down
3 changes: 2 additions & 1 deletion kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ return {
ROCK_VERSION = VERSION.."-1",
SYSLOG = {
ADDRESS = "kong-hf.mashape.com",
PORT = 61828
PORT = 61828,
API = "api"
},
CLI = {
GLOBAL_KONG_CONF = "/etc/kong/kong.yml",
Expand Down
19 changes: 17 additions & 2 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ local function init_plugins()
handler = require("kong.resolver.handler")()
})

if configuration.send_anonymous_reports then
table.insert(result, {
reports = true,
name = "reports",
handler = require("kong.reports.handler")()
})
end

-- Add the plugins in a sorted order
for _, v in utils.sort_table_iter(unsorted_plugins, utils.sort.descending) do -- In descending order
if v then
Expand Down Expand Up @@ -150,6 +158,13 @@ function _M.init()
plugins = init_plugins()
end

function _M.exec_plugins_init_worker()
-- Calling the Init handler
for _, plugin in ipairs(plugins) do
plugin.handler:init_worker()
end
end

function _M.exec_plugins_certificate()
ngx.ctx.plugin_conf = {}

Expand Down Expand Up @@ -259,8 +274,8 @@ function _M.exec_plugins_log()

for _, plugin in ipairs(plugins) do
local conf = ngx.ctx.plugin_conf[plugin.name]
if conf then
plugin.handler:log(conf.value)
if conf or plugin.reports then
plugin.handler:log(conf and conf.value or nil)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/base_plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ function BasePlugin:new(name)
self._name = name
end

function BasePlugin:init_worker()
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": init_worker")
end

function BasePlugin:certificate()
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": certificate")
end
Expand Down
21 changes: 21 additions & 0 deletions kong/reports/handler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local BasePlugin = require "kong.plugins.base_plugin"
local init_worker = require "kong.reports.init_worker"
local log = require "kong.reports.log"

local ReportsHandler = BasePlugin:extend()

function ReportsHandler:new()
ReportsHandler.super.new(self, "reports")
end

function ReportsHandler:init_worker()
ReportsHandler.super.init_worker(self)
init_worker.execute()
end

function ReportsHandler:log()
ReportsHandler.super.log(self)
log.execute()
end

return ReportsHandler
34 changes: 34 additions & 0 deletions kong/reports/init_worker.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local syslog = require "kong.tools.syslog"
local lock = require "resty.lock"
local cache = require "kong.tools.database_cache"

local INTERVAL = 3600

local _M = {}

local function create_timer(at, cb)
local ok, err = ngx.timer.at(at, cb)
if not ok then
ngx.log(ngx.ERR, "[reports] failed to create timer: ", err)
end
end

local function send_ping(premature)
local lock = lock:new("locks", {
exptime = INTERVAL - 0.001
})
local elapsed = lock:lock("ping")
if elapsed and elapsed == 0 then
local reqs = cache.get(cache.requests_key())
if not reqs then reqs = 0 end
syslog.log({signal = "ping", requests=reqs})
end
create_timer(INTERVAL, send_ping)
end

function _M.execute()
cache.rawset(cache.requests_key(), 0, 0) -- Initializing the counter
create_timer(0, send_ping)
end

return _M
9 changes: 9 additions & 0 deletions kong/reports/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local cache = require "kong.tools.database_cache"

local _M = {}

function _M.execute(conf)
cache.incr(cache.requests_key(), 1)
end

return _M
20 changes: 17 additions & 3 deletions kong/tools/database_cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ local CACHE_KEYS = {
PLUGINS_CONFIGURATIONS = "plugins_configurations",
BASICAUTH_CREDENTIAL = "basicauth_credentials",
KEYAUTH_CREDENTIAL = "keyauth_credentials",
SSL = "ssl"
SSL = "ssl",
REQUESTS = "requests"
}

local _M = {}

function _M.rawset(key, value, exptime)
local cache = ngx.shared.cache
return cache:set(key, value, exptime or 0)
end

function _M.set(key, value, exptime)
if exptime == nil then
exptime = configuration and configuration.database_cache_expiration or 0
end

local cache = ngx.shared.cache
if value then
value = cjson.encode(value)
ngx.log(ngx.DEBUG, " saving cache key \""..key.."\": "..value)
end

return cache:set(key, value, exptime)
return _M.rawset(key, value, exptime)
end

function _M.get(key)
Expand All @@ -37,11 +42,20 @@ function _M.get(key)
return value, flags
end

function _M.incr(key, value)
local cache = ngx.shared.cache
return cache:incr(key, value)
end

function _M.delete(key)
local cache = ngx.shared.cache
cache:delete(key)
end

function _M.requests_key()
return CACHE_KEYS.REQUESTS
end

function _M.api_key(host)
return CACHE_KEYS.APIS.."/"..host
end
Expand Down
13 changes: 12 additions & 1 deletion kong/tools/syslog.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local constants = require "kong.constants"
local IO = require "kong.tools.io"
local stringy = require "stringy"

local _M = {}

Expand Down Expand Up @@ -40,7 +41,17 @@ function _M.log(args)
end

-- Send
IO.os_execute("nc -w1 -u "..constants.SYSLOG.ADDRESS.." "..tostring(constants.SYSLOG.PORT).." <<< \"<14>"..info.."\"")
IO.os_execute("nc -w1 -u "..constants.SYSLOG.ADDRESS.." "..tostring(constants.SYSLOG.PORT).." <<< \"<14>"..info.."\" &")
end

function _M.format_entity(t)
t.created_at = nil
for k, _ in pairs(t) do
if stringy.endswith(k, "id") then
t[k] = nil
end
end
return t
end

return _M
15 changes: 15 additions & 0 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ function _M.is_array(t)
return true
end

function _M.table_copy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[_M.table_copy(orig_key)] = _M.table_copy(orig_value)
end
setmetatable(copy, _M.table_copy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end

-- Add an error message to a key/value table.
-- Can accept a nil argument, and if is nil, will initialize the table.
-- @param `errors` (Optional) Can be nil. Table to attach the error to. If nil, the table will be created.
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/statics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ nginx: |
lua_code_cache on;
lua_max_running_timers 4096;
lua_max_pending_timers 16384;
lua_shared_dict locks 100k;
lua_shared_dict cache {{memory_cache_size}}m;
lua_socket_log_errors off;
Expand All @@ -158,6 +159,8 @@ nginx: |
end
';
init_worker_by_lua 'kong.exec_plugins_init_worker()';
server {
server_name _;
listen {{proxy_port}};
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/stub_coverage_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local IO = require "kong.tools.io"
-- Stub DAO for lapis controllers
_G.dao = {}

local lua_sources = IO.retrieve_files("./kong", { exclude_dir_patterns = {"cli", "vendor", "filelog"}, file_pattern = ".lua" })
local lua_sources = IO.retrieve_files("./kong", { exclude_dir_patterns = {"cli", "vendor", "filelog", "reports"}, file_pattern = ".lua" })

for _, source_link in ipairs(lua_sources) do
dofile(source_link)
Expand Down

0 comments on commit 0465806

Please sign in to comment.