feat(message_validation): add `ignore` failure action

This commit is contained in:
Thales Macedo Garitezi 2024-03-20 14:31:43 -03:00
parent 8753e3583f
commit 4944cc080e
4 changed files with 31 additions and 2 deletions

View File

@ -383,6 +383,12 @@ run_validations(Validations, Message) ->
case run_validation(Validation, Message) of case run_validation(Validation, Message) of
ok -> ok ->
{cont, Acc}; {cont, Acc};
ignore ->
trace_failure(Validation, "validation_failed", #{
validation => Name,
action => ignore
}),
{cont, Acc};
FailureAction -> FailureAction ->
trace_failure(Validation, "validation_failed", #{ trace_failure(Validation, "validation_failed", #{
validation => Name, validation => Name,

View File

@ -75,7 +75,7 @@ fields(validation) ->
)}, )},
{failure_action, {failure_action,
mk( mk(
hoconsc:enum([drop, disconnect]), hoconsc:enum([drop, disconnect, ignore]),
#{desc => ?DESC("failure_action"), required => true} #{desc => ?DESC("failure_action"), required => true}
)}, )},
{log_failure, {log_failure,

View File

@ -573,9 +573,31 @@ t_log_failure_none(_Config) ->
), ),
ok. ok.
t_action_ignore(_Config) ->
?check_trace(
begin
Name1 = <<"foo">>,
AlwaysFailCheck = sql_check(<<"select * where false">>),
Validation1 = validation(
Name1,
[AlwaysFailCheck],
#{<<"failure_action">> => <<"ignore">>}
),
{201, _} = insert(Validation1),
C = connect(<<"c1">>),
{ok, _, [_]} = emqtt:subscribe(C, <<"t/#">>),
ok = publish(C, <<"t/1">>, #{}),
?assertReceive({publish, _}),
ok
end,
fun(Trace) ->
?assertMatch([#{action := ignore}], ?of_kind(message_validation_failure, Trace)),
ok
end
),
ok. ok.
%% Check the `all_pass' strategy %% Check the `all_pass' strategy

View File

@ -71,7 +71,8 @@ emqx_message_validation_schema {
"""How to proceed if the validation fails. """How to proceed if the validation fails.
<code>drop</code>: The offending message is simply dropped without further processing. <code>drop</code>: The offending message is simply dropped without further processing.
<code>disconnect</code>: The message is not published, and the publishing client is disconnected.""" <code>disconnect</code>: The message is not published, and the publishing client is disconnected.
<code>ignore</code>: Only the failure is logged and traced. No other action is taken."""
failure_action.label: failure_action.label:
"""Failure action""" """Failure action"""