Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring #43

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b0b9bdc
first steps of new version
ekho Jul 12, 2016
5e015e7
first steps of new version
ekho Jul 13, 2016
af61c5a
+ vhosts
ekho Jul 13, 2016
36e53cf
vhosts spec
ekho Jul 13, 2016
ae1b5b5
* luacov config
ekho Jul 13, 2016
207c8c5
* minor fix in specs
ekho Jul 13, 2016
053f130
+ logger
ekho Jul 13, 2016
60f127f
* logger
ekho Jul 14, 2016
9187dec
+ storage
ekho Jul 13, 2016
880a857
* storage
ekho Jul 13, 2016
769f17e
+ pre-commit
ekho Jul 13, 2016
c123919
pre-commit hook updated
ekho Jul 14, 2016
a79ebf3
+ serializer
ekho Jul 14, 2016
3c157db
* vhosts fixed
ekho Jul 14, 2016
f253f33
* main module correctly handles errors and disables itself
ekho Jul 14, 2016
254b65c
* storage
ekho Jul 14, 2016
47f6d9b
* storage
ekho Jul 14, 2016
25aef92
* storage
ekho Jul 14, 2016
ec26ef8
+ scheduler and aggregator
ekho Jul 15, 2016
090a9d3
+ scheduler and aggregator
ekho Jul 15, 2016
338f3e8
+ scheduler and aggregator
ekho Jul 15, 2016
db07544
+ scheduler and aggregator
ekho Jul 15, 2016
514fb03
* scheduler tests
ekho Jul 18, 2016
6c607e4
+ validator
ekho Jul 20, 2016
0de5fd4
* scheduler tests
ekho Jul 21, 2016
d1e6fcc
* scheduler tests
ekho Jul 21, 2016
7abb726
* scheduler tests fixed
ekho Jul 21, 2016
f8ec0e9
* scheduler tests
ekho Jul 21, 2016
e29a75b
* validator added to coverage and rockspec
ekho Jul 21, 2016
dcea88f
* validator tests
ekho Jul 22, 2016
7c38801
* tests
ekho Jul 22, 2016
224e1a4
* scheduler locking to prevent unnecessary executions
ekho Aug 2, 2016
169c705
+ collectors manager
ekho Aug 4, 2016
f5d8018
* collectors manager
ekho Aug 4, 2016
2a58dc7
* collectors manager
ekho Aug 4, 2016
88bef18
worker_id moved to logger
ekho Aug 4, 2016
28b428b
worker_id moved to logger
ekho Aug 4, 2016
d6ff751
specs
ekho Aug 5, 2016
c5d489f
specs
ekho Aug 5, 2016
3ffa256
specs
ekho Aug 5, 2016
7f1428a
collectors
ekho Aug 11, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .busted
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
return {
_all = {
coverage = true,
ROOT = { "tests" },
ROOT = { "spec" },
['sort-files'] = true,
},
default = {
Expand Down
5 changes: 2 additions & 3 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
stds.fun = require 'fun'
stds.is = require 'nginx-metrix.lib.is'

std = 'max+luajit+fun+ngx_lua'

files['tests'] = { std = "+busted" }
files['spec'] = { std = "+busted" }

new_read_globals = {
'__TEST__',
'is',
'copy',
'print_inspected',
}
33 changes: 10 additions & 23 deletions .luacov
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
return {
include = {
'^[.]/nginx[-]metrix/'
'^[.]/src/'
},
modules = {
['nginx-metrix.main'] = 'nginx-metrix/main.lua',

['nginx-metrix.collectors'] = 'nginx-metrix/collectors.lua',
['nginx-metrix.listener'] = 'nginx-metrix/listener.lua',
['nginx-metrix.logger'] = 'nginx-metrix/logger.lua',
['nginx-metrix.scheduler'] = 'nginx-metrix/scheduler.lua',
['nginx-metrix.version'] = 'nginx-metrix/version.lua',

['nginx-metrix.lib.is'] = 'nginx-metrix/lib/is.lua',

['nginx-metrix.collectors.request'] = 'nginx-metrix/collectors/request.lua',
['nginx-metrix.collectors.status'] = 'nginx-metrix/collectors/status.lua',
['nginx-metrix.collectors.upstream'] = 'nginx-metrix/collectors/upstream.lua',

['nginx-metrix.output.helper'] = 'nginx-metrix/output/helper.lua',
['nginx-metrix.output.renderer'] = 'nginx-metrix/output/renderer.lua',

['nginx-metrix.storage.collector_wrapper_factory'] = 'nginx-metrix/storage/collector_wrapper_factory.lua',
['nginx-metrix.storage.dict'] = 'nginx-metrix/storage/dict.lua',
['nginx-metrix.storage.namespaces'] = 'nginx-metrix/storage/namespaces.lua',
['nginx-metrix.storage.serializer'] = 'nginx-metrix/storage/serializer.lua',
['nginx-metrix.storage.window'] = 'nginx-metrix/storage/window.lua',
['nginx-metrix'] = 'src/nginx-metrix.lua',
['nginx-metrix.aggregator'] = 'src/nginx-metrix/aggregator.lua',
['nginx-metrix.collectors'] = 'src/nginx-metrix/collectors.lua',
['nginx-metrix.logger'] = 'src/nginx-metrix/logger.lua',
['nginx-metrix.scheduler'] = 'src/nginx-metrix/scheduler.lua',
['nginx-metrix.serializer'] = 'src/nginx-metrix/serializer.lua',
['nginx-metrix.storage'] = 'src/nginx-metrix/storage.lua',
['nginx-metrix.validator'] = 'src/nginx-metrix/validator.lua',
['nginx-metrix.vhosts'] = 'src/nginx-metrix/vhosts.lua',
},
}
19 changes: 19 additions & 0 deletions pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

files=$(git diff --cached --name-only $against -- '*.lua')

[ -z "${files}" ] && exit 0

eval $(luarocks path --bin)

exec luacheck ${files}
49 changes: 0 additions & 49 deletions rockspecs/nginx-metrix-1.1.2-1.rockspec

This file was deleted.

2 changes: 1 addition & 1 deletion rockspecs/nginx-metrix-2.0.dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = {
build = {
type = "builtin",
modules = {
['nginx-metrix.main'] = 'nginx-metrix/main.lua',
['nginx-metrix.main'] = 'nginx-metrix/nginx-metrix.lua',

['nginx-metrix.collectors'] = 'nginx-metrix/collectors.lua',
['nginx-metrix.listener'] = 'nginx-metrix/listener.lua',
Expand Down
46 changes: 18 additions & 28 deletions rockspecs/nginx-metrix-local-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
package = 'nginx-metrix'

version = 'local-1'

source = {
url = "file://nginx-metrix/",
url = "file://src/",
dir = "nginx-metrix",
}

description = {
summary = 'Extended Nginx status and metrics.',
detailed = [[Extended Nginx status and metrics.]],
homepage = 'https://github.com/bankiru/nginx-metrix/',
license = 'MIT <http://opensource.org/licenses/MIT>',
summary = 'Extended Nginx status and metrics.',
detailed = [[Extended Nginx status and metrics.]],
homepage = 'https://github.com/bankiru/nginx-metrix/',
license = 'MIT <http://opensource.org/licenses/MIT>',
maintainer = 'Boris Gorbylev <ekho@ekho.name>',
}

dependencies = {
'lua ~> 5.1',
'lpeg',
'dkjson',
'fun-alloyed',
'inspect',
'lust',
}

build = {
type = "builtin",
modules = {
['nginx-metrix.main'] = 'nginx-metrix/main.lua',

['nginx-metrix.collectors'] = 'nginx-metrix/collectors.lua',
['nginx-metrix.listener'] = 'nginx-metrix/listener.lua',
['nginx-metrix.logger'] = 'nginx-metrix/logger.lua',
['nginx-metrix.scheduler'] = 'nginx-metrix/scheduler.lua',
['nginx-metrix.version'] = 'nginx-metrix/version.lua',

['nginx-metrix.lib.is'] = 'nginx-metrix/lib/is.lua',

['nginx-metrix.collectors.request'] = 'nginx-metrix/collectors/request.lua',
['nginx-metrix.collectors.status'] = 'nginx-metrix/collectors/status.lua',
['nginx-metrix.collectors.upstream'] = 'nginx-metrix/collectors/upstream.lua',

['nginx-metrix.output.helper'] = 'nginx-metrix/output/helper.lua',
['nginx-metrix.output.renderer'] = 'nginx-metrix/output/renderer.lua',

['nginx-metrix.storage.collector_wrapper_factory'] = 'nginx-metrix/storage/collector_wrapper_factory.lua',
['nginx-metrix.storage.dict'] = 'nginx-metrix/storage/dict.lua',
['nginx-metrix.storage.namespaces'] = 'nginx-metrix/storage/namespaces.lua',
['nginx-metrix.storage.serializer'] = 'nginx-metrix/storage/serializer.lua',
['nginx-metrix.storage.window'] = 'nginx-metrix/storage/window.lua',
['nginx-metrix'] = 'src/nginx-metrix.lua',
['nginx-metrix.aggregator'] = 'src/nginx-metrix/aggregator.lua',
['nginx-metrix.collectors'] = 'src/nginx-metrix/collectors.lua',
['nginx-metrix.logger'] = 'src/nginx-metrix/logger.lua',
['nginx-metrix.scheduler'] = 'src/nginx-metrix/scheduler.lua',
['nginx-metrix.serializer'] = 'src/nginx-metrix/serializer.lua',
['nginx-metrix.storage'] = 'src/nginx-metrix/storage.lua',
['nginx-metrix.validator'] = 'src/nginx-metrix/validator.lua',
['nginx-metrix.vhosts'] = 'src/nginx-metrix/vhosts.lua',
},
}
2 changes: 1 addition & 1 deletion rockspecs/nginx-metrix-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = {
build = {
type = "builtin",
modules = {
['nginx-metrix.main'] = 'nginx-metrix/main.lua',
['nginx-metrix.main'] = 'nginx-metrix/nginx-metrix.lua',

['nginx-metrix.collectors'] = 'nginx-metrix/collectors.lua',
['nginx-metrix.listener'] = 'nginx-metrix/listener.lua',
Expand Down
151 changes: 151 additions & 0 deletions spec/bootstrap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
_G.__TEST__ = true

require 'nginx-metrix.lib.is'()

local copy
copy = function(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
_G.copy = copy

_G.print_inspected = function(...)
print(require 'inspect'(...))
end

local s = require('say') --our i18n lib, installed through luarocks, included as a luassert dependency

local M = {
assertions = {},
matchers = {},
}

local function table_includes(container, contained)
if container == contained then return true end
local t1, t2 = type(container), type(contained)
if t1 ~= t2 then return false end

if t1 == 'table' then
for k, v in pairs(contained) do
if not table_includes(container[k], v) then return false end
end
return true
end
return false
end

local function table_contains(t, element)
if t then
for _, value in pairs(t) do
if type(value) == type(element) then
if type(element) == 'table' then
-- if we wanted recursive items content comparison, we could use
-- table_equals(v, expected) but one level of just comparing
-- items is sufficient
if M.assertions.table_equals(nil, { value, element }) then
return true
end
else
if value == element then
return true
end
end
end
end
end
return false
end


local function table_equals(actual, expected)
if type(actual) == 'table' and type(expected) == 'table' then
for _, v in pairs(actual) do
if not table_contains(expected, v) then
return false
end
end
for _, v in pairs(expected) do
if not table_contains(actual, v) then
return false
end
end
return true
elseif type(actual) ~= type(expected) then
return false
elseif actual == expected then
return true
end
return false
end

local function is_callable(obj)
return type(obj) == 'function' or getmetatable(obj) and getmetatable(obj).__call and true
end

local function json_equal(json1, json2)
local json = require 'dkjson'
local util = require 'luassert.util'

if type(json1) == 'string' then
json1 = json.decode(json1)
end

if type(json2) == 'string' then
json2 = json.decode(json2)
end

return util.deepcompare(json1, json2, true)
end

s:set("assertion.fail.positive", "%s")
s:set("assertion.fail.negative", "%s")
M.assertions['fail'] = function(_) return false end

s:set("assertion.table_contains.positive", "Expected %s\n to contain \n%s")
s:set("assertion.table_contains.negative", "Expected %s\n to NOT contain \n%s")
M.assertions['table_contains'] = function(_, arguments) return table_contains(arguments[1], arguments[2]) end

s:set("assertion.table_equals.positive", "Expected %s\n to be equal with %s")
s:set("assertion.table_equals.negative", "Expected %s\n to be NOT equal with %s")
M.assertions['table_equals'] = function(_, arguments) return table_equals(arguments[1], arguments[2]) end

s:set("assertion.table_includes.positive", "Expected %s\n to include \n%s")
s:set("assertion.table_includes.negative", "Expected %s\n to NOT include \n%s")
M.assertions['table_includes'] = function(_, arguments) return table_includes(arguments[1], arguments[2]) end

s:set("assertion.is_callable.positive", "Expected %s to be callable")
s:set("assertion.is_callable.negative", "Expected %s to be NOT callable")
M.assertions['is_callable'] = function(_, arguments) return is_callable(arguments[1]) end

s:set("assertion.json_equal.positive", "Expected %s\n to be equal with %s")
s:set("assertion.json_equal.negative", "Expected %s\n to be NOT equal with %s")
M.assertions['json_equal'] = function(_, arguments) return json_equal(arguments[1], arguments[2]) end

M.matchers["json_equal"] = function(_, arguments)
return function(value)
local is_eq = json_equal(value, arguments[1])
if not is_eq then
print_inspected{ json1 = value, json2 = arguments[1] }
end
return is_eq
end
end

-- a special syntax sugar to export all functions to the global table
setmetatable(M, {
__call = function(t, assert)
for a_name, a_func in pairs(t.assertions) do
assert:register("assertion", a_name, a_func, "assertion." .. a_name .. ".positive", "assertion." .. a_name .. ".negative")
end
for a_name, a_func in pairs(t.matchers) do
assert:register("matcher", a_name, a_func)
end
end,
})

return M
Loading