Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple hosts support #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ High Performance Erlang StatsD Client
</tr>
<tr>
<td>hostname</td>
<td>inet:ip_address() | inet:hostname() | binary()</td>
<td>inet:ip_address() | inet:hostname() | binary() | [{inet:ip_address() | inet:hostname() | binary(), inet:port_number()}]</td>
<td>{127,0,0,1}</td>
<td>server hostname</td>
</tr>
<tr>
<td>port</td>
<td>inet:port_number()</td>
<td>8125</td>
<td>server port</td>
<td>server port. Not needed when using the list syntax in the hostname variable</td>
</tr>
</table>

Expand Down
3 changes: 2 additions & 1 deletion src/statsderl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ cast(OpCode, Key, Value, SampleRate) ->
cast(OpCode, Key, Value, SampleRate, ServerName).

cast(OpCode, Key, Value, SampleRate, ServerName) ->
KeyHash = erlang:phash2(iolist_to_binary(Key)),
Packet = statsderl_protocol:encode(OpCode, Key, Value, SampleRate),
send(ServerName, {cast, Packet}).
send(ServerName, {cast, KeyHash, Packet}).

maybe_cast(OpCode, Key, Value, 1) ->
cast(OpCode, Key, Value, 1);
Expand Down
49 changes: 32 additions & 17 deletions src/statsderl_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
]).

-record(state, {
header :: iodata(),
headers :: tuple(),
socket :: inet:socket()
}).

Expand All @@ -17,22 +17,26 @@
init(Parent, Name) ->
BaseKey = ?ENV(?ENV_BASEKEY, ?DEFAULT_BASEKEY),
Hostname = ?ENV(?ENV_HOSTNAME, ?DEFAULT_HOSTNAME),
Port = ?ENV(?ENV_PORT, ?DEFAULT_PORT),
{ok, Headers} = case is_list(Hostname)
andalso Hostname /= []
andalso is_tuple(hd(Hostname)) of
true ->
generate_udp_headers(Hostname, BaseKey, []);
false ->
Port = ?ENV(?ENV_PORT, ?DEFAULT_PORT),
generate_udp_headers([{Hostname, Port}],
BaseKey, [])
end,

case udp_header(Hostname, Port, BaseKey) of
{ok, Header} ->
case gen_udp:open(0, [{active, false}]) of
{ok, Socket} ->
register(Name, self()),
proc_lib:init_ack(Parent, {ok, self()}),
case gen_udp:open(0, [{active, false}]) of
{ok, Socket} ->
register(Name, self()),
proc_lib:init_ack(Parent, {ok, self()}),

loop(#state {
socket = Socket,
header = Header
});
{error, Reason} ->
exit(Reason)
end;
loop(#state {
socket = Socket,
headers = list_to_tuple(Headers)
});
{error, Reason} ->
exit(Reason)
end.
Expand All @@ -43,11 +47,12 @@ start_link(Name) ->
proc_lib:start_link(?MODULE, init, [self(), Name]).

%% private
handle_msg({cast, Packet}, #state {
header = Header,
handle_msg({cast, KeyHash, Packet}, #state {
headers = Headers,
socket = Socket
} = State) ->

Header = element((KeyHash rem tuple_size(Headers)) + 1, Headers),
statsderl_udp:send(Socket, Header, Packet),
{ok, State};
handle_msg({inet_reply, _Socket, ok}, State) ->
Expand All @@ -62,6 +67,16 @@ loop(State) ->
loop(State2)
end.

generate_udp_headers([], _BaseKey, Acc) ->
{ok, lists:reverse(Acc)};
generate_udp_headers([{Hostname, Port} | Rest], BaseKey, Acc) ->
case udp_header(Hostname, Port, BaseKey) of
{ok, Header} ->
generate_udp_headers(Rest, BaseKey, [Header | Acc]);
Error ->
Error
end.

udp_header(Hostname, Port, BaseKey) ->
case statsderl_utils:getaddrs(Hostname) of
{ok, {A, B, C, D}} ->
Expand Down
13 changes: 13 additions & 0 deletions test/statsderl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ statsderl_hostname_test() ->
meck:unload(statsderl_utils),
cleanup(Socket).

statsderl_multiple_hostname_test() ->
meck:new(statsderl_utils, [passthrough, no_history]),
meck:expect(statsderl_utils, getaddrs, fun (_) ->
{ok, {127, 0, 0, 1}}
end),
Socket = setup([{?ENV_HOSTNAME,
[{<<"adgear.com">>, ?DEFAULT_PORT}, {"whatever.com", ?DEFAULT_PORT}]}]),
statsderl:counter("test", 1, 1),
{ok, {_Address, _Port, Packet}} = gen_udp:recv(Socket, 0),
?assertEqual(<<"test:1|c">>, Packet),
meck:unload(statsderl_utils),
cleanup(Socket).

statsderl_test_() ->
{setup,
fun () -> setup() end,
Expand Down