Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…tartup
  • Loading branch information...
commit 35e930982ffac4af558c3b4e102de8abd732aea1 1 parent 64b24d3
Fabian Linzberger lefant authored

Showing 2 changed files with 47 additions and 17 deletions. Show diff stats Hide diff stats

  1. +22 17 src/folsom.erl
  2. +25 0 test/folsom_tests.erl
39 src/folsom.erl
@@ -36,24 +36,29 @@ stop() ->
36 36
37 37 start(_Type, _Args) ->
38 38 {ok, Pid} = folsom_sup:start_link(),
39   - lists:foreach(
40   - fun ({K, New}) ->
41   - case application:get_env(?APP, K) of
42   - {ok, Name} when is_atom(Name) ->
43   - New(Name);
44   - {ok, Names} when is_list(Names) ->
45   - lists:foreach(New, Names);
46   - undefined ->
47   - ok
48   - end
49   - end,
50   - [{counter, fun folsom_metrics:new_counter/1},
51   - {gauge, fun folsom_metrics:new_gauge/1},
52   - {histogram, fun folsom_metrics:new_histogram/1},
53   - {history, fun folsom_metrics:new_history/1},
54   - {meter, fun folsom_metrics:new_meter/1},
55   - {meter_reader, fun folsom_metrics:new_meter_reader/1}]),
  39 + lists:foreach(fun configure/1,
  40 + [{counter, new_counter},
  41 + {gauge, new_gauge},
  42 + {histogram, new_histogram},
  43 + {history, new_history},
  44 + {meter, new_meter},
  45 + {meter_reader, new_meter_reader}]),
56 46 {ok, Pid}.
57 47
58 48 stop(_State) ->
59 49 ok.
  50 +
  51 +%% internal
  52 +configure({K, New}) ->
  53 + case application:get_env(?APP, K) of
  54 + {ok, Specs} when is_list(Specs) ->
  55 + [configure_metric(New, Spec) || Spec <- Specs];
  56 + {ok, Spec} ->
  57 + configure_metric(New, Spec);
  58 + undefined -> ok
  59 + end.
  60 +
  61 +configure_metric(New, Spec) when is_list(Spec) ->
  62 + apply(folsom_metrics, New, Spec);
  63 +configure_metric(New, Name) ->
  64 + folsom_metrics:New(Name).
25 test/folsom_tests.erl
@@ -46,3 +46,28 @@ run_test_() ->
46 46 fun folsom_erlang_checks:delete_metrics/0},
47 47 {"cpu topology test",
48 48 fun folsom_erlang_checks:cpu_topology/0}]}.
  49 +
  50 +configure_test_() ->
  51 + {foreach, fun setup_app/0, fun cleanup_app/1,
  52 + [{"start with configured metrics",
  53 + fun() ->
  54 + ?assertMatch(ok, application:start(folsom)),
  55 + [counter, slide, <<"gauge">>, <<"uniform">>] =
  56 + lists:sort(folsom_metrics:get_metrics())
  57 + end}]}.
  58 +
  59 +setup_app() ->
  60 + application:unload(folsom),
  61 + Env = [{counter, counter},
  62 + {gauge, <<"gauge">>},
  63 + {histogram, [[<<"uniform">>, uniform, 5000],
  64 + [slide, slide_uniform, {60, 1028}]]}],
  65 + application:load({application, folsom, [{mod, {folsom, []}}, {env, Env}]}),
  66 + ok.
  67 +
  68 +cleanup_app(ok) ->
  69 + lists:foreach(fun folsom_metrics:delete_metric/1,
  70 + [counter, slide, <<"gauge">>, <<"uniform">>]),
  71 + application:stop(folsom),
  72 + application:unload(folsom),
  73 + ok.

0 comments on commit 35e9309

Please sign in to comment.
Something went wrong with that request. Please try again.