feat: impl resource reset_metrics

This commit is contained in:
EMQ-YangM 2022-04-06 17:25:21 +08:00
parent fa0c4d17ee
commit 8f06a9ec62
4 changed files with 40 additions and 1 deletions

View File

@ -53,6 +53,8 @@
, recreate_local/4
, remove/1 %% remove the config and stop the instance
, remove_local/1
, reset_metrics/1
, reset_metrics_local/1
]).
%% Calls to the callback module with current resource state
@ -184,6 +186,14 @@ remove(InstId) ->
remove_local(InstId) ->
call_instance(InstId, {remove, InstId}).
-spec reset_metrics_local(instance_id()) -> ok.
reset_metrics_local(InstId) ->
call_instance(InstId, {reset_metrics, InstId}).
-spec reset_metrics(instance_id()) -> ok | {error, Reason :: term()}.
reset_metrics(InstId) ->
wrap_rpc(emqx_resource_proto_v1:reset_metrics(InstId)).
%% =================================================================================
-spec query(instance_id(), Request :: term()) -> Result :: term().
query(InstId, Request) ->

View File

@ -25,6 +25,7 @@
%% load resource instances from *.conf files
-export([ lookup/1
, get_metrics/1
, reset_metrics/1
, list_all/0
, list_group/1
]).
@ -77,6 +78,9 @@ make_test_id() ->
get_metrics(InstId) ->
emqx_plugin_libs_metrics:get_metrics(resource_metrics, InstId).
reset_metrics(InstId) ->
emqx_plugin_libs_metrics:reset_metrics(resource_metrics, InstId).
force_lookup(InstId) ->
{ok, _Group, Data} = lookup(InstId),
Data.
@ -114,6 +118,9 @@ handle_call({create_dry_run, ResourceType, Config}, _From, State) ->
handle_call({recreate, InstId, ResourceType, Config, Opts}, _From, State) ->
{reply, do_recreate(InstId, ResourceType, Config, Opts), State};
handle_call({reset_metrics, InstId}, _From, State) ->
{reply, do_reset_metrics(InstId), State};
handle_call({remove, InstId}, _From, State) ->
{reply, do_remove(InstId), State};
@ -222,6 +229,9 @@ do_create_dry_run(ResourceType, Config) ->
{error, Reason}
end.
do_reset_metrics(Instance) ->
reset_metrics(Instance).
do_remove(Instance) ->
do_remove(Instance, true).

View File

@ -24,6 +24,7 @@
, create_dry_run/2
, recreate/4
, remove/1
, reset_metrics/1
]).
-include_lib("emqx/include/bpapi.hrl").
@ -61,3 +62,8 @@ recreate(InstId, ResourceType, Config, Opts) ->
emqx_cluster_rpc:multicall_return(ok).
remove(InstId) ->
emqx_cluster_rpc:multicall(emqx_resource, remove_local, [InstId]).
-spec reset_metrics(emqx_resource:instance_id()) ->
emqx_cluster_rpc:multicall_return(ok).
reset_metrics(InstId) ->
emqx_cluster_rpc:multicall(emqx_resource, reset_metrics_local, [InstId]).

View File

@ -313,6 +313,19 @@ t_test_func(_) ->
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(array, 10), [[a,b,c,d]])),
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(string, 10), ["less10"])).
t_reset_metrics(_) ->
{ok, _} = emqx_resource:create(
?ID,
?DEFAULT_RESOURCE_GROUP,
?TEST_RESOURCE,
#{name => test_resource}),
#{pid := Pid} = emqx_resource:query(?ID, get_state),
emqx_resource:reset_metrics(?ID),
?assert(is_process_alive(Pid)),
ok = emqx_resource:remove(?ID),
?assertNot(is_process_alive(Pid)).
%%------------------------------------------------------------------------------
%% Helpers
%%------------------------------------------------------------------------------