Merge pull request #13480 from zhongwencool/rule-engine-log-tag

feat: Rule engine log tag
This commit is contained in:
zhongwencool 2024-07-22 16:47:13 +08:00 committed by GitHub
commit a2bed1efb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 70 additions and 31 deletions

View File

@ -128,3 +128,4 @@
-define(KEY_PATH, [rule_engine, rules]). -define(KEY_PATH, [rule_engine, rules]).
-define(RULE_PATH(RULE), [rule_engine, rules, RULE]). -define(RULE_PATH(RULE), [rule_engine, rules, RULE]).
-define(TAG, "RULE").

View File

@ -134,7 +134,15 @@ republish(
}, },
_Args _Args
) -> ) ->
?SLOG(error, #{msg => "recursive_republish_detected", topic => Topic}); ?SLOG(
error,
#{
msg => "recursive_republish_detected",
topic => Topic,
rule_id => RuleId
},
#{tag => ?TAG}
);
republish( republish(
Selected, Selected,
#{metadata := #{rule_id := RuleId}} = Env, #{metadata := #{rule_id := RuleId}} = Env,
@ -311,6 +319,8 @@ render_pub_props(UserPropertiesTemplate, Selected, Env) ->
rule_id => emqx_utils_maps:deep_get([metadata, rule_id], ENV, undefined), rule_id => emqx_utils_maps:deep_get([metadata, rule_id], ENV, undefined),
reason => REASON, reason => REASON,
property => K property => K
}#{
tag => ?TAG
} }
) )
). ).

View File

@ -21,6 +21,7 @@
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-include_lib("hocon/include/hoconsc.hrl"). -include_lib("hocon/include/hoconsc.hrl").
-include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/logger.hrl").
-include("rule_engine.hrl").
-export([check_params/2]). -export([check_params/2]).
@ -36,10 +37,14 @@ check_params(Params, Tag) ->
#{Tag := Checked} -> {ok, Checked} #{Tag := Checked} -> {ok, Checked}
catch catch
throw:Reason -> throw:Reason ->
?SLOG(error, #{ ?SLOG(
msg => "check_rule_params_failed", info,
reason => Reason #{
}), msg => "check_rule_params_failed",
reason => Reason
},
#{tag => ?TAG}
),
{error, Reason} {error, Reason}
end. end.

View File

@ -437,15 +437,15 @@ handle_call({delete_rule, Rule}, _From, State) ->
ok = do_delete_rule(Rule), ok = do_delete_rule(Rule),
{reply, ok, State}; {reply, ok, State};
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->
?SLOG(error, #{msg => "unexpected_call", request => Req}), ?SLOG(error, #{msg => "unexpected_call", request => Req}, #{tag => ?TAG}),
{reply, ignored, State}. {reply, ignored, State}.
handle_cast(Msg, State) -> handle_cast(Msg, State) ->
?SLOG(error, #{msg => "unexpected_cast", request => Msg}), ?SLOG(error, #{msg => "unexpected_cast", request => Msg}, #{tag => ?TAG}),
{noreply, State}. {noreply, State}.
handle_info(Info, State) -> handle_info(Info, State) ->
?SLOG(error, #{msg => "unexpected_info", request => Info}), ?SLOG(error, #{msg => "unexpected_info", request => Info}, #{tag => ?TAG}),
{noreply, State}. {noreply, State}.
terminate(_Reason, _State) -> terminate(_Reason, _State) ->

View File

@ -388,11 +388,15 @@ param_path_id() ->
{ok, #{post_config_update := #{emqx_rule_engine := Rule}}} -> {ok, #{post_config_update := #{emqx_rule_engine := Rule}}} ->
{201, format_rule_info_resp(Rule)}; {201, format_rule_info_resp(Rule)};
{error, Reason} -> {error, Reason} ->
?SLOG(error, #{ ?SLOG(
msg => "create_rule_failed", info,
id => Id, #{
reason => Reason msg => "create_rule_failed",
}), rule_id => Id,
reason => Reason
},
#{tag => ?TAG}
),
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}} {400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
end end
end end
@ -448,11 +452,15 @@ param_path_id() ->
{ok, #{post_config_update := #{emqx_rule_engine := Rule}}} -> {ok, #{post_config_update := #{emqx_rule_engine := Rule}}} ->
{200, format_rule_info_resp(Rule)}; {200, format_rule_info_resp(Rule)};
{error, Reason} -> {error, Reason} ->
?SLOG(error, #{ ?SLOG(
msg => "update_rule_failed", info,
id => Id, #{
reason => Reason msg => "update_rule_failed",
}), rule_id => Id,
reason => Reason
},
#{tag => ?TAG}
),
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}} {400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
end; end;
'/rules/:id'(delete, #{bindings := #{id := Id}}) -> '/rules/:id'(delete, #{bindings := #{id := Id}}) ->
@ -463,11 +471,15 @@ param_path_id() ->
{ok, _} -> {ok, _} ->
{204}; {204};
{error, Reason} -> {error, Reason} ->
?SLOG(error, #{ ?SLOG(
msg => "delete_rule_failed", error,
id => Id, #{
reason => Reason msg => "delete_rule_failed",
}), rule_id => Id,
reason => Reason
},
#{tag => ?TAG}
),
{500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}} {500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}}
end; end;
not_found -> not_found ->
@ -587,10 +599,15 @@ get_rule_metrics(Id) ->
NodeMetrics = [format_metrics(Node, Metrics) || {Node, {ok, Metrics}} <- NodeResults], NodeMetrics = [format_metrics(Node, Metrics) || {Node, {ok, Metrics}} <- NodeResults],
NodeErrors = [Result || Result = {_Node, {NOk, _}} <- NodeResults, NOk =/= ok], NodeErrors = [Result || Result = {_Node, {NOk, _}} <- NodeResults, NOk =/= ok],
NodeErrors == [] orelse NodeErrors == [] orelse
?SLOG(warning, #{ ?SLOG(
msg => "rpc_get_rule_metrics_errors", warning,
errors => NodeErrors #{
}), msg => "rpc_get_rule_metrics_errors",
rule_id => Id,
errors => NodeErrors
},
#{tag => ?TAG}
),
NodeMetrics. NodeMetrics.
format_metrics(Node, #{ format_metrics(Node, #{

View File

@ -15,6 +15,7 @@
-module(emqx_rule_sqltester). -module(emqx_rule_sqltester).
-include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/logger.hrl").
-include("rule_engine.hrl").
-export([ -export([
test/1, test/1,
@ -127,10 +128,15 @@ test(#{sql := Sql, context := Context}) ->
end end
end; end;
{error, Reason} -> {error, Reason} ->
?SLOG(debug, #{ ?SLOG(
msg => "rulesql_parse_error", debug,
detail => Reason #{
}), msg => "rulesql_parse_error",
sql => Sql,
reason => Reason
},
#{tag => ?TAG}
),
{error, Reason} {error, Reason}
end. end.