feat: impl reset_metrics
This commit is contained in:
parent
64eccecede
commit
18e2a2ce56
|
@ -32,6 +32,7 @@
|
|||
, create_metrics/3
|
||||
, create_metrics/4
|
||||
, clear_metrics/2
|
||||
, reset_metrics/2
|
||||
, has_metrics/2
|
||||
]).
|
||||
|
||||
|
@ -116,6 +117,10 @@ create_metrics(Name, Id, Metrics, RateMetrics) ->
|
|||
clear_metrics(Name, Id) ->
|
||||
gen_server:call(Name, {delete_metrics, Id}).
|
||||
|
||||
-spec(reset_metrics(handler_name(), metric_id()) -> ok).
|
||||
reset_metrics(Name, Id) ->
|
||||
gen_server:call(Name, {reset_metrics, Id}).
|
||||
|
||||
-spec(has_metrics(handler_name(), metric_id()) -> boolean()).
|
||||
has_metrics(Name, Id) ->
|
||||
case get_ref(Name, Id) of
|
||||
|
@ -143,6 +148,13 @@ get_counters(Name, Id) ->
|
|||
get(Name, Id, Index)
|
||||
end, get_indexes(Name, Id)).
|
||||
|
||||
-spec reset_counters(handler_name(), metric_id()) -> ok.
|
||||
reset_counters(Name, Id) ->
|
||||
Indexes = maps:values(get_indexes(Name, Id)),
|
||||
Ref = get_ref(Name, Id),
|
||||
[counters:put(Ref, Idx, 0) || Idx <- Indexes ],
|
||||
ok.
|
||||
|
||||
-spec(get_metrics(handler_name(), metric_id()) -> metrics()).
|
||||
get_metrics(Name, Id) ->
|
||||
#{rate => get_rate(Name, Id), counters => get_counters(Name, Id)}.
|
||||
|
@ -198,6 +210,17 @@ handle_call({delete_metrics, Id}, _From,
|
|||
_ -> maps:remove(Id, Rates)
|
||||
end}};
|
||||
|
||||
handle_call({reset_metrics, Id}, _From,
|
||||
State = #state{rates = Rates}) ->
|
||||
{reply, reset_counters(get_self_name(), Id),
|
||||
State#state{rates = case Rates of
|
||||
undefined -> undefined;
|
||||
_ -> ResetRate =
|
||||
maps:map(fun(_Key, _Value) -> #rate{} end,
|
||||
maps:get(Id, Rates, #{})),
|
||||
maps:put(Id, ResetRate, Rates)
|
||||
end}};
|
||||
|
||||
handle_call(_Request, _From, State) ->
|
||||
{reply, ok, State}.
|
||||
|
||||
|
|
|
@ -85,6 +85,44 @@ t_get_metrics(_) ->
|
|||
?assert(MaxA > 0), ?assert(MaxB > 0), ?assert(MaxC > 0)}),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_reset_metrics(_) ->
|
||||
Metrics = [a, b, c],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics),
|
||||
%% all the metrics are set to zero at start
|
||||
?assertMatch(#{
|
||||
rate := #{
|
||||
a := #{current := 0.0, max := 0.0, last5m := 0.0},
|
||||
b := #{current := 0.0, max := 0.0, last5m := 0.0},
|
||||
c := #{current := 0.0, max := 0.0, last5m := 0.0}
|
||||
},
|
||||
counters := #{
|
||||
a := 0,
|
||||
b := 0,
|
||||
c := 0
|
||||
}
|
||||
}, emqx_plugin_libs_metrics: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),
|
||||
ct:sleep(1500),
|
||||
ok = emqx_plugin_libs_metrics:reset_metrics(?NAME, <<"testid">>),
|
||||
?LET(#{
|
||||
rate := #{
|
||||
a := #{current := CurrA, max := MaxA, last5m := _},
|
||||
b := #{current := CurrB, max := MaxB, last5m := _},
|
||||
c := #{current := CurrC, max := MaxC, last5m := _}
|
||||
},
|
||||
counters := #{
|
||||
a := 0,
|
||||
b := 0,
|
||||
c := 0
|
||||
}
|
||||
}, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>),
|
||||
{?assert(CurrA == 0), ?assert(CurrB == 0), ?assert(CurrC == 0),
|
||||
?assert(MaxA == 0), ?assert(MaxB == 0), ?assert(MaxC == 0)}),
|
||||
ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
|
||||
|
||||
t_get_metrics_2(_) ->
|
||||
Metrics = [a, b, c],
|
||||
ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics,
|
||||
|
|
Loading…
Reference in New Issue