diff --git a/src/emqx_metrics.erl b/src/emqx_metrics.erl index 7bec83465..c919e25eb 100644 --- a/src/emqx_metrics.erl +++ b/src/emqx_metrics.erl @@ -429,8 +429,12 @@ inc_sent(Packet) -> do_inc_sent(?CONNACK_PACKET(ReasonCode)) -> (ReasonCode == ?RC_SUCCESS) orelse inc('packets.connack.error'), - (ReasonCode == ?RC_NOT_AUTHORIZED) andalso inc('packets.connack.auth_error'), - (ReasonCode == ?RC_BAD_USER_NAME_OR_PASSWORD) andalso inc('packets.connack.auth_error'), + ((ReasonCode == ?RC_NOT_AUTHORIZED) + orelse (ReasonCode == ?CONNACK_AUTH)) + andalso inc('packets.connack.auth_error'), + ((ReasonCode == ?RC_BAD_USER_NAME_OR_PASSWORD) + orelse (ReasonCode == ?CONNACK_CREDENTIALS)) + andalso inc('packets.connack.auth_error'), inc('packets.connack.sent'); do_inc_sent(?PUBLISH_PACKET(QoS)) -> diff --git a/test/emqx_broker_SUITE.erl b/test/emqx_broker_SUITE.erl index e8d19c5c0..ec46ff8e1 100644 --- a/test/emqx_broker_SUITE.erl +++ b/test/emqx_broker_SUITE.erl @@ -277,6 +277,38 @@ t_stats_fun({'end', _Config}) -> ok = emqx_broker:unsubscribe(<<"topic">>), ok = emqx_broker:unsubscribe(<<"topic2">>). +t_connack_auth_error({init, Config}) -> + process_flag(trap_exit, true), + emqx_ct_helpers:stop_apps([]), + emqx_ct_helpers:boot_modules(all), + Handler = + fun(emqx) -> + application:set_env(emqx, acl_nomatch, deny), + application:set_env(emqx, allow_anonymous, false), + application:set_env(emqx, enable_acl_cache, false), + ok; + (_) -> + ok + end, + emqx_ct_helpers:start_apps([], Handler), + Config; +t_connack_auth_error({'end', _Config}) -> + emqx_ct_helpers:stop_apps([]), + emqx_ct_helpers:boot_modules(all), + emqx_ct_helpers:start_apps([]), + ok; +t_connack_auth_error(Config) when is_list(Config) -> + %% MQTT 3.1 + ?assertEqual(0, emqx_metrics:val('packets.connack.auth_error')), + {ok, C0} = emqtt:start_link([{proto_ver, v4}]), + ?assertEqual({error, {unauthorized_client, undefined}}, emqtt:connect(C0)), + ?assertEqual(1, emqx_metrics:val('packets.connack.auth_error')), + %% MQTT 5.0 + {ok, C1} = emqtt:start_link([{proto_ver, v5}]), + ?assertEqual({error, {not_authorized, #{}}}, emqtt:connect(C1)), + ?assertEqual(2, emqx_metrics:val('packets.connack.auth_error')), + ok. + recv_msgs(Count) -> recv_msgs(Count, []).