refactor: move emqx_plugin_libs_metrics to emqx app
because it can not depend on other apps
This commit is contained in:
parent
725c96dc2f
commit
4e65322667
|
@ -582,7 +582,7 @@ handle_delete_authenticator(Chain, AuthenticatorID) ->
|
|||
[] ->
|
||||
{error, {not_found, {authenticator, AuthenticatorID}}};
|
||||
[AuthenticatorID] ->
|
||||
emqx_plugin_libs_metrics:clear_metrics(authn_metrics, AuthenticatorID),
|
||||
emqx_metrics_worker:clear_metrics(authn_metrics, AuthenticatorID),
|
||||
ok
|
||||
end.
|
||||
|
||||
|
@ -613,7 +613,7 @@ handle_create_authenticator(Chain, Config, Providers) ->
|
|||
Chain#chain{authenticators = NAuthenticators}
|
||||
),
|
||||
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(
|
||||
ok = emqx_metrics_worker:create_metrics(
|
||||
authn_metrics,
|
||||
AuthenticatorID,
|
||||
[matched, success, failed, ignore],
|
||||
|
@ -628,10 +628,10 @@ handle_create_authenticator(Chain, Config, Providers) ->
|
|||
do_authenticate([], _) ->
|
||||
{stop, {error, not_authorized}};
|
||||
do_authenticate([#authenticator{id = ID, provider = Provider, state = State} | More], Credential) ->
|
||||
emqx_plugin_libs_metrics:inc(authn_metrics, ID, matched),
|
||||
emqx_metrics_worker:inc(authn_metrics, ID, matched),
|
||||
try Provider:authenticate(Credential, State) of
|
||||
ignore ->
|
||||
ok = emqx_plugin_libs_metrics:inc(authn_metrics, ID, ignore),
|
||||
ok = emqx_metrics_worker:inc(authn_metrics, ID, ignore),
|
||||
do_authenticate(More, Credential);
|
||||
Result ->
|
||||
%% {ok, Extra}
|
||||
|
@ -641,9 +641,9 @@ do_authenticate([#authenticator{id = ID, provider = Provider, state = State} | M
|
|||
%% {error, Reason}
|
||||
case Result of
|
||||
{ok, _} ->
|
||||
emqx_plugin_libs_metrics:inc(authn_metrics, ID, success);
|
||||
emqx_metrics_worker:inc(authn_metrics, ID, success);
|
||||
{error, _} ->
|
||||
emqx_plugin_libs_metrics:inc(authn_metrics, ID, failed);
|
||||
emqx_metrics_worker:inc(authn_metrics, ID, failed);
|
||||
_ ->
|
||||
ok
|
||||
end,
|
||||
|
@ -657,7 +657,7 @@ do_authenticate([#authenticator{id = ID, provider = Provider, state = State} | M
|
|||
stacktrace => Stacktrace,
|
||||
authenticator => ID
|
||||
}),
|
||||
emqx_plugin_libs_metrics:inc(authn_metrics, ID, ignore),
|
||||
emqx_metrics_worker:inc(authn_metrics, ID, ignore),
|
||||
do_authenticate(More, Credential)
|
||||
end.
|
||||
|
||||
|
|
|
@ -26,8 +26,8 @@ start_link() ->
|
|||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
init([]) ->
|
||||
AuthnMetrics = emqx_plugin_libs_metrics:child_spec(emqx_authn_metrics, authn_metrics),
|
||||
AuthzMetrics = emqx_plugin_libs_metrics:child_spec(eqmx_authz_metrics, authz_metrics),
|
||||
AuthnMetrics = emqx_metrics_worker:child_spec(emqx_authn_metrics, authn_metrics),
|
||||
AuthzMetrics = emqx_metrics_worker:child_spec(eqmx_authz_metrics, authz_metrics),
|
||||
{ok,
|
||||
{
|
||||
{one_for_one, 10, 100},
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(emqx_plugin_libs_metrics).
|
||||
-module(emqx_metrics_worker).
|
||||
|
||||
-behaviour(gen_server).
|
||||
|
||||
|
@ -100,16 +100,16 @@
|
|||
|
||||
-spec child_spec(handler_name()) -> supervisor:child_spec().
|
||||
child_spec(Name) ->
|
||||
child_spec(emqx_plugin_libs_metrics, Name).
|
||||
child_spec(emqx_metrics_worker, Name).
|
||||
|
||||
child_spec(ChldName, Name) ->
|
||||
#{
|
||||
id => ChldName,
|
||||
start => {emqx_plugin_libs_metrics, start_link, [Name]},
|
||||
start => {emqx_metrics_worker, start_link, [Name]},
|
||||
restart => permanent,
|
||||
shutdown => 5000,
|
||||
type => worker,
|
||||
modules => [emqx_plugin_libs_metrics]
|
||||
modules => [emqx_metrics_worker]
|
||||
}.
|
||||
|
||||
-spec create_metrics(handler_name(), metric_id(), [atom()]) -> ok | {error, term()}.
|
||||
|
@ -284,7 +284,15 @@ terminate(_Reason, #state{metric_ids = MIDs}) ->
|
|||
persistent_term:erase(?CntrRef(Name)).
|
||||
|
||||
stop(Name) ->
|
||||
gen_server:stop(Name).
|
||||
try
|
||||
gen_server:stop(Name)
|
||||
catch
|
||||
exit:noproc ->
|
||||
ok;
|
||||
exit:timeout ->
|
||||
%% after timeout, the process killed by gen.erl
|
||||
ok
|
||||
end.
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% Internal Functions
|
|
@ -14,7 +14,7 @@
|
|||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(emqx_plugin_libs_metrics_SUITE).
|
||||
-module(emqx_metrics_worker_SUITE).
|
||||
|
||||
-compile(export_all).
|
||||
-compile(nowarn_export_all).
|
||||
|
@ -31,18 +31,15 @@ suite() ->
|
|||
-define(NAME, ?MODULE).
|
||||
|
||||
init_per_suite(Config) ->
|
||||
emqx_common_test_helpers:start_apps([emqx_conf]),
|
||||
{ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
|
||||
{ok, _} = emqx_metrics_worker:start_link(?NAME),
|
||||
Config.
|
||||
|
||||
end_per_suite(_Config) ->
|
||||
catch emqx_plugin_libs_metrics:stop(?NAME),
|
||||
emqx_common_test_helpers:stop_apps([emqx_conf]),
|
||||
ok.
|
||||
ok = emqx_metrics_worker:stop(?NAME).
|
||||
|
||||
init_per_testcase(_, Config) ->
|
||||
catch emqx_plugin_libs_metrics:stop(?NAME),
|
||||
{ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
|
||||
ok = emqx_metrics_worker:stop(?NAME),
|
||||
{ok, _} = emqx_metrics_worker:start_link(?NAME),
|
||||
Config.
|
||||
|
||||
end_per_testcase(_, _Config) ->
|
||||
|
@ -50,7 +47,7 @@ end_per_testcase(_, _Config) ->
|
|||
|
||||
t_get_metrics(_) ->
|
||||
Metrics = [a, b, c],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"testid">>, Metrics),
|
||||
%% all the metrics are set to zero at start
|
||||
?assertMatch(
|
||||
#{
|
||||
|
@ -65,12 +62,12 @@ t_get_metrics(_) ->
|
|||
c := 0
|
||||
}
|
||||
},
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>)
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
ct:sleep(1500),
|
||||
?LET(
|
||||
#{
|
||||
|
@ -85,7 +82,7 @@ t_get_metrics(_) ->
|
|||
c := 2
|
||||
}
|
||||
},
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>),
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>),
|
||||
{
|
||||
?assert(CurrA > 0),
|
||||
?assert(CurrB > 0),
|
||||
|
@ -95,11 +92,11 @@ t_get_metrics(_) ->
|
|||
?assert(MaxC > 0)
|
||||
}
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_reset_metrics(_) ->
|
||||
Metrics = [a, b, c],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"testid">>, Metrics),
|
||||
%% all the metrics are set to zero at start
|
||||
?assertMatch(
|
||||
#{
|
||||
|
@ -114,14 +111,14 @@ t_reset_metrics(_) ->
|
|||
c := 0
|
||||
}
|
||||
},
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>)
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
ct:sleep(1500),
|
||||
ok = emqx_plugin_libs_metrics:reset_metrics(?NAME, <<"testid">>),
|
||||
ok = emqx_metrics_worker:reset_metrics(?NAME, <<"testid">>),
|
||||
?LET(
|
||||
#{
|
||||
rate := #{
|
||||
|
@ -135,7 +132,7 @@ t_reset_metrics(_) ->
|
|||
c := 0
|
||||
}
|
||||
},
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>),
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>),
|
||||
{
|
||||
?assert(CurrA == 0),
|
||||
?assert(CurrB == 0),
|
||||
|
@ -145,19 +142,19 @@ t_reset_metrics(_) ->
|
|||
?assert(MaxC == 0)
|
||||
}
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_get_metrics_2(_) ->
|
||||
Metrics = [a, b, c],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(
|
||||
ok = emqx_metrics_worker:create_metrics(
|
||||
?NAME,
|
||||
<<"testid">>,
|
||||
Metrics,
|
||||
[a]
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
?assertMatch(
|
||||
#{
|
||||
rate := Rate = #{
|
||||
|
@ -169,13 +166,13 @@ t_get_metrics_2(_) ->
|
|||
c := 1
|
||||
}
|
||||
} when map_size(Rate) =:= 1,
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>)
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_recreate_metrics(_) ->
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a]),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"testid">>, [a]),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, a),
|
||||
?assertMatch(
|
||||
#{
|
||||
rate := R = #{
|
||||
|
@ -185,12 +182,12 @@ t_recreate_metrics(_) ->
|
|||
a := 1
|
||||
}
|
||||
} when map_size(R) == 1 andalso map_size(C) == 1,
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>)
|
||||
),
|
||||
%% we create the metrics again, to add some counters
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a, b, c]),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"testid">>, [a, b, c]),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, b),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"testid">>, c),
|
||||
?assertMatch(
|
||||
#{
|
||||
rate := R = #{
|
||||
|
@ -202,42 +199,42 @@ t_recreate_metrics(_) ->
|
|||
a := 1, b := 1, c := 1
|
||||
}
|
||||
} when map_size(R) == 3 andalso map_size(C) == 3,
|
||||
emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)
|
||||
emqx_metrics_worker:get_metrics(?NAME, <<"testid">>)
|
||||
),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_inc_matched(_) ->
|
||||
Metrics = ['rules.matched'],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, Metrics),
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule2">>, Metrics),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
|
||||
?assertEqual(1, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
|
||||
?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule2">>, 'rules.matched')),
|
||||
?assertEqual(0, emqx_plugin_libs_metrics:get(?NAME, <<"rule3">>, 'rules.matched')),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule2">>).
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"rule1">>, Metrics),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"rule2">>, Metrics),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule2">>, 'rules.matched'),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule2">>, 'rules.matched'),
|
||||
?assertEqual(1, emqx_metrics_worker:get(?NAME, <<"rule1">>, 'rules.matched')),
|
||||
?assertEqual(2, emqx_metrics_worker:get(?NAME, <<"rule2">>, 'rules.matched')),
|
||||
?assertEqual(0, emqx_metrics_worker:get(?NAME, <<"rule3">>, 'rules.matched')),
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"rule1">>),
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"rule2">>).
|
||||
|
||||
t_rate(_) ->
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, ['rules.matched']),
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule:2">>, ['rules.matched']),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule:2">>, 'rules.matched'),
|
||||
?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"rule1">>, ['rules.matched']),
|
||||
ok = emqx_metrics_worker:create_metrics(?NAME, <<"rule:2">>, ['rules.matched']),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule1">>, 'rules.matched'),
|
||||
ok = emqx_metrics_worker:inc(?NAME, <<"rule:2">>, 'rules.matched'),
|
||||
?assertEqual(2, emqx_metrics_worker:get(?NAME, <<"rule1">>, 'rules.matched')),
|
||||
ct:sleep(1000),
|
||||
?LET(
|
||||
#{'rules.matched' := #{max := Max, current := Current}},
|
||||
emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
|
||||
emqx_metrics_worker:get_rate(?NAME, <<"rule1">>),
|
||||
{?assert(Max =< 2), ?assert(Current =< 2)}
|
||||
),
|
||||
ct:sleep(2100),
|
||||
?LET(
|
||||
#{'rules.matched' := #{max := Max, current := Current, last5m := Last5Min}},
|
||||
emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
|
||||
emqx_metrics_worker:get_rate(?NAME, <<"rule1">>),
|
||||
{?assert(Max =< 2), ?assert(Current == 0), ?assert(Last5Min =< 0.67)}
|
||||
),
|
||||
ct:sleep(3000),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule:2">>).
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"rule1">>),
|
||||
ok = emqx_metrics_worker:clear_metrics(?NAME, <<"rule:2">>).
|
|
@ -943,7 +943,7 @@ lookup_from_local_node(ChainName, AuthenticatorID) ->
|
|||
NodeId = node(self()),
|
||||
case emqx_authentication:lookup_authenticator(ChainName, AuthenticatorID) of
|
||||
{ok, #{provider := Provider, state := State}} ->
|
||||
Metrics = emqx_plugin_libs_metrics:get_metrics(authn_metrics, AuthenticatorID),
|
||||
Metrics = emqx_metrics_worker:get_metrics(authn_metrics, AuthenticatorID),
|
||||
case lists:member(Provider, resource_provider()) of
|
||||
false ->
|
||||
{ok, {NodeId, connected, Metrics, #{}}};
|
||||
|
|
|
@ -31,8 +31,8 @@ introduced_in() ->
|
|||
|
||||
-spec get_metrics(
|
||||
node(),
|
||||
emqx_plugin_libs_metrics:handler_name(),
|
||||
emqx_plugin_libs_metrics:metric_id()
|
||||
) -> emqx_plugin_libs_metrics:metrics() | {badrpc, _}.
|
||||
emqx_metrics_worker:handler_name(),
|
||||
emqx_metrics_worker:metric_id()
|
||||
) -> emqx_metrics_worker:metrics() | {badrpc, _}.
|
||||
get_metrics(Node, HandlerName, MetricId) ->
|
||||
rpc:call(Node, emqx_plugin_libs_metrics, get_metrics, [HandlerName, MetricId]).
|
||||
rpc:call(Node, emqx_metrics_worker, get_metrics, [HandlerName, MetricId]).
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
config := resource_config(),
|
||||
state := resource_state(),
|
||||
status := resource_connection_status(),
|
||||
metrics := emqx_plugin_libs_metrics:metrics()
|
||||
metrics := emqx_metrics_worker:metrics()
|
||||
}.
|
||||
-type resource_group() :: binary().
|
||||
-type create_opts() :: #{
|
||||
|
|
|
@ -250,12 +250,12 @@ query(InstId, Request, AfterQuery) ->
|
|||
{ok, _Group, #{mod := Mod, state := ResourceState, status := connected}} ->
|
||||
%% the resource state is readonly to Module:on_query/4
|
||||
%% and the `after_query()` functions should be thread safe
|
||||
ok = emqx_plugin_libs_metrics:inc(resource_metrics, InstId, matched),
|
||||
ok = emqx_metrics_worker:inc(resource_metrics, InstId, matched),
|
||||
try
|
||||
Mod:on_query(InstId, Request, AfterQuery, ResourceState)
|
||||
catch
|
||||
Err:Reason:ST ->
|
||||
emqx_plugin_libs_metrics:inc(resource_metrics, InstId, exception),
|
||||
emqx_metrics_worker:inc(resource_metrics, InstId, exception),
|
||||
erlang:raise(Err, Reason, ST)
|
||||
end;
|
||||
{error, not_found} ->
|
||||
|
@ -418,8 +418,8 @@ filter_instances(Filter) ->
|
|||
[Id || #{id := Id, mod := Mod} <- list_instances_verbose(), Filter(Id, Mod)].
|
||||
|
||||
inc_metrics_funcs(InstId) ->
|
||||
OnFailed = [{fun emqx_plugin_libs_metrics:inc/3, [resource_metrics, InstId, failed]}],
|
||||
OnSucc = [{fun emqx_plugin_libs_metrics:inc/3, [resource_metrics, InstId, success]}],
|
||||
OnFailed = [{fun emqx_metrics_worker:inc/3, [resource_metrics, InstId, failed]}],
|
||||
OnSucc = [{fun emqx_metrics_worker:inc/3, [resource_metrics, InstId, success]}],
|
||||
{OnSucc, OnFailed}.
|
||||
|
||||
call_instance(InstId, Query) ->
|
||||
|
|
|
@ -82,10 +82,10 @@ make_test_id() ->
|
|||
<<?TEST_ID_PREFIX, RandId/binary>>.
|
||||
|
||||
get_metrics(InstId) ->
|
||||
emqx_plugin_libs_metrics:get_metrics(resource_metrics, InstId).
|
||||
emqx_metrics_worker:get_metrics(resource_metrics, InstId).
|
||||
|
||||
reset_metrics(InstId) ->
|
||||
emqx_plugin_libs_metrics:reset_metrics(resource_metrics, InstId).
|
||||
emqx_metrics_worker:reset_metrics(resource_metrics, InstId).
|
||||
|
||||
force_lookup(InstId) ->
|
||||
{ok, _Group, Data} = lookup(InstId),
|
||||
|
@ -200,7 +200,7 @@ do_create(InstId, Group, ResourceType, Config, Opts) ->
|
|||
{ok, already_created};
|
||||
{error, not_found} ->
|
||||
ok = do_start(InstId, Group, ResourceType, Config, Opts),
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(
|
||||
ok = emqx_metrics_worker:create_metrics(
|
||||
resource_metrics,
|
||||
InstId,
|
||||
[matched, success, failed, exception],
|
||||
|
@ -243,7 +243,7 @@ do_remove(Group, #{id := InstId} = Data, ClearMetrics) ->
|
|||
_ = do_stop(Group, Data),
|
||||
ets:delete(emqx_resource_instance, InstId),
|
||||
case ClearMetrics of
|
||||
true -> ok = emqx_plugin_libs_metrics:clear_metrics(resource_metrics, InstId);
|
||||
true -> ok = emqx_metrics_worker:clear_metrics(resource_metrics, InstId);
|
||||
false -> ok
|
||||
end,
|
||||
ok.
|
||||
|
|
|
@ -33,7 +33,7 @@ init([]) ->
|
|||
_ = ets:new(emqx_resource_instance, TabOpts),
|
||||
|
||||
SupFlags = #{strategy => one_for_one, intensity => 10, period => 10},
|
||||
Metrics = emqx_plugin_libs_metrics:child_spec(resource_metrics),
|
||||
Metrics = emqx_metrics_worker:child_spec(resource_metrics),
|
||||
|
||||
Pool = ?RESOURCE_INST_MOD,
|
||||
Mod = ?RESOURCE_INST_MOD,
|
||||
|
|
|
@ -214,19 +214,19 @@ load_hooks_for_rule(#{from := Topics}) ->
|
|||
lists:foreach(fun emqx_rule_events:load/1, Topics).
|
||||
|
||||
maybe_add_metrics_for_rule(Id) ->
|
||||
case emqx_plugin_libs_metrics:has_metrics(rule_metrics, Id) of
|
||||
case emqx_metrics_worker:has_metrics(rule_metrics, Id) of
|
||||
true ->
|
||||
ok;
|
||||
false ->
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(rule_metrics, Id, ?METRICS, ?RATE_METRICS)
|
||||
ok = emqx_metrics_worker:create_metrics(rule_metrics, Id, ?METRICS, ?RATE_METRICS)
|
||||
end.
|
||||
|
||||
clear_metrics_for_rule(Id) ->
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(rule_metrics, Id).
|
||||
ok = emqx_metrics_worker:clear_metrics(rule_metrics, Id).
|
||||
|
||||
-spec reset_metrics_for_rule(rule_id()) -> ok.
|
||||
reset_metrics_for_rule(Id) ->
|
||||
emqx_plugin_libs_metrics:reset_metrics(rule_metrics, Id).
|
||||
emqx_metrics_worker:reset_metrics(rule_metrics, Id).
|
||||
|
||||
unload_hooks_for_rule(#{id := Id, from := Topics}) ->
|
||||
lists:foreach(
|
||||
|
|
|
@ -36,5 +36,5 @@ init([]) ->
|
|||
type => worker,
|
||||
modules => [emqx_rule_engine]
|
||||
},
|
||||
Metrics = emqx_plugin_libs_metrics:child_spec(rule_metrics),
|
||||
Metrics = emqx_metrics_worker:child_spec(rule_metrics),
|
||||
{ok, {{one_for_one, 10, 10}, [Registry, Metrics]}}.
|
||||
|
|
|
@ -64,14 +64,14 @@ apply_rule_discard_result(Rule, Input) ->
|
|||
ok.
|
||||
|
||||
apply_rule(Rule = #{id := RuleID}, Input) ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.matched'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.matched'),
|
||||
clear_rule_payload(),
|
||||
try
|
||||
do_apply_rule(Rule, add_metadata(Input, #{rule_id => RuleID}))
|
||||
catch
|
||||
%% ignore the errors if select or match failed
|
||||
_:Reason = {select_and_transform_error, Error} ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
?SLOG(warning, #{
|
||||
msg => "SELECT_clause_exception",
|
||||
rule_id => RuleID,
|
||||
|
@ -79,7 +79,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
|
|||
}),
|
||||
{error, Reason};
|
||||
_:Reason = {match_conditions_error, Error} ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
?SLOG(warning, #{
|
||||
msg => "WHERE_clause_exception",
|
||||
rule_id => RuleID,
|
||||
|
@ -87,7 +87,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
|
|||
}),
|
||||
{error, Reason};
|
||||
_:Reason = {select_and_collect_error, Error} ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
?SLOG(warning, #{
|
||||
msg => "FOREACH_clause_exception",
|
||||
rule_id => RuleID,
|
||||
|
@ -95,7 +95,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
|
|||
}),
|
||||
{error, Reason};
|
||||
_:Reason = {match_incase_error, Error} ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
?SLOG(warning, #{
|
||||
msg => "INCASE_clause_exception",
|
||||
rule_id => RuleID,
|
||||
|
@ -103,7 +103,7 @@ apply_rule(Rule = #{id := RuleID}, Input) ->
|
|||
}),
|
||||
{error, Reason};
|
||||
Class:Error:StkTrace ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleID, 'sql.failed.exception'),
|
||||
?SLOG(error, #{
|
||||
msg => "apply_rule_failed",
|
||||
rule_id => RuleID,
|
||||
|
@ -141,13 +141,13 @@ do_apply_rule(
|
|||
Collection2 = filter_collection(Input, InCase, DoEach, Collection),
|
||||
case Collection2 of
|
||||
[] ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'sql.failed.no_result');
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result');
|
||||
_ ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'sql.passed')
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.passed')
|
||||
end,
|
||||
{ok, [handle_output_list(RuleId, Outputs, Coll, Input) || Coll <- Collection2]};
|
||||
false ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'sql.failed.no_result'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result'),
|
||||
{error, nomatch}
|
||||
end;
|
||||
do_apply_rule(
|
||||
|
@ -171,10 +171,10 @@ do_apply_rule(
|
|||
)
|
||||
of
|
||||
true ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'sql.passed'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.passed'),
|
||||
{ok, handle_output_list(RuleId, Outputs, Selected, Input)};
|
||||
false ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'sql.failed.no_result'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'sql.failed.no_result'),
|
||||
{error, nomatch}
|
||||
end.
|
||||
|
||||
|
@ -316,21 +316,21 @@ handle_output_list(RuleId, Outputs, Selected, Envs) ->
|
|||
[handle_output(RuleId, Out, Selected, Envs) || Out <- Outputs].
|
||||
|
||||
handle_output(RuleId, OutId, Selected, Envs) ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'outputs.total'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'outputs.total'),
|
||||
try
|
||||
Result = do_handle_output(OutId, Selected, Envs),
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'outputs.success'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'outputs.success'),
|
||||
Result
|
||||
catch
|
||||
throw:out_of_service ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'outputs.failed'),
|
||||
ok = emqx_plugin_libs_metrics:inc(
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'outputs.failed'),
|
||||
ok = emqx_metrics_worker:inc(
|
||||
rule_metrics, RuleId, 'outputs.failed.out_of_service'
|
||||
),
|
||||
?SLOG(warning, #{msg => "out_of_service", output => OutId});
|
||||
Err:Reason:ST ->
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'outputs.failed'),
|
||||
ok = emqx_plugin_libs_metrics:inc(rule_metrics, RuleId, 'outputs.failed.unknown'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'outputs.failed'),
|
||||
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'outputs.failed.unknown'),
|
||||
?SLOG(error, #{
|
||||
msg => "output_failed",
|
||||
output => OutId,
|
||||
|
|
Loading…
Reference in New Issue