Просмотр исходного кода

fix: make RabbitMQ error log messages easier to understand

Fixes:
https://emqx.atlassian.net/browse/EMQX-12462
Kjell Winblad 1 год назад
Родитель
Сommit
b994e0f1c0

+ 1 - 1
apps/emqx_bridge_rabbitmq/src/emqx_bridge_rabbitmq.app.src

@@ -1,6 +1,6 @@
 {application, emqx_bridge_rabbitmq, [
     {description, "EMQX Enterprise RabbitMQ Bridge"},
-    {vsn, "0.2.1"},
+    {vsn, "0.2.2"},
     {registered, []},
     {mod, {emqx_bridge_rabbitmq_app, []}},
     {applications, [

+ 24 - 1
apps/emqx_bridge_rabbitmq/src/emqx_bridge_rabbitmq_connector.erl

@@ -279,8 +279,31 @@ publish_messages(
             {error, Reason};
         %% if send a message to a non-existent exchange, RabbitMQ client will crash
         %% {shutdown,{server_initiated_close,404,<<"NOT_FOUND - no exchange 'xyz' in vhost '/'">>}
-        %% so we catch and return {recoverable_error, Reason} to increase metrics
+        %% so we catch and return a more user friendly message in that case.
+        %% This seems to happen sometimes when the exchange does not exists.
+        exit:{{shutdown, {server_initiated_close, Code, Msg}}, _InternalReason} ->
+            ?tp(emqx_bridge_rabbitmq_connector_rabbit_publish_failed_with_msg, #{}),
+            {error,
+                {recoverable_error, #{
+                    msg => <<"rabbitmq_publish_failed">>,
+                    explain => Msg,
+                    exchange => Exchange,
+                    routing_key => RoutingKey,
+                    rabbit_mq_error_code => Code
+                }}};
+        %% This probably happens when the RabbitMQ driver is restarting the connection process
+        exit:{noproc, _} = InternalError ->
+            ?tp(emqx_bridge_rabbitmq_connector_rabbit_publish_failed_con_not_ready, #{}),
+            {error,
+                {recoverable_error, #{
+                    msg => <<"rabbitmq_publish_failed">>,
+                    explain => "Connection is establishing",
+                    exchange => Exchange,
+                    routing_key => RoutingKey,
+                    internal_error => InternalError
+                }}};
         _Type:Reason ->
+            ?tp(emqx_bridge_rabbitmq_connector_rabbit_publish_failed_other, #{}),
             Msg = iolist_to_binary(io_lib:format("RabbitMQ: publish_failed: ~p", [Reason])),
             {error, {recoverable_error, Msg}}
     end.

+ 21 - 0
apps/emqx_bridge_rabbitmq/test/emqx_bridge_rabbitmq_v2_SUITE.erl

@@ -288,7 +288,9 @@ t_action_not_exist_exchange(_Config) ->
             description => <<"bridge_v2 send msg to rabbitmq action failed">>
         }
     ),
+    ok = snabbkaffe:start_trace(),
     on_exit(fun() ->
+        snabbkaffe:stop(),
         emqx_rule_engine:delete_rule(RuleId),
         _ = delete_action(Name)
     end),
@@ -316,6 +318,25 @@ t_action_not_exist_exchange(_Config) ->
     ok = delete_action(Name),
     ActionsAfterDelete = emqx_bridge_v2:list(actions),
     ?assertNot(lists:any(Any, ActionsAfterDelete), ActionsAfterDelete),
+    Trace = snabbkaffe:collect_trace(50),
+    ?assert(
+        lists:any(
+            fun(K) ->
+                maps:get(msg, K, not_found) =:=
+                    emqx_bridge_rabbitmq_connector_rabbit_publish_failed_with_msg
+            end,
+            Trace
+        )
+    ),
+    ?assert(
+        lists:any(
+            fun(K) ->
+                maps:get(msg, K, not_found) =:=
+                    emqx_bridge_rabbitmq_connector_rabbit_publish_failed_con_not_ready
+            end,
+            Trace
+        )
+    ),
     ok.
 
 t_replace_action_source(Config) ->