Skip to content

Commit

Permalink
support n-ary metric initialization from application environment at s…
Browse files Browse the repository at this point in the history
…tartup
  • Loading branch information
Fabian Linzberger committed Aug 27, 2012
1 parent 64b24d3 commit 35e9309
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/folsom.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@ stop() ->

start(_Type, _Args) ->
{ok, Pid} = folsom_sup:start_link(),
lists:foreach(
fun ({K, New}) ->
case application:get_env(?APP, K) of
{ok, Name} when is_atom(Name) ->
New(Name);
{ok, Names} when is_list(Names) ->
lists:foreach(New, Names);
undefined ->
ok
end
end,
[{counter, fun folsom_metrics:new_counter/1},
{gauge, fun folsom_metrics:new_gauge/1},
{histogram, fun folsom_metrics:new_histogram/1},
{history, fun folsom_metrics:new_history/1},
{meter, fun folsom_metrics:new_meter/1},
{meter_reader, fun folsom_metrics:new_meter_reader/1}]),
lists:foreach(fun configure/1,
[{counter, new_counter},
{gauge, new_gauge},
{histogram, new_histogram},
{history, new_history},
{meter, new_meter},
{meter_reader, new_meter_reader}]),
{ok, Pid}.

stop(_State) ->
ok.

%% internal
configure({K, New}) ->
case application:get_env(?APP, K) of
{ok, Specs} when is_list(Specs) ->
[configure_metric(New, Spec) || Spec <- Specs];
{ok, Spec} ->
configure_metric(New, Spec);
undefined -> ok
end.

configure_metric(New, Spec) when is_list(Spec) ->
apply(folsom_metrics, New, Spec);
configure_metric(New, Name) ->
folsom_metrics:New(Name).
25 changes: 25 additions & 0 deletions test/folsom_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ run_test_() ->
fun folsom_erlang_checks:delete_metrics/0},
{"cpu topology test",
fun folsom_erlang_checks:cpu_topology/0}]}.

configure_test_() ->
{foreach, fun setup_app/0, fun cleanup_app/1,
[{"start with configured metrics",
fun() ->
?assertMatch(ok, application:start(folsom)),
[counter, slide, <<"gauge">>, <<"uniform">>] =
lists:sort(folsom_metrics:get_metrics())
end}]}.

setup_app() ->
application:unload(folsom),
Env = [{counter, counter},
{gauge, <<"gauge">>},
{histogram, [[<<"uniform">>, uniform, 5000],
[slide, slide_uniform, {60, 1028}]]}],
application:load({application, folsom, [{mod, {folsom, []}}, {env, Env}]}),
ok.

cleanup_app(ok) ->
lists:foreach(fun folsom_metrics:delete_metric/1,
[counter, slide, <<"gauge">>, <<"uniform">>]),
application:stop(folsom),
application:unload(folsom),
ok.

0 comments on commit 35e9309

Please sign in to comment.