-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract MappingRule from Configuration
- Loading branch information
Showing
2 changed files
with
96 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
local setmetatable = setmetatable | ||
local pairs = pairs | ||
local error = error | ||
local type = type | ||
local re_match = ngx.re.match | ||
local insert = table.insert | ||
|
||
local _M = {} | ||
|
||
local mt = { __index = _M } | ||
|
||
local function hash_to_array(hash) | ||
local array = {} | ||
|
||
for k,v in pairs(hash or {}) do | ||
insert(array, { k, v }) | ||
end | ||
|
||
return array | ||
end | ||
|
||
local function regexpify(path) | ||
return path:gsub('?.*', ''):gsub("{.-}", '([\\w_.-]+)'):gsub("%.", "\\.") | ||
end | ||
|
||
local regex_variable = '\\{[-\\w_]+\\}' | ||
|
||
local function check_querystring_params(params, args) | ||
local match = true | ||
|
||
for i=1, #params do | ||
local param = params[i][1] | ||
local expected = params[i][2] | ||
local m, err = re_match(expected, regex_variable, 'oj') | ||
local value = args[param] | ||
|
||
if m then | ||
if not value then -- regex variable have to have some value | ||
ngx.log(ngx.DEBUG, 'check query params ', param, | ||
' value missing ', expected) | ||
match = false | ||
break | ||
end | ||
else | ||
if err then ngx.log(ngx.ERR, 'check match error ', err) end | ||
|
||
-- if many values were passed use the last one | ||
if type(value) == 'table' then | ||
value = value[#value] | ||
end | ||
|
||
if value ~= expected then -- normal variables have to have exact value | ||
ngx.log(ngx.DEBUG, 'check query params does not match ', | ||
param, ' value ' , value, ' == ', expected) | ||
match = false | ||
break | ||
end | ||
end | ||
end | ||
|
||
return match | ||
end | ||
|
||
function _M.new(http_method, pattern, params, querystring_params, metric, delta) | ||
local self = setmetatable({}, mt) | ||
|
||
local querystring_parameters = hash_to_array(querystring_params) | ||
|
||
self.method = http_method | ||
self.pattern = pattern | ||
self.regexpified_pattern = regexpify(pattern) | ||
self.parameters = params | ||
self.system_name = metric or error('missing metric name of rule') | ||
self.delta = delta | ||
|
||
self.querystring_params = function(args) | ||
return check_querystring_params(querystring_parameters, args) | ||
end | ||
|
||
return self | ||
end | ||
|
||
function _M.from_proxy_rule(proxy_rule) | ||
return _M.new( | ||
proxy_rule.http_method, | ||
proxy_rule.pattern, | ||
proxy_rule.parameters, | ||
proxy_rule.querystring_parameters, | ||
proxy_rule.metric_system_name, | ||
proxy_rule.delta | ||
) | ||
end | ||
|
||
return _M |