Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Folsom as an application #18

Merged
merged 2 commits into from

2 participants

@etrepum

This pull request makes folsom an application that can be started with application:start/1 and stopped with application:stop/1. I also made some changes to the test suite so they pass on a slower machine, and some fixes to the docs near the area where it talks about application startup, and picked some nits in the .app.src.

The other thing is that you can now create metrics from the command line or application config at startup (see the doc changes).

@etrepum

The only thing that I would consider to be controversial is the change of folsom:start/0 to call application:start/1 instead of folsom_sup:start_link/0. The previous function did the wrong thing, since functions called start shouldn't actually do start_link (I've made this mistake myself). This "fix" would also take care of #17

@joewilliams joewilliams merged commit c42bd7a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -1,3 +1,5 @@
+/deps
+/.eunit
ebin/*
*#
*~
View
24 README.md
@@ -8,13 +8,31 @@ First, regarding using folsom and folsom_webmachine together. To make sure you h
You need a (preferably recent) version of Erlang installed but that should be it.
- ./rebar compile
+ ./rebar get-deps compile
folsom can be run standalone or embedded in an Erlang application.
- $ erl -pa ebin
+ $ erl -pa ebin deps/*/ebin
- > folsom_sup:start_link(). % this creates the needed ETS tables and starts a gen_server
+ > folsom:start(). % this creates the needed ETS tables and starts a gen_server
+
+You can also start it as an application:
+
+ $ erl -pa ebin deps/*/ebin
+ > application:start(folsom).
+
+ $ erl -pa ebin deps/*/ebin -s folsom
+
+The application can be configured to create individual or lists of metrics at
+startup on the command line or in an application config file:
+
+ $ erl -pa ebin deps/*/ebin -s folsom \
+ -folsom history '[hist1,hist2]' \
+ -folsom gauge gauge1
+
+ $ echo '[{folsom, [{history, [hist1, hist2]}, {gauge, gauge1}]}].' \
+ > myapp.config
+ $ erl -pa ebin deps/*/ebin -config myapp.config -s folsom
#### Metrics API
View
8 src/folsom.app.src
@@ -1,11 +1,15 @@
+%% -*- mode: erlang -*-
{application, folsom,
[
{description, ""},
{vsn, git},
- {registered, []},
+ {registered, [folsom_meter_timer_server,
+ folsom_metrics_histogram_ets,
+ folsom_sup]},
{applications, [
kernel,
stdlib
]},
- {env, []}
+ {env, []},
+ {mod, {folsom, []}}
]}.
View
34 src/folsom.erl
@@ -23,7 +23,37 @@
%%%------------------------------------------------------------------
-module(folsom).
--export([start/0]).
+-export([start/0, stop/0]).
+-export([start/2, stop/1]).
+-behaviour(application).
+-define(APP, ?MODULE).
start() ->
- folsom_sup:start_link().
+ application:start(?APP).
+
+stop() ->
+ application:stop(?APP).
+
+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}]),
+ {ok, Pid}.
+
+stop(?APP) ->
+ ok.
View
47 test/folsom_tests.erl
@@ -26,30 +26,23 @@
-include_lib("eunit/include/eunit.hrl").
-run_test() ->
- folsom:start(),
-
- ?debugFmt("creating metrics", []),
- folsom_erlang_checks:create_metrics(),
-
- ?debugFmt("populating metrics", []),
- folsom_erlang_checks:populate_metrics(),
-
- ?debugFmt("checking metrics", []),
- folsom_erlang_checks:check_metrics(),
-
- ?debugFmt("checking counter metric", []),
- folsom_erlang_checks:counter_metric(10000, testcounter),
-
- ?debugFmt("checking erlang vm metrics", []),
- folsom_erlang_checks:vm_metrics(),
-
- ?debugFmt("deleting metrics", []),
- folsom_erlang_checks:delete_metrics(),
-
- ?debugFmt("cpu topology test", []),
- folsom_erlang_checks:cpu_topology().
-
-
-
-
+run_test_() ->
+ {setup,
+ fun () -> folsom:start() end,
+ fun (_) -> folsom:stop() end,
+ [{"creating metrics",
+ fun folsom_erlang_checks:create_metrics/0},
+ {"populating metrics",
+ {timeout, 30, fun folsom_erlang_checks:populate_metrics/0}},
+ {"checking metrics",
+ fun folsom_erlang_checks:check_metrics/0},
+ {"checking counter metric",
+ fun () ->
+ folsom_erlang_checks:counter_metric(10000, testcounter)
+ end},
+ {"checking erlang vm metrics",
+ fun folsom_erlang_checks:vm_metrics/0},
+ {"deleting metrics",
+ fun folsom_erlang_checks:delete_metrics/0},
+ {"cpu topology test",
+ fun folsom_erlang_checks:cpu_topology/0}]}.
Something went wrong with that request. Please try again.