Explorar el Código

feat: impl resource reset_metrics

EMQ-YangM hace 3 años
padre
commit
8f06a9ec62

+ 10 - 0
apps/emqx_resource/src/emqx_resource.erl

@@ -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) ->

+ 10 - 0
apps/emqx_resource/src/emqx_resource_instance.erl

@@ -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).
 

+ 6 - 0
apps/emqx_resource/src/proto/emqx_resource_proto_v1.erl

@@ -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]).

+ 14 - 1
apps/emqx_resource/test/emqx_resource_SUITE.erl

@@ -294,7 +294,7 @@ t_create_dry_run_local(_) ->
 
     ?assertEqual(undefined, whereis(test_resource)).
 
-t_create_dry_run_local_failed(_) -> 
+t_create_dry_run_local_failed(_) ->
     {Res, _} = emqx_resource:create_dry_run_local(?TEST_RESOURCE,
                        #{cteate_error => true}),
     ?assertEqual(error, Res),
@@ -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
 %%------------------------------------------------------------------------------