fix: rename the rule metrics

This commit is contained in:
Shawn 2022-05-17 17:17:07 +08:00
parent d1ffba7ff4
commit c98fd04f8f
6 changed files with 64 additions and 64 deletions

View File

@ -645,14 +645,14 @@ t_ingress_mqtt_bridge_with_rules(_) ->
#{ #{
<<"id">> := RuleId, <<"id">> := RuleId,
<<"metrics">> := #{ <<"metrics">> := #{
<<"sql.matched">> := 1, <<"matched">> := 1,
<<"sql.passed">> := 1, <<"passed">> := 1,
<<"sql.failed">> := 0, <<"failed">> := 0,
<<"sql.failed.exception">> := 0, <<"failed.exception">> := 0,
<<"sql.failed.no_result">> := 0, <<"failed.no_result">> := 0,
<<"sql.matched.rate">> := _, <<"matched.rate">> := _,
<<"sql.matched.rate.max">> := _, <<"matched.rate.max">> := _,
<<"sql.matched.rate.last5m">> := _, <<"matched.rate.last5m">> := _,
<<"outputs.total">> := 1, <<"outputs.total">> := 1,
<<"outputs.success">> := 1, <<"outputs.success">> := 1,
<<"outputs.failed">> := 0, <<"outputs.failed">> := 0,
@ -752,14 +752,14 @@ t_egress_mqtt_bridge_with_rules(_) ->
#{ #{
<<"id">> := RuleId, <<"id">> := RuleId,
<<"metrics">> := #{ <<"metrics">> := #{
<<"sql.matched">> := 1, <<"matched">> := 1,
<<"sql.passed">> := 1, <<"passed">> := 1,
<<"sql.failed">> := 0, <<"failed">> := 0,
<<"sql.failed.exception">> := 0, <<"failed.exception">> := 0,
<<"sql.failed.no_result">> := 0, <<"failed.no_result">> := 0,
<<"sql.matched.rate">> := _, <<"matched.rate">> := _,
<<"sql.matched.rate.max">> := _, <<"matched.rate.max">> := _,
<<"sql.matched.rate.last5m">> := _, <<"matched.rate.last5m">> := _,
<<"outputs.total">> := 1, <<"outputs.total">> := 1,
<<"outputs.success">> := 1, <<"outputs.success">> := 1,
<<"outputs.failed">> := 0, <<"outputs.failed">> := 0,

View File

