Skip to content

Commit

Permalink
Refactor metrics handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bzurkowski committed Mar 25, 2017
1 parent 8d3b4ac commit e4fe2db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/mas.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
-type agent() :: any().
-type behaviour() :: atom().
-type topology() :: atom().
-type counter() :: dict:dict(term(), integer()).
42 changes: 19 additions & 23 deletions src/mas_population.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
terminate/2,
code_change/3]).

-type metric() :: [any()].
-type metrics_counter() :: dict:dict(term(), integer()).

-record(state, {module :: module(),
agents :: [agent()],
sim_params :: sim_params(),
metrics :: [metric()],
behaviours_counter :: metrics_counter(),
behaviours_counter :: counter(),
config :: mas:config()}).

%%%=============================================================================
Expand Down Expand Up @@ -103,8 +99,9 @@ handle_info(_Info, State) ->
%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
terminate(_Reason, #state{metrics = Metrics}) ->
lists:foreach(fun unsubscribe_metric/1, Metrics).
terminate(_Reason, #state{behaviours_counter = Counter}) ->
Behaviours = dict:fetch_keys(Counter),
unsubscribe_metrics(Behaviours).

%%------------------------------------------------------------------------------
%% @private
Expand All @@ -121,11 +118,11 @@ code_change(_OldVsn, State, _Extra) ->
%%------------------------------------------------------------------------------
init_state(SP, Config = #config{population_mod = Mod}) ->
Behaviours = behaviours(Mod),
subscribe_metrics(Behaviours),
#state{
module = Mod,
agents = generate_population(Mod, SP, Config),
sim_params = SP,
metrics = setup_metrics(Behaviours),
behaviours_counter = mas_counter:new(Behaviours),
config = Config
}.
Expand Down Expand Up @@ -209,34 +206,33 @@ count_behaviours(Arenas) ->
%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
setup_metrics(Behaviours) ->
[subscribe_metric(Behaviour) || Behaviour <- Behaviours].
subscribe_metrics(Behaviours) ->
[mas_reporter:subscribe(metric(Behaviour)) || Behaviour <- Behaviours].

%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
subscribe_metric(Name) ->
mas_reporter:subscribe([self(), Name]).
unsubscribe_metrics(Behaviours) ->
[mas_reporter:unsubscribe(metric(Behaviour)) || Behaviour <- Behaviours].

%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
unsubscribe_metric(Metric) ->
mas_reporter:unsubscribe(Metric).
schedule_metrics_update(#config{write_interval = WriteInterval}) ->
erlang:send_after(WriteInterval, self(), update_metrics).

%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
schedule_metrics_update(#config{write_interval = WriteInterval}) ->
erlang:send_after(WriteInterval, self(), update_metrics).
update_metrics(State = #state{behaviours_counter = Counter}) ->
Behaviours = dict:fetch_keys(Counter),
lists:foreach(fun(Behaviour) ->
mas_reporter:update(metric(Behaviour),
dict:fetch(Behaviour, Counter))
end, Behaviours),
State#state{behaviours_counter = mas_counter:reset(Counter)}.

%%------------------------------------------------------------------------------
%% @private
%%------------------------------------------------------------------------------
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),
NewCounter = mas_counter:reset(Counter),
State#state{behaviours_counter = NewCounter}.
metric(Name) -> [self(), Name].
10 changes: 9 additions & 1 deletion src/mas_reporter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
teardown/0,
subscribe/1,
subscribe/3,
unsubscribe/1]).
unsubscribe/1,
update/2]).

%%%=============================================================================
%%% API functions
Expand Down Expand Up @@ -60,3 +61,10 @@ subscribe(Metric, Type, DataPoint) ->
unsubscribe(Metric) ->
exometer_report:unsubscribe_all(exometer_report_fs, Metric),
exometer:delete(Metric).

%%%-----------------------------------------------------------------------------
%%% @doc Updates metric with given value.
%%% @end
%%%-----------------------------------------------------------------------------
update(Metric, Value) ->
exometer:update(Metric, Value).

0 comments on commit e4fe2db

Please sign in to comment.