From 35e930982ffac4af558c3b4e102de8abd732aea1 Mon Sep 17 00:00:00 2001 From: Fabian Linzberger Date: Fri, 27 Jul 2012 16:51:48 +0200 Subject: [PATCH] support n-ary metric initialization from application environment at startup --- src/folsom.erl | 39 ++++++++++++++++++++++----------------- test/folsom_tests.erl | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/folsom.erl b/src/folsom.erl index 0f08da0..9fbe2e0 100644 --- a/src/folsom.erl +++ b/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). diff --git a/test/folsom_tests.erl b/test/folsom_tests.erl index 04f0ba8..296b57f 100644 --- a/test/folsom_tests.erl +++ b/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.