feat(resource): reset metrics when stopping a resource

This commit is contained in:
Thales Macedo Garitezi 2022-10-18 09:32:35 -03:00
parent ee4c723fcb
commit 62eeb4b8e8
2 changed files with 20 additions and 2 deletions

View File

@ -507,13 +507,16 @@ start_resource(Data, From) ->
stop_resource(#data{state = undefined, id = ResId} = _Data) -> stop_resource(#data{state = undefined, id = ResId} = _Data) ->
_ = maybe_clear_alarm(ResId), _ = maybe_clear_alarm(ResId),
ok = emqx_metrics_worker:reset_metrics(?RES_METRICS, ResId),
ok; ok;
stop_resource(Data) -> stop_resource(Data) ->
%% We don't care the return value of the Mod:on_stop/2. %% We don't care the return value of the Mod:on_stop/2.
%% The callback mod should make sure the resource is stopped after on_stop/2 %% The callback mod should make sure the resource is stopped after on_stop/2
%% is returned. %% is returned.
ResId = Data#data.id,
_ = emqx_resource:call_stop(Data#data.manager_id, Data#data.mod, Data#data.state), _ = emqx_resource:call_stop(Data#data.manager_id, Data#data.mod, Data#data.state),
_ = maybe_clear_alarm(Data#data.id), _ = maybe_clear_alarm(ResId),
ok = emqx_metrics_worker:reset_metrics(?RES_METRICS, ResId),
ok. ok.
make_test_id() -> make_test_id() ->

View File

@ -502,6 +502,10 @@ t_stop_start(_) ->
#{<<"name">> => <<"test_resource">>} #{<<"name">> => <<"test_resource">>}
), ),
%% add some metrics to test their persistence
emqx_resource_metrics:batching_change(?ID, 5),
?assertEqual(5, emqx_resource_metrics:batching_get(?ID)),
{ok, _} = emqx_resource:check_and_recreate( {ok, _} = emqx_resource:check_and_recreate(
?ID, ?ID,
?TEST_RESOURCE, ?TEST_RESOURCE,
@ -513,6 +517,9 @@ t_stop_start(_) ->
?assert(is_process_alive(Pid0)), ?assert(is_process_alive(Pid0)),
%% metrics are reset when recreating
?assertEqual(0, emqx_resource_metrics:batching_get(?ID)),
ok = emqx_resource:stop(?ID), ok = emqx_resource:stop(?ID),
?assertNot(is_process_alive(Pid0)), ?assertNot(is_process_alive(Pid0)),
@ -527,7 +534,15 @@ t_stop_start(_) ->
{ok, #{pid := Pid1}} = emqx_resource:query(?ID, get_state), {ok, #{pid := Pid1}} = emqx_resource:query(?ID, get_state),
?assert(is_process_alive(Pid1)). ?assert(is_process_alive(Pid1)),
%% now stop while resetting the metrics
emqx_resource_metrics:batching_change(?ID, 5),
?assertEqual(5, emqx_resource_metrics:batching_get(?ID)),
ok = emqx_resource:stop(?ID),
?assertEqual(0, emqx_resource_metrics:batching_get(?ID)),
ok.
t_stop_start_local(_) -> t_stop_start_local(_) ->
{error, _} = emqx_resource:check_and_create_local( {error, _} = emqx_resource:check_and_create_local(