From e1274e1117e884a426281830767a5ba4898f242d Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Wed, 19 Jan 2022 17:58:49 +0800 Subject: [PATCH] feat(acl): internal acl should support metrics(client.acl.ignore/allow/deny). --- .../src/emqx_mod_acl_internal.erl | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib-ce/emqx_modules/src/emqx_mod_acl_internal.erl b/lib-ce/emqx_modules/src/emqx_mod_acl_internal.erl index 8956229ea..0f999848e 100644 --- a/lib-ce/emqx_modules/src/emqx_mod_acl_internal.erl +++ b/lib-ce/emqx_modules/src/emqx_mod_acl_internal.erl @@ -38,12 +38,26 @@ -type(acl_rules() :: #{publish => [emqx_access_rule:rule()], subscribe => [emqx_access_rule:rule()]}). +-record(acl_metrics, { + allow = 'client.acl.allow', + deny = 'client.acl.deny', + ignore = 'client.acl.ignore' + }). + +-define(METRICS(Type), tl(tuple_to_list(#Type{}))). +-define(METRICS(Type, K), #Type{}#Type.K). + +-define(ACL_METRICS, ?METRICS(acl_metrics)). +-define(ACL_METRICS(K), ?METRICS(acl_metrics, K)). + + %%-------------------------------------------------------------------- %% API %%-------------------------------------------------------------------- load(Env) -> Rules = rules_from_file(proplists:get_value(acl_file, Env)), + register_metrics(), emqx_hooks:add('client.check_acl', {?MODULE, check_acl, [Rules]}, -1). unload(_Env) -> @@ -68,9 +82,15 @@ description() -> -> {ok, allow} | {ok, deny} | ok). check_acl(Client, PubSub, Topic, _AclResult, Rules) -> case match(Client, Topic, lookup(PubSub, Rules)) of - {matched, allow} -> {ok, allow}; - {matched, deny} -> {ok, deny}; - nomatch -> ok + {matched, allow} -> + emqx_metrics:inc(?ACL_METRICS(allow)), + {ok, allow}; + {matched, deny} -> + emqx_metrics:inc(?ACL_METRICS(deny)), + {ok, deny}; + nomatch -> + emqx_metrics:inc(?ACL_METRICS(ignore)), + ok end. %%-------------------------------------------------------------------- @@ -107,6 +127,9 @@ rules_from_file(AclFile) -> #{} end. +register_metrics() -> + lists:foreach(fun emqx_metrics:ensure/1, ?ACL_METRICS). + filter(_PubSub, {allow, all}) -> true; filter(_PubSub, {deny, all}) -> @@ -119,4 +142,3 @@ filter(subscribe, {_AllowDeny, _Who, subscribe, _Topics}) -> true; filter(_PubSub, {_AllowDeny, _Who, _, _Topics}) -> false. -