Skip to content

Commit

Permalink
Reset behaviours counter after each metrics write
Browse files Browse the repository at this point in the history
  • Loading branch information
bzurkowski committed Mar 6, 2017
1 parent f0f1767 commit 4c2fc39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/mas_counter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
%%% API
-export([new/1,
new/2,
update/2]).
update/2,
reset/1,
reset/2]).

%%%=============================================================================
%%% API functions
Expand Down Expand Up @@ -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)).
10 changes: 6 additions & 4 deletions src/mas_population.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}.

Expand Down Expand Up @@ -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}.

0 comments on commit 4c2fc39

Please sign in to comment.