feat: add rule tag to rule_engine log

This commit is contained in:
zhongwencool 2024-07-17 12:15:57 +08:00
parent 9e4a84cf76
commit 604cff4887
6 changed files with 70 additions and 31 deletions

View File

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

View File

@ -134,7 +134,15 @@ republish(
},
_Args
) ->
?SLOG(error, #{msg => "recursive_republish_detected", topic => Topic});
?SLOG(
error,
#{
msg => "recursive_republish_detected",
topic => Topic,
rule_id => RuleId
},
#{tag => ?TAG}
);
republish(
Selected,
#{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),
reason => REASON,
property => K
}#{
tag => ?TAG
}
)
).

View File

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

View File

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

View File

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

View File

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