Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding support for basekey
  • Loading branch information
ferd authored and lpgauth committed Mar 6, 2012
1 parent 8e6617f commit a0f8571
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
/ebin
/deps
/logs
/log
.DS_Store
/test/*.beam
*.swp
erl_crash.dump
/.eunit
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,10 @@
Timestamp = erlang:now(),
statsderl:timing_now("test.timing", Timestamp, 0.5).

### Base Key

For multi-node setups, it might be useful to be able to define a basic key based on the current node name, for example. Statsderl supports doing so by setting the app variable `base_key` to some iolist:

application:set_env(statsderl, base_key, "my_node_name")

No need to split the keys -- the `.` as a separator is added automatically.
2 changes: 1 addition & 1 deletion src/statsderl.app.src
@@ -1,6 +1,6 @@
{application, statsderl, [
{description, "StatsD client"},
{vsn, "0.1"},
{vsn, "0.2"},
{registered, []},
{applications, [
kernel,
Expand Down
23 changes: 14 additions & 9 deletions src/statsderl.erl
Expand Up @@ -8,14 +8,15 @@
-record(state, {
hostname,
port,
socket
socket,
basekey
}).

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------

-export([start_link/0, increment/3, decrement/3, timing/3, timing_now/3]).
-export([start_link/1, increment/3, decrement/3, timing/3, timing_now/3]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
Expand All @@ -28,8 +29,8 @@
%% API Function Definitions
%% ------------------------------------------------------------------

start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
start_link(BaseKey) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, BaseKey, []).

increment(Key, Magnitude, SampleRate) ->
Stats = io_lib:format("~s:~B|c|@~f", [Key, Magnitude, SampleRate]),
Expand All @@ -51,23 +52,27 @@ timing_now(Key, Timestamp, SampleRate) ->
%% gen_server Function Definitions
%% ------------------------------------------------------------------

init(_Args) ->
init(BaseKey) ->
{ok, Hostname} = application:get_env(statsderl, hostname),
{ok, Port} = application:get_env(statsderl, port),
{ok, Socket} = gen_udp:open(0, [{active, false}]),
State = #state {
hostname = Hostname,
port = Port,
socket = Socket
socket = Socket,
basekey = case BaseKey of
"" -> "";
_ -> [BaseKey, $.]
end
},
{ok, State}.

handle_call(_Request, _From, State) ->
{noreply, ok, State}.

handle_cast({udp_send, Stats},
handle_cast({udp_send, Stats = #state{basekey=BaseKey}},
#state{hostname=Hostname, port=Port, socket=Socket}=State) ->
gen_udp:send(Socket, Hostname, Port, Stats),
gen_udp:send(Socket, Hostname, Port, [BaseKey, Stats]),
decrease_backlog(),
{noreply, State};
handle_cast(_Msg, State) ->
Expand Down Expand Up @@ -133,4 +138,4 @@ increase_backlog() ->
ets:update_counter(statsderl, backlog, 1).

decrease_backlog() ->
ets:update_counter(statsderl, backlog, -1).
ets:update_counter(statsderl, backlog, -1).
12 changes: 8 additions & 4 deletions src/statsderl_sup.erl
Expand Up @@ -10,22 +10,26 @@
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-define(CHILD(I, Type, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
init_red(),
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
BaseKey = case application:get_env(statsderl, base_key) of
{ok, Key} -> Key;
undefined -> ""
end,
supervisor:start_link({local, ?MODULE}, ?MODULE, BaseKey).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
{ok, { {one_for_one, 5, 10}, [?CHILD(statsderl, worker)]} }.
init(BaseKey) ->
{ok, { {one_for_one, 5, 10}, [?CHILD(statsderl, worker, BaseKey)]} }.

%% ------------------------------------------------------------------
%% Internal Function Definitions
Expand Down

0 comments on commit a0f8571

Please sign in to comment.