diff --git a/src/mas_counter.erl b/src/mas_counter.erl index 716a44d..4adfa78 100644 --- a/src/mas_counter.erl +++ b/src/mas_counter.erl @@ -8,7 +8,9 @@ %%% API -export([new/1, new/2, - update/2]). + update/2, + reset/1, + reset/2]). %%%============================================================================= %%% API functions @@ -36,3 +38,18 @@ new(Metrics, InitValue) -> update(DataPoints, Counter) -> Fun = fun ({K, V}, DictAcc) -> dict:update_counter(K, V, DictAcc) end, lists:foldl(Fun, Counter, DataPoints). + +%%------------------------------------------------------------------------------ +%% @doc Resets all metric values to zero. +%% @end +%%------------------------------------------------------------------------------ +reset(Counter) -> + reset(Counter, 0). + +%%------------------------------------------------------------------------------ +%% @doc Resets all metric values to provided value. +%% @end +%%------------------------------------------------------------------------------ +reset(Counter, Value) -> + Fun = fun(K, DictAcc) -> dict:store(K, Value, DictAcc) end, + lists:foldl(Fun, Counter, dict:fetch_keys(Counter)). diff --git a/src/mas_population.erl b/src/mas_population.erl index 59220a8..bda3b87 100644 --- a/src/mas_population.erl +++ b/src/mas_population.erl @@ -101,9 +101,8 @@ handle_info(process_population, State) -> self() ! process_population, {noreply, NewState}; handle_info(update_metrics, State = #state{config = Config}) -> - update_metrics(State), schedule_metrics_update(Config), - {noreply, State}; + {noreply, update_metrics(State)}; handle_info(_Info, State) -> {noreply, State}. @@ -240,7 +239,10 @@ schedule_metrics_update(#config{write_interval = WriteInterval}) -> %%------------------------------------------------------------------------------ %% @private %%------------------------------------------------------------------------------ -update_metrics(#state{metrics = Metrics, behaviours_counter = Counter}) -> +update_metrics(State) -> + #state{metrics = Metrics, behaviours_counter = Counter} = State, lists:foreach(fun(Metric = [_Pid, Behaviour]) -> exometer:update(Metric, dict:fetch(Behaviour, Counter)) - end, Metrics). + end, Metrics), + NewCounter = mas_counter:reset(Counter), + State#state{behaviours_counter = NewCounter}.