Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support n-ary metric initialization from application environment at s…

…tartup
  • Loading branch information...
commit 35e930982ffac4af558c3b4e102de8abd732aea1 1 parent 64b24d3
Fabian Linzberger authored
Showing with 47 additions and 17 deletions.
  1. +22 −17 src/folsom.erl
  2. +25 −0 test/folsom_tests.erl
View
39 src/folsom.erl
@@ -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).
View
25 test/folsom_tests.erl
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.