diff --git a/apps/emqx_auth_http/include/emqx_auth_http.hrl b/apps/emqx_auth_http/include/emqx_auth_http.hrl index b62f02bcd..0eaa59daf 100644 --- a/apps/emqx_auth_http/include/emqx_auth_http.hrl +++ b/apps/emqx_auth_http/include/emqx_auth_http.hrl @@ -1,14 +1 @@ - -define(APP, emqx_auth_http). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_http/src/emqx_auth_http.erl b/apps/emqx_auth_http/src/emqx_auth_http.erl index f97276849..98a897a8c 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http.erl @@ -30,22 +30,16 @@ ]). %% Callbacks --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 ]). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - check(ClientInfo, AuthResult, #{auth := AuthParms = #{path := Path}, super := SuperParams}) -> case authenticate(AuthParms, ClientInfo) of {ok, 200, <<"ignore">>} -> - emqx_metrics:inc(?AUTH_METRICS(ignore)), ok; + ok; {ok, 200, Body} -> - emqx_metrics:inc(?AUTH_METRICS(success)), IsSuperuser = is_superuser(SuperParams, ClientInfo), {stop, AuthResult#{is_superuser => IsSuperuser, auth_result => success, @@ -54,12 +48,10 @@ check(ClientInfo, AuthResult, #{auth := AuthParms = #{path := Path}, {ok, Code, _Body} -> ?LOG(error, "Deny connection from path: ~s, response http code: ~p", [Path, Code]), - emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => http_to_connack_error(Code), anonymous => false}}; {error, Error} -> ?LOG(error, "Request auth path: ~s, error: ~p", [Path, Error]), - emqx_metrics:inc(?AUTH_METRICS(failure)), %%FIXME later: server_unavailable is not right. {stop, AuthResult#{auth_result => server_unavailable, anonymous => false}} diff --git a/apps/emqx_auth_http/src/emqx_auth_http_app.erl b/apps/emqx_auth_http/src/emqx_auth_http_app.erl index fb23f9c0a..9e4b842d8 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_app.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_app.erl @@ -112,7 +112,6 @@ load_hooks() -> case application:get_env(?APP, auth_req) of undefined -> ok; {ok, AuthReq} -> - ok = emqx_auth_http:register_metrics(), PoolOpts = proplists:get_value(pool_opts, AuthReq), PoolName = proplists:get_value(pool_name, AuthReq), {ok, _} = ehttpc_sup:start_pool(PoolName, PoolOpts), @@ -160,4 +159,3 @@ path(#{path := ""}) -> "/"; path(#{path := Path}) -> Path. - diff --git a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl index b900f2bed..040f9b629 100644 --- a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl +++ b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl @@ -21,28 +21,11 @@ -logger_header("[JWT]"). --export([ register_metrics/0 - , check_auth/3 +-export([ check_auth/3 , check_acl/5 , description/0 ]). --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). - --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - %%-------------------------------------------------------------------- %% Authentication callbacks %%-------------------------------------------------------------------- @@ -50,17 +33,16 @@ register_metrics() -> check_auth(ClientInfo, AuthResult, #{from := From, checklists := Checklists}) -> case maps:find(From, ClientInfo) of error -> - ok = emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {ok, undefined} -> - ok = emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {ok, Token} -> case emqx_auth_jwt_svr:verify(Token) of {error, not_found} -> - ok = emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {error, not_token} -> - ok = emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {error, Reason} -> - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => Reason, anonymous => false}}; {ok, Claims} -> {stop, maps:merge(AuthResult, verify_claims(Checklists, Claims, ClientInfo))} @@ -121,10 +103,8 @@ verify_acl(ClientInfo, [AclTopic | AclTopics], Topic) -> verify_claims(Checklists, Claims, ClientInfo) -> case do_verify_claims(feedvar(Checklists, ClientInfo), Claims) of {error, Reason} -> - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), #{auth_result => Reason, anonymous => false}; ok -> - ok = emqx_metrics:inc(?AUTH_METRICS(success)), #{auth_result => success, anonymous => false, jwt_claims => Claims} end. diff --git a/apps/emqx_auth_jwt/src/emqx_auth_jwt_app.erl b/apps/emqx_auth_jwt/src/emqx_auth_jwt_app.erl index c94e8aa3c..d4e0e4960 100644 --- a/apps/emqx_auth_jwt/src/emqx_auth_jwt_app.erl +++ b/apps/emqx_auth_jwt/src/emqx_auth_jwt_app.erl @@ -32,7 +32,6 @@ start(_Type, _Args) -> {ok, Sup} = supervisor:start_link({local, ?MODULE}, ?MODULE, []), {ok, _} = start_auth_server(jwks_svr_options()), - ok = emqx_auth_jwt:register_metrics(), AuthEnv = auth_env(), _ = emqx:hook('client.authenticate', {emqx_auth_jwt, check_auth, [AuthEnv]}), diff --git a/apps/emqx_auth_ldap/include/emqx_auth_ldap.hrl b/apps/emqx_auth_ldap/include/emqx_auth_ldap.hrl index 97a7d0cfc..0c573e0e2 100644 --- a/apps/emqx_auth_ldap/include/emqx_auth_ldap.hrl +++ b/apps/emqx_auth_ldap/include/emqx_auth_ldap.hrl @@ -1,14 +1 @@ - -define(APP, emqx_auth_ldap). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_ldap/src/emqx_auth_ldap.erl b/apps/emqx_auth_ldap/src/emqx_auth_ldap.erl index f2b6140a2..da932c2fe 100644 --- a/apps/emqx_auth_ldap/src/emqx_auth_ldap.erl +++ b/apps/emqx_auth_ldap/src/emqx_auth_ldap.erl @@ -26,17 +26,12 @@ -import(emqx_auth_ldap_cli, [search/3]). --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 , prepare_filter/4 , replace_vars/2 ]). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - check(ClientInfo = #{username := Username, password := Password}, AuthResult, State = #{password_attr := PasswdAttr, bind_as_user := BindAsUserRequired, pool := Pool}) -> CheckResult = @@ -63,12 +58,10 @@ check(ClientInfo = #{username := Username, password := Password}, AuthResult, end, case CheckResult of ok -> - ok = emqx_metrics:inc(?AUTH_METRICS(success)), {stop, AuthResult#{auth_result => success, anonymous => false}}; {error, not_found} -> - emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {error, ResultCode} -> - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), ?LOG(error, "[LDAP] Auth from ldap failed: ~p", [ResultCode]), {stop, AuthResult#{auth_result => ResultCode, anonymous => false}} end. diff --git a/apps/emqx_auth_ldap/src/emqx_auth_ldap_app.erl b/apps/emqx_auth_ldap/src/emqx_auth_ldap_app.erl index 63fde463b..14c31f1bd 100644 --- a/apps/emqx_auth_ldap/src/emqx_auth_ldap_app.erl +++ b/apps/emqx_auth_ldap/src/emqx_auth_ldap_app.erl @@ -49,7 +49,6 @@ stop(_State) -> ok. load_auth_hook(DeviceDn) -> - ok = emqx_auth_ldap:register_metrics(), Params = maps:from_list(DeviceDn), emqx:hook('client.authenticate', fun emqx_auth_ldap:check/3, [Params#{pool => ?APP}]). diff --git a/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl b/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl index 2d5a6eb6d..f8569ef8a 100644 --- a/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl +++ b/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl @@ -41,15 +41,3 @@ }). -type(acl_record() :: {acl_target(), emqx_topic:topic(), action(), access(), created_at()}). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_mnesia/src/emqx_auth_mnesia.erl b/apps/emqx_auth_mnesia/src/emqx_auth_mnesia.erl index 386adba4b..e14de3cf5 100644 --- a/apps/emqx_auth_mnesia/src/emqx_auth_mnesia.erl +++ b/apps/emqx_auth_mnesia/src/emqx_auth_mnesia.erl @@ -27,7 +27,6 @@ -define(TABLE, emqx_user). %% Auth callbacks -export([ init/1 - , register_metrics/0 , check/3 , description/0 ]). @@ -51,10 +50,6 @@ init(#{clientid_list := ClientidList, username_list := UsernameList}) -> ok = ekka_mnesia:copy_table(?TABLE, disc_copies). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - hash_type() -> application:get_env(emqx_auth_mnesia, password_hash, sha256). @@ -67,17 +62,14 @@ check(ClientInfo = #{ clientid := Clientid end), case ets:select(?TABLE, MatchSpec) of [] -> - emqx_metrics:inc(?AUTH_METRICS(ignore)), ok; List -> case match_password(NPassword, HashType, List) of false -> Info = maps:without([password], ClientInfo), ?LOG(info, "[Mnesia] Auth from mnesia failed: ~p", [Info]), - emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{anonymous => false, auth_result => password_error}}; _ -> - emqx_metrics:inc(?AUTH_METRICS(success)), {stop, AuthResult#{anonymous => false, auth_result => success}} end end. diff --git a/apps/emqx_auth_mnesia/src/emqx_auth_mnesia_app.erl b/apps/emqx_auth_mnesia/src/emqx_auth_mnesia_app.erl index 467b6cf7c..ee38b7165 100644 --- a/apps/emqx_auth_mnesia/src/emqx_auth_mnesia_app.erl +++ b/apps/emqx_auth_mnesia/src/emqx_auth_mnesia_app.erl @@ -56,7 +56,6 @@ load_auth_hook() -> ClientidList = application:get_env(?APP, clientid_list, []), UsernameList = application:get_env(?APP, username_list, []), ok = emqx_auth_mnesia:init(#{clientid_list => ClientidList, username_list => UsernameList}), - ok = emqx_auth_mnesia:register_metrics(), Params = #{hash_type => emqx_auth_mnesia:hash_type()}, emqx:hook('client.authenticate', fun emqx_auth_mnesia:check/3, [Params]). diff --git a/apps/emqx_auth_mongo/include/emqx_auth_mongo.hrl b/apps/emqx_auth_mongo/include/emqx_auth_mongo.hrl index 69044e920..68a97ad33 100644 --- a/apps/emqx_auth_mongo/include/emqx_auth_mongo.hrl +++ b/apps/emqx_auth_mongo/include/emqx_auth_mongo.hrl @@ -1,4 +1,3 @@ - -define(APP, emqx_auth_mongo). -define(DEFAULT_SELECTORS, [{<<"username">>, <<"%u">>}]). @@ -14,15 +13,3 @@ -record(aclquery, {collection = <<"mqtt_acl">>, selector = {<<"username">>, <<"%u">>}}). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_mongo/src/emqx_auth_mongo.erl b/apps/emqx_auth_mongo/src/emqx_auth_mongo.erl index 0df342295..bfb911707 100644 --- a/apps/emqx_auth_mongo/src/emqx_auth_mongo.erl +++ b/apps/emqx_auth_mongo/src/emqx_auth_mongo.erl @@ -23,8 +23,7 @@ -include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/types.hrl"). --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 ]). @@ -39,20 +38,15 @@ , available/3 ]). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - check(ClientInfo = #{password := Password}, AuthResult, Env = #{authquery := AuthQuery, superquery := SuperQuery}) -> #authquery{collection = Collection, field = Fields, hash = HashType, selector = Selector} = AuthQuery, Pool = maps:get(pool, Env, ?APP), case query(Pool, Collection, maps:from_list(replvars(Selector, ClientInfo))) of - undefined -> emqx_metrics:inc(?AUTH_METRICS(ignore)); + undefined -> ok; {error, Reason} -> ?LOG(error, "[MongoDB] Can't connect to MongoDB server: ~0p", [Reason]), - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => not_authorized, anonymous => false}}; UserMap -> Result = case [maps:get(Field, UserMap, undefined) || Field <- Fields] of @@ -64,13 +58,11 @@ check(ClientInfo = #{password := Password}, AuthResult, end, case Result of ok -> - ok = emqx_metrics:inc(?AUTH_METRICS(success)), {stop, AuthResult#{is_superuser => is_superuser(Pool, SuperQuery, ClientInfo), anonymous => false, auth_result => success}}; {error, Error} -> ?LOG(error, "[MongoDB] check auth fail: ~p", [Error]), - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => Error, anonymous => false}} end end. diff --git a/apps/emqx_auth_mongo/src/emqx_auth_mongo_app.erl b/apps/emqx_auth_mongo/src/emqx_auth_mongo_app.erl index 92c025621..a63aa8193 100644 --- a/apps/emqx_auth_mongo/src/emqx_auth_mongo_app.erl +++ b/apps/emqx_auth_mongo/src/emqx_auth_mongo_app.erl @@ -68,7 +68,6 @@ safe_start() -> reg_authmod(AuthQuery) -> case emqx_auth_mongo:available(?APP, AuthQuery) of ok -> - emqx_auth_mongo:register_metrics(), HookFun = fun emqx_auth_mongo:check/3, HookOptions = #{authquery => AuthQuery, superquery => undefined, pool => ?APP}, case r(super_query, application:get_env(?APP, super_query, undefined)) of @@ -122,4 +121,3 @@ r(auth_query, Config) -> r(acl_query, Config) -> #aclquery{collection = list_to_binary(get_value(collection, Config, "mqtt_acl")), selector = get_value(selector, Config, [?DEFAULT_SELECTORS])}. - diff --git a/apps/emqx_auth_mysql/include/emqx_auth_mysql.hrl b/apps/emqx_auth_mysql/include/emqx_auth_mysql.hrl index 56da35401..b7c185fcf 100644 --- a/apps/emqx_auth_mysql/include/emqx_auth_mysql.hrl +++ b/apps/emqx_auth_mysql/include/emqx_auth_mysql.hrl @@ -1,14 +1 @@ - -define(APP, emqx_auth_mysql). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_mysql/src/emqx_auth_mysql.erl b/apps/emqx_auth_mysql/src/emqx_auth_mysql.erl index 17d618838..31d9a007f 100644 --- a/apps/emqx_auth_mysql/src/emqx_auth_mysql.erl +++ b/apps/emqx_auth_mysql/src/emqx_auth_mysql.erl @@ -22,17 +22,12 @@ -include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/types.hrl"). --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 ]). -define(EMPTY(Username), (Username =:= undefined orelse Username =:= <<>>)). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - check(ClientInfo = #{password := Password}, AuthResult, #{auth_query := {AuthSql, AuthParams}, super_query := SuperQuery, @@ -51,15 +46,13 @@ check(ClientInfo = #{password := Password}, AuthResult, end, case CheckPass of ok -> - emqx_metrics:inc(?AUTH_METRICS(success)), {stop, AuthResult#{is_superuser => is_superuser(Pool, SuperQuery, ClientInfo), anonymous => false, auth_result => success}}; {error, not_found} -> - emqx_metrics:inc(?AUTH_METRICS(ignore)), ok; + ok; {error, ResultCode} -> ?LOG(error, "[MySQL] Auth from mysql failed: ~p", [ResultCode]), - emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => ResultCode, anonymous => false}} end. @@ -88,4 +81,3 @@ check_pass(Password, HashType) -> end. description() -> "Authentication with MySQL". - diff --git a/apps/emqx_auth_mysql/src/emqx_auth_mysql_app.erl b/apps/emqx_auth_mysql/src/emqx_auth_mysql_app.erl index 716cb7a7a..e58f62a90 100644 --- a/apps/emqx_auth_mysql/src/emqx_auth_mysql_app.erl +++ b/apps/emqx_auth_mysql/src/emqx_auth_mysql_app.erl @@ -50,7 +50,6 @@ stop(_State) -> ok. load_auth_hook(AuthQuery) -> - ok = emqx_auth_mysql:register_metrics(), SuperQuery = parse_query(application:get_env(?APP, super_query, undefined)), {ok, HashType} = application:get_env(?APP, password_hash), Params = #{auth_query => AuthQuery, diff --git a/apps/emqx_auth_pgsql/include/emqx_auth_pgsql.hrl b/apps/emqx_auth_pgsql/include/emqx_auth_pgsql.hrl index 92b971667..565aa4668 100644 --- a/apps/emqx_auth_pgsql/include/emqx_auth_pgsql.hrl +++ b/apps/emqx_auth_pgsql/include/emqx_auth_pgsql.hrl @@ -1,13 +1 @@ -define(APP, emqx_auth_pgsql). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_pgsql/src/emqx_auth_pgsql.erl b/apps/emqx_auth_pgsql/src/emqx_auth_pgsql.erl index f38552085..f673e07e4 100644 --- a/apps/emqx_auth_pgsql/src/emqx_auth_pgsql.erl +++ b/apps/emqx_auth_pgsql/src/emqx_auth_pgsql.erl @@ -21,15 +21,10 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 ]). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - %%-------------------------------------------------------------------- %% Auth Module Callbacks %%-------------------------------------------------------------------- @@ -50,15 +45,13 @@ check(ClientInfo = #{password := Password}, AuthResult, end, case CheckPass of ok -> - emqx_metrics:inc(?AUTH_METRICS(success)), {stop, AuthResult#{is_superuser => is_superuser(Pool, SuperQuery, ClientInfo), anonymous => false, auth_result => success}}; {error, not_found} -> - emqx_metrics:inc(?AUTH_METRICS(ignore)), ok; + ok; {error, ResultCode} -> ?LOG(error, "[Postgres] Auth from pgsql failed: ~p", [ResultCode]), - emqx_metrics:inc(?AUTH_METRICS(failure)), {stop, AuthResult#{auth_result => ResultCode, anonymous => false}} end. @@ -88,4 +81,3 @@ check_pass(Password, HashType) -> end. description() -> "Authentication with PostgreSQL". - diff --git a/apps/emqx_auth_pgsql/src/emqx_auth_pgsql_app.erl b/apps/emqx_auth_pgsql/src/emqx_auth_pgsql_app.erl index c658c4d11..dbfeb8423 100644 --- a/apps/emqx_auth_pgsql/src/emqx_auth_pgsql_app.erl +++ b/apps/emqx_auth_pgsql/src/emqx_auth_pgsql_app.erl @@ -42,7 +42,6 @@ start(_StartType, _StartArgs) -> super_query => SuperQuery, hash_type => HashType, pool => ?APP}, - ok = emqx_auth_pgsql:register_metrics(), ok = emqx:hook('client.authenticate', fun emqx_auth_pgsql:check/3, [AuthEnv]) end), if_enabled(acl_query, fun(AclQuery) -> @@ -59,4 +58,3 @@ if_enabled(Par, Fun) -> {ok, Query} -> Fun(parse_query(Par, Query)); undefined -> ok end. - diff --git a/apps/emqx_auth_redis/include/emqx_auth_redis.hrl b/apps/emqx_auth_redis/include/emqx_auth_redis.hrl index fe488c37f..075d649ec 100644 --- a/apps/emqx_auth_redis/include/emqx_auth_redis.hrl +++ b/apps/emqx_auth_redis/include/emqx_auth_redis.hrl @@ -1,14 +1 @@ - -define(APP, emqx_auth_redis). - --record(auth_metrics, { - success = 'client.auth.success', - failure = 'client.auth.failure', - ignore = 'client.auth.ignore' - }). - --define(METRICS(Type), tl(tuple_to_list(#Type{}))). --define(METRICS(Type, K), #Type{}#Type.K). - --define(AUTH_METRICS, ?METRICS(auth_metrics)). --define(AUTH_METRICS(K), ?METRICS(auth_metrics, K)). diff --git a/apps/emqx_auth_redis/src/emqx_auth_redis.erl b/apps/emqx_auth_redis/src/emqx_auth_redis.erl index 04d3542f0..d432e012b 100644 --- a/apps/emqx_auth_redis/src/emqx_auth_redis.erl +++ b/apps/emqx_auth_redis/src/emqx_auth_redis.erl @@ -21,15 +21,10 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). --export([ register_metrics/0 - , check/3 +-export([ check/3 , description/0 ]). --spec(register_metrics() -> ok). -register_metrics() -> - lists:foreach(fun emqx_metrics:ensure/1, ?AUTH_METRICS). - check(ClientInfo = #{password := Password}, AuthResult, #{auth_cmd := AuthCmd, super_cmd := SuperCmd, @@ -52,15 +47,13 @@ check(ClientInfo = #{password := Password}, AuthResult, end, case CheckPass of ok -> - ok = emqx_metrics:inc(?AUTH_METRICS(success)), IsSuperuser = is_superuser(Pool, Type, SuperCmd, ClientInfo, Timeout), {stop, AuthResult#{is_superuser => IsSuperuser, anonymous => false, auth_result => success}}; {error, not_found} -> - ok = emqx_metrics:inc(?AUTH_METRICS(ignore)); + ok; {error, ResultCode} -> - ok = emqx_metrics:inc(?AUTH_METRICS(failure)), ?LOG(error, "[Redis] Auth from redis failed: ~p", [ResultCode]), {stop, AuthResult#{auth_result => ResultCode, anonymous => false}} end. @@ -82,4 +75,3 @@ check_pass(Password, HashType) -> ok -> ok; {error, _Reason} -> {error, not_authorized} end. - diff --git a/apps/emqx_auth_redis/src/emqx_auth_redis_app.erl b/apps/emqx_auth_redis/src/emqx_auth_redis_app.erl index c6de8b80a..cbea448f9 100644 --- a/apps/emqx_auth_redis/src/emqx_auth_redis_app.erl +++ b/apps/emqx_auth_redis/src/emqx_auth_redis_app.erl @@ -49,7 +49,6 @@ load_auth_hook(AuthCmd) -> timeout => Timeout, type => Type, pool => ?APP}, - ok = emqx_auth_redis:register_metrics(), emqx:hook('client.authenticate', fun emqx_auth_redis:check/3, [Config]). load_acl_hook(AclCmd) -> @@ -66,4 +65,3 @@ if_cmd_enabled(Par, Fun) -> {ok, Cmd} -> Fun(Cmd); undefined -> ok end. - diff --git a/apps/emqx_exproto/src/emqx_exproto_channel.erl b/apps/emqx_exproto/src/emqx_exproto_channel.erl index 0e39b40a7..dce3b36e1 100644 --- a/apps/emqx_exproto/src/emqx_exproto_channel.erl +++ b/apps/emqx_exproto/src/emqx_exproto_channel.erl @@ -299,8 +299,6 @@ handle_call({auth, RequestedClientInfo, Password}, case emqx_access_control:authenticate(ClientInfo1#{password => Password}) of {ok, AuthResult} -> emqx_logger:set_metadata_clientid(ClientId), - is_anonymous(AuthResult) andalso - emqx_metrics:inc('client.auth.anonymous'), NClientInfo = maps:merge(ClientInfo1, AuthResult), NChannel = Channel1#channel{clientinfo = NClientInfo}, case emqx_cm:open_session(true, NClientInfo, NConnInfo) of @@ -424,9 +422,6 @@ terminate(Reason, Channel) -> Req = #{reason => stringfy(Reason)}, try_dispatch(on_socket_closed, wrap(Req), Channel). -is_anonymous(#{anonymous := true}) -> true; -is_anonymous(_AuthResult) -> false. - packet_to_message(Topic, Qos, Payload, #channel{ conninfo = #{proto_ver := ProtoVer}, diff --git a/apps/emqx_prometheus/src/emqx_prometheus.erl b/apps/emqx_prometheus/src/emqx_prometheus.erl index 57c54446f..a7c450ba6 100644 --- a/apps/emqx_prometheus/src/emqx_prometheus.erl +++ b/apps/emqx_prometheus/src/emqx_prometheus.erl @@ -412,8 +412,8 @@ emqx_collect(emqx_client_connected, Stats) -> counter_metric(?C('client.connected', Stats)); emqx_collect(emqx_client_authenticate, Stats) -> counter_metric(?C('client.authenticate', Stats)); -emqx_collect(emqx_client_auth_anonymous, Stats) -> - counter_metric(?C('client.auth.anonymous', Stats)); +emqx_collect(emqx_client_auth_success_anonymous, Stats) -> + counter_metric(?C('client.auth.success.anonymous', Stats)); emqx_collect(emqx_client_check_acl, Stats) -> counter_metric(?C('client.check_acl', Stats)); emqx_collect(emqx_client_subscribe, Stats) -> @@ -566,7 +566,7 @@ emqx_metrics_delivery() -> emqx_metrics_client() -> [ emqx_client_connected , emqx_client_authenticate - , emqx_client_auth_anonymous + , emqx_client_auth_success_anonymous , emqx_client_check_acl , emqx_client_subscribe , emqx_client_unsubscribe