@ -453,7 +453,7 @@ emqx_rule_api_schema {
metrics_outputs_total { metrics_outputs_total {
desc { desc {
en: "How much times the outputs are called by the rule. This value may several times of 'sql.matched', depending on the number of the outputs of the rule." en: "How much times the outputs are called by the rule. This value may several times of 'matched', depending on the number of the outputs of the rule."
zh: "规则调用输出的次数。 该值可能是“sql.matched”的几倍具体取决于规则输出的数量。" zh: "规则调用输出的次数。 该值可能是“sql.matched”的几倍具体取决于规则输出的数量。"
} }
label: { label: {

View File

@ -103,24 +103,24 @@ fields("rule_test") ->
]; ];
fields("metrics") -> fields("metrics") ->
[ [
{"sql.matched", {"matched",
sc(non_neg_integer(), #{ sc(non_neg_integer(), #{
desc => ?DESC("metrics_sql_matched") desc => ?DESC("metrics_sql_matched")
})}, })},
{"sql.matched.rate", sc(float(), #{desc => ?DESC("metrics_sql_matched_rate")})}, {"matched.rate", sc(float(), #{desc => ?DESC("metrics_sql_matched_rate")})},
{"sql.matched.rate.max", sc(float(), #{desc => ?DESC("metrics_sql_matched_rate_max")})}, {"matched.rate.max", sc(float(), #{desc => ?DESC("metrics_sql_matched_rate_max")})},
{"sql.matched.rate.last5m", {"matched.rate.last5m",
sc( sc(
float(), float(),
#{desc => ?DESC("metrics_sql_matched_rate_last5m")} #{desc => ?DESC("metrics_sql_matched_rate_last5m")}
)}, )},
{"sql.passed", sc(non_neg_integer(), #{desc => ?DESC("metrics_sql_passed")})}, {"passed", sc(non_neg_integer(), #{desc => ?DESC("metrics_sql_passed")})},
{"sql.failed", sc(non_neg_integer(), #{desc => ?DESC("metrics_sql_failed")})}, {"failed", sc(non_neg_integer(), #{desc => ?DESC("metrics_sql_failed")})},
{"sql.failed.exception", {"failed.exception",
sc(non_neg_integer(), #{ sc(non_neg_integer(), #{
desc => ?DESC("metrics_sql_failed_exception") desc => ?DESC("metrics_sql_failed_exception")
})}, })},
{"sql.failed.unknown", {"failed.unknown",
sc(non_neg_integer(), #{ sc(non_neg_integer(), #{
desc => ?DESC("metrics_sql_failed_unknown") desc => ?DESC("metrics_sql_failed_unknown")
})}, })},

View File

@ -83,11 +83,11 @@
%% NOTE: This order cannot be changed! This is to make the metric working during relup. %% NOTE: This order cannot be changed! This is to make the metric working during relup.
%% Append elements to this list to add new metrics. %% Append elements to this list to add new metrics.
-define(METRICS, [ -define(METRICS, [
'sql.matched', 'matched',
'sql.passed', 'passed',
'sql.failed', 'failed',
'sql.failed.exception', 'failed.exception',
'sql.failed.no_result', 'failed.no_result',
'outputs.total', 'outputs.total',
'outputs.success', 'outputs.success',
'outputs.failed', 'outputs.failed',
@ -95,7 +95,7 @@
'outputs.failed.unknown' 'outputs.failed.unknown'
]). ]).
-define(RATE_METRICS, ['sql.matched']). -define(RATE_METRICS, ['matched']).
config_key_path() -> config_key_path() ->
[rule_engine, rules]. [rule_engine, rules].

View File

@ -60,19 +60,19 @@ end).
RATE_5 RATE_5
), ),
#{ #{
'sql.matched' => MATCH, 'matched' => MATCH,
'sql.passed' => PASS, 'passed' => PASS,
'sql.failed' => FAIL, 'failed' => FAIL,
'sql.failed.exception' => FAIL_EX, 'failed.exception' => FAIL_EX,
'sql.failed.no_result' => FAIL_NORES, 'failed.no_result' => FAIL_NORES,
'outputs.total' => O_TOTAL, 'outputs.total' => O_TOTAL,
'outputs.failed' => O_FAIL, 'outputs.failed' => O_FAIL,
'outputs.failed.out_of_service' => O_FAIL_OOS, 'outputs.failed.out_of_service' => O_FAIL_OOS,
'outputs.failed.unknown' => O_FAIL_UNKNOWN, 'outputs.failed.unknown' => O_FAIL_UNKNOWN,
'outputs.success' => O_SUCC, 'outputs.success' => O_SUCC,
'sql.matched.rate' => RATE, 'matched.rate' => RATE,
'sql.matched.rate.max' => RATE_MAX, 'matched.rate.max' => RATE_MAX,
'sql.matched.rate.last5m' => RATE_5 'matched.rate.last5m' => RATE_5
} }
). ).
-define(metrics( -define(metrics(
@ -91,19 +91,19 @@ end).
RATE_5 RATE_5
), ),
#{ #{
'sql.matched' := MATCH, 'matched' := MATCH,
'sql.passed' := PASS, 'passed' := PASS,
'sql.failed' := FAIL, 'failed' := FAIL,
'sql.failed.exception' := FAIL_EX, 'failed.exception' := FAIL_EX,
'sql.failed.no_result' := FAIL_NORES, 'failed.no_result' := FAIL_NORES,
'outputs.total' := O_TOTAL, 'outputs.total' := O_TOTAL,
'outputs.failed' := O_FAIL, 'outputs.failed' := O_FAIL,
'outputs.failed.out_of_service' := O_FAIL_OOS, 'outputs.failed.out_of_service' := O_FAIL_OOS,
'outputs.failed.unknown' := O_FAIL_UNKNOWN, 'outputs.failed.unknown' := O_FAIL_UNKNOWN,
'outputs.success' := O_SUCC, 'outputs.success' := O_SUCC,
'sql.matched.rate' := RATE, 'matched.rate' := RATE,
'sql.matched.rate.max' := RATE_MAX, 'matched.rate.max' := RATE_MAX,
'sql.matched.rate.last5m' := RATE_5 'matched.rate.last5m' := RATE_5
} }
). ).
@ -406,11 +406,11 @@ get_rule_metrics(Id) ->
#{ #{
counters := counters :=
#{ #{
'sql.matched' := Matched, 'matched' := Matched,
'sql.passed' := Passed, 'passed' := Passed,
'sql.failed' := Failed, 'failed' := Failed,
'sql.failed.exception' := FailedEx, 'failed.exception' := FailedEx,
'sql.failed.no_result' := FailedNoRes, 'failed.no_result' := FailedNoRes,
'outputs.total' := OTotal, 'outputs.total' := OTotal,
'outputs.failed' := OFailed, 'outputs.failed' := OFailed,
'outputs.failed.out_of_service' := OFailedOOS, 'outputs.failed.out_of_service' := OFailedOOS,
@ -419,7 +419,7 @@ get_rule_metrics(Id) ->
}, },
rate := rate :=
#{ #{
'sql.matched' := 'matched' :=
#{current := Current, max := Max, last5m := Last5M} #{current := Current, max := Max, last5m := Last5M}
} }
} }

View File

@ -64,14 +64,14 @@ apply_rule_discard_result(Rule, Input) ->
ok. ok.
apply_rule(Rule = #{id := RuleID}, Input) -> apply_rule(Rule = #{id := RuleID}, Input) ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.matched'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'matched'),
clear_rule_payload(), clear_rule_payload(),
try try
do_apply_rule(Rule, add_metadata(Input, #{rule_id => RuleID})) do_apply_rule(Rule, add_metadata(Input, #{rule_id => RuleID}))
catch catch
%% ignore the errors if select or match failed %% ignore the errors if select or match failed
_:Reason = {select_and_transform_error, Error} -> _:Reason = {select_and_transform_error, Error} ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'failed.exception'),
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "SELECT_clause_exception", msg => "SELECT_clause_exception",
rule_id => RuleID, rule_id => RuleID,
@ -79,7 +79,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
}), }),
{error, Reason}; {error, Reason};
_:Reason = {match_conditions_error, Error} -> _:Reason = {match_conditions_error, Error} ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'failed.exception'),
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "WHERE_clause_exception", msg => "WHERE_clause_exception",
rule_id => RuleID, rule_id => RuleID,
@ -87,7 +87,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
}), }),
{error, Reason}; {error, Reason};
_:Reason = {select_and_collect_error, Error} -> _:Reason = {select_and_collect_error, Error} ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'failed.exception'),
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "FOREACH_clause_exception", msg => "FOREACH_clause_exception",
rule_id => RuleID, rule_id => RuleID,
@ -95,7 +95,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
}), }),
{error, Reason}; {error, Reason};
_:Reason = {match_incase_error, Error} -> _:Reason = {match_incase_error, Error} ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'failed.exception'),
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "INCASE_clause_exception", msg => "INCASE_clause_exception",
rule_id => RuleID, rule_id => RuleID,
@ -103,7 +103,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
}), }),
{error, Reason}; {error, Reason};
Class:Error:StkTrace -> Class:Error:StkTrace ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'), ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'failed.exception'),
?SLOG(error, #{ ?SLOG(error, #{
msg => "apply_rule_failed", msg => "apply_rule_failed",
rule_id => RuleID, rule_id => RuleID,
@ -141,13 +141,13 @@ do_apply_rule(
Collection2 = filter_collection(Input, InCase, DoEach, Collection), Collection2 = filter_collection(Input, InCase, DoEach, Collection),
case Collection2 of case Collection2 of
[] -> [] ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result'); ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result');
_ -> _ ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.passed') ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'passed')
end, end,
{ok, [handle_output_list(RuleId, Outputs, Coll, Input) || Coll <- Collection2]}; {ok, [handle_output_list(RuleId, Outputs, Coll, Input) || Coll <- Collection2]};
false -> false ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result'), ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result'),
{error, nomatch} {error, nomatch}
end; end;
do_apply_rule( do_apply_rule(
@ -171,10 +171,10 @@ do_apply_rule(
) )
of of
true -> true ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.passed'), ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'passed'),
{ok, handle_output_list(RuleId, Outputs, Selected, Input)}; {ok, handle_output_list(RuleId, Outputs, Selected, Input)};
false -> false ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result'), ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result'),
{error, nomatch} {error, nomatch}
end. end.