Simplify AFD alarm field structure

The patche removes the transformation performed on the AFD table
fields: name=value -> {name='name', value='value'} -> name=value

This transformation is useless and ineffective because it allocates a table
for every field pair.
Furthermore, this prevents to access directly alarm.fields['field name']
for any further computation.

Change-Id: I7356ab5cf5975700ecf02a2a678871094a06cae9
This commit is contained in:
Swann Croiset 2016-09-16 09:09:25 +02:00
parent bb7904e30f
commit 385da2a160
6 changed files with 17 additions and 26 deletions

View File

@ -21,6 +21,7 @@ local consts = require 'gse_constants'
local read_message = read_message
local assert = assert
local ipairs = ipairs
local pairs = pairs
local pcall = pcall
local table = table
@ -47,11 +48,11 @@ end
-- for instance: "CPU load too high (WARNING, rule='last(load_midterm)>=5', current=7)"
function get_alarm_for_human(alarm)
local metric
if #(alarm.fields) > 0 then
local fields = {}
for _, field in ipairs(alarm.fields) do
fields[#fields+1] = field.name .. '="' .. field.value .. '"'
end
local fields = {}
for name, value in pairs(alarm.fields) do
fields[#fields+1] = name .. '="' .. value .. '"'
end
if #fields > 0 then
metric = string.format('%s[%s]', alarm.metric, table.concat(fields, ','))
else
metric = alarm.metric

View File

@ -124,16 +124,6 @@ function Alarm:add_value(ts, metric, value, fields)
end
end
-- convert fields to fields map
-- {foo="bar"} --> {name="foo", value="bar"}
local function convert_field_list(fields)
local named_fields = {}
for name, value in pairs(fields or {}) do
named_fields[#named_fields+1] = {name=name, value=value}
end
return named_fields
end
-- return: state of alarm and a list of alarm details.
--
-- with alarm list when state != OKAY:
@ -179,7 +169,7 @@ function Alarm:evaluate(ns)
end
for _, context in ipairs(context_list) do
add_alarm(rule, context.value, msg,
convert_field_list(context.fields))
context.fields)
end
end

View File

@ -41,7 +41,7 @@ function process_message()
afd.add_to_alarms(consts.DOWN,
'last',
metric_name,
{{name='service',value=service},{name='state',value='up'}},
{service=service,state='up'},
{},
'==',
haproxy_backend_states[service].up,
@ -54,7 +54,7 @@ function process_message()
afd.add_to_alarms(consts.CRIT,
'last',
metric_name,
{{name='service',value=service},{name='state',value='down'}},
{service=service,state='down'},
{},
'>=',
haproxy_backend_states[service].down,
@ -67,7 +67,7 @@ function process_message()
afd.add_to_alarms(consts.WARN,
'last',
metric_name,
{{name='service',value=service},{name='state',value='down'}},
{service=service,state='down'},
{},
'>',
haproxy_backend_states[service].down,

View File

@ -45,7 +45,7 @@ function process_message()
afd.add_to_alarms(consts.DOWN,
'last',
metric_name,
{{name='service',value=service},{name='state',value='up'}},
{service=service,state='up'},
{},
'==',
worker.up,
@ -58,7 +58,7 @@ function process_message()
afd.add_to_alarms(consts.CRIT,
'last',
metric_name,
{{name='service',value=service},{name='state',value='down'}},
{service=service,state='down'},
{},
'>=',
worker.down,
@ -71,7 +71,7 @@ function process_message()
afd.add_to_alarms(consts.WARN,
'last',
metric_name,
{{name='service',value=service},{name='state',value='down'}},
{service=service,state='down'},
{},
'>',
worker.down,

View File

@ -95,7 +95,7 @@ TestAfd = {}
severity='CRITICAL',
['function']='avg',
metric='fs_space_percent_free',
fields={{name='fs',value='/'}},
fields={fs='/'},
tags={},
operator='<=',
value=2,

View File

@ -685,7 +685,7 @@ function TestLMAAlarm:test_rules_fields()
local root_fs = lma_alarm.get_alarm('FS_root')
local state, result = root_fs:evaluate(t)
assertEquals(#result, 1)
assertItemsEquals(result[1].fields, {{name='fs', value='/'}})
assertItemsEquals(result[1].fields, {fs='/'})
assertEquals(result[1].value, 8)
@ -730,7 +730,7 @@ function TestLMAAlarm:test_rule_with_multivalue()
lma_alarm.add_value(next_time(), 'http_response_times', {upper_90 = 4, foo = 1}, {http_method = 'POST'})
local state, result = lma_alarm.evaluate(next_time()) -- window 60 second
assertEquals(state, consts.WARN)
assertItemsEquals(result[1].alert.fields, {{name='http_method', value='POST'}})
assertItemsEquals(result[1].alert.fields, {http_method='POST'})
assertEquals(result[1].alert.value, 6)
end
@ -781,7 +781,7 @@ function TestLMAAlarm:test_complex_field_matching_alarm_trigger()
local state, result = lma_alarm.evaluate(next_time()) -- window 60 second
assertEquals(state, consts.WARN)
assertEquals(result[1].alert.value, 6) -- the max
assertItemsEquals(result[1].alert.fields, {{name='http_method', value='POST || GET'}, {name="http_status", value="2xx || ==3xx"}})
assertItemsEquals(result[1].alert.fields, {http_method='POST || GET', http_status='2xx || ==3xx'})
end
function TestLMAAlarm:test_complex_field_matching_alarm_ok()