fix(metrics): counters not created before incr 'rule.matched'

This commit is contained in:
Shawn 2020-12-18 16:22:50 +08:00
parent c3159420bb
commit 86d20b060c
2 changed files with 19 additions and 4 deletions

View File

@ -163,7 +163,18 @@ inc(Id, Metric) ->
-spec inc(rule_id(), atom(), pos_integer()) -> ok.
inc(Id, Metric, Val) ->
counters:add(couters_ref(Id), metrics_idx(Metric), Val),
case couters_ref(Id) of
not_found ->
%% this may occur when increasing a counter for
%% a rule that was created from a remove node.
case atom_to_list(Metric) of
"rules." ++ _ -> create_rule_metrics(Id);
_ -> create_metrics(Id)
end,
counters:add(couters_ref(Id), metrics_idx(Metric), Val);
Ref ->
counters:add(Ref, metrics_idx(Metric), Val)
end,
inc_overall(Metric, Val).
-spec(inc_overall(atom(), pos_integer()) -> ok).

View File

@ -34,7 +34,8 @@ groups() ->
[ t_action
, t_rule
, t_clear
, t_no_creation
, t_no_creation_1
, t_no_creation_2
]},
{speed, [sequence],
[ rule_speed
@ -60,8 +61,11 @@ init_per_testcase(_, Config) ->
end_per_testcase(_, _Config) ->
ok.
t_no_creation(_) ->
?assertError(_, emqx_rule_metrics:inc_actions_taken(<<"action:0">>)).
t_no_creation_1(_) ->
?assertEqual(ok, emqx_rule_metrics:inc_rules_matched(<<"rule1">>)).
t_no_creation_2(_) ->
?assertEqual(ok, emqx_rule_metrics:inc_actions_taken(<<"action:0">>)).
t_action(_) ->
?assertEqual(0, emqx_rule_metrics:get_actions_taken(<<"action:1">>)),