Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding support for basekey

  • Loading branch information...
commit a0f8571791d34bb4a1d0628e7cb0843d33de2dcc 1 parent 8e6617f
@ferd ferd authored lpgauth committed
View
9 .gitignore
@@ -0,0 +1,9 @@
+/ebin
+/deps
+/logs
+/log
+.DS_Store
+/test/*.beam
+*.swp
+erl_crash.dump
+/.eunit
View
4 README.md
@@ -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.
View
2  src/statsderl.app.src
@@ -1,6 +1,6 @@
{application, statsderl, [
{description, "StatsD client"},
- {vsn, "0.1"},
+ {vsn, "0.2"},
{registered, []},
{applications, [
kernel,
View
23 src/statsderl.erl
@@ -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
@@ -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]),
@@ -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) ->
@@ -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).
View
12 src/statsderl_sup.erl
@@ -10,7 +10,7 @@
-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
@@ -18,14 +18,18 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.