From 36ce949a296a722f7d3f5dc3a6d88c54d87f5ab0 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Wed, 12 Oct 2016 12:13:21 -0400 Subject: [PATCH 1/3] Cleanup code + Remove try/catch --- src/statsderl.erl | 103 +++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/src/statsderl.erl b/src/statsderl.erl index d068996..581c3fa 100644 --- a/src/statsderl.erl +++ b/src/statsderl.erl @@ -19,42 +19,50 @@ ]). %% public --spec counter(key(), value(), sample_rate()) -> ok. +-spec counter(key(), value(), sample_rate()) -> + ok. counter(Key, Value, SampleRate) -> - maybe_cast(counter, Key, Value, SampleRate). + sample(counter, Key, Value, SampleRate). --spec decrement(key(), value(), sample_rate()) -> ok. +-spec decrement(key(), value(), sample_rate()) -> + ok. decrement(Key, Value, SampleRate) when Value >= 0 -> - maybe_cast(counter, Key, -Value, SampleRate). + sample(counter, Key, -Value, SampleRate). --spec gauge(key(), value(), sample_rate()) -> ok. +-spec gauge(key(), value(), sample_rate()) -> + ok. gauge(Key, Value, SampleRate) when Value >= 0 -> - maybe_cast(gauge, Key, Value, SampleRate). + sample(gauge, Key, Value, SampleRate). --spec gauge_decrement(key(), value(), sample_rate()) -> ok. +-spec gauge_decrement(key(), value(), sample_rate()) -> + ok. gauge_decrement(Key, Value, SampleRate) when Value >= 0 -> - maybe_cast(gauge_decrement, Key, Value, SampleRate). + sample(gauge_decrement, Key, Value, SampleRate). --spec gauge_increment(key(), value(), sample_rate()) -> ok. +-spec gauge_increment(key(), value(), sample_rate()) -> + ok. gauge_increment(Key, Value, SampleRate) when Value >= 0 -> - maybe_cast(gauge_increment, Key, Value, SampleRate). + sample(gauge_increment, Key, Value, SampleRate). --spec increment(key(), value(), sample_rate()) -> ok. +-spec increment(key(), value(), sample_rate()) -> + ok. increment(Key, Value, SampleRate) when Value >= 0 -> - maybe_cast(counter, Key, Value, SampleRate). + sample(counter, Key, Value, SampleRate). --spec timing(key(), value(), sample_rate()) -> ok. +-spec timing(key(), value(), sample_rate()) -> + ok. timing(Key, Value, SampleRate) -> - maybe_cast(timing, Key, Value, SampleRate). + sample(timing, Key, Value, SampleRate). --spec timing_fun(key(), fun(), sample_rate()) -> ok. +-spec timing_fun(key(), fun(), sample_rate()) -> + ok. timing_fun(Key, Fun, SampleRate) -> Timestamp = statsderl_utils:timestamp(), @@ -62,64 +70,55 @@ timing_fun(Key, Fun, SampleRate) -> timing_now(Key, Timestamp, SampleRate), Result. --spec timing_now(key(), erlang:timestamp(), sample_rate()) -> ok. +-spec timing_now(key(), erlang:timestamp(), sample_rate()) -> + ok. timing_now(Key, Timestamp, SampleRate) -> - maybe_cast(timing_now, Key, Timestamp, SampleRate). + sample(timing_now, Key, Timestamp, SampleRate). --spec timing_now_us(key(), erlang:timestamp(), sample_rate()) -> ok. +-spec timing_now_us(key(), erlang:timestamp(), sample_rate()) -> + ok. timing_now_us(Key, Timestamp, SampleRate) -> - maybe_cast(timing_now_us, Key, Timestamp, SampleRate). + sample(timing_now_us, Key, Timestamp, SampleRate). %% private -cast(OpCode, Key, Value, SampleRate) -> - ServerName = statsderl_utils:random_server(), - cast(OpCode, Key, Value, SampleRate, ServerName). - cast(OpCode, Key, Value, SampleRate, ServerName) -> Packet = statsderl_protocol:encode(OpCode, Key, Value, SampleRate), send(ServerName, {cast, Packet}). -maybe_cast(timing_now, Key, Value, 1) -> - cast(timing, Key, timing_now(Value), 1); -maybe_cast(timing_now_us, Key, Value, 1) -> - cast(timing, Key, timing_now_us(Value), 1); -maybe_cast(OpCode, Key, Value, 1) -> - cast(OpCode, Key, Value, 1); -maybe_cast(timing_now, Key, Value, 1.0) -> - cast(timing, Key, timing_now(Value), 1.0); -maybe_cast(timing_now_us, Key, Value, 1.0) -> - cast(timing, Key, timing_now_us(Value), 1.0); -maybe_cast(OpCode, Key, Value, 1.0) -> - cast(OpCode, Key, Value, 1); -maybe_cast(OpCode, Key, Value, SampleRate) -> +operation(OpCode, Key, Value, SampleRate) -> + ServerName = statsderl_utils:random_server(), + operation(OpCode, Key, Value, SampleRate, ServerName). + +operation(timing_now, Key, Value, SampleRate, ServerName) -> + cast(timing, Key, timing_now(Value), SampleRate, ServerName); +operation(timing_now_us, Key, Value, SampleRate, ServerName) -> + cast(timing, Key, timing_now_us(Value), SampleRate, ServerName); +operation(OpCode, Key, Value, SampleRate, ServerName) -> + cast(OpCode, Key, Value, SampleRate, ServerName). + +sample(OpCode, Key, Value, 1) -> + operation(OpCode, Key, Value, 1); +sample(OpCode, Key, Value, 1.0) -> + operation(OpCode, Key, Value, 1); +sample(OpCode, Key, Value, SampleRate) -> Rand = statsderl_utils:random(?MAX_UNSIGNED_INT_32), case Rand =< SampleRate * ?MAX_UNSIGNED_INT_32 of true -> N = Rand rem ?POOL_SIZE + 1, ServerName = statsderl_utils:server_name(N), - case OpCode of - timing_now -> - cast(timing, Key, timing_now(Value), SampleRate, - ServerName); - timing_now_us -> - cast(timing, Key, timing_now_us(Value), SampleRate, - ServerName); - _ -> - cast(OpCode, Key, Value, SampleRate, ServerName) - end; + operation(OpCode, Key, Value, SampleRate, ServerName); false -> ok end. send(ServerName, Msg) -> - try - ServerName ! Msg, - ok - catch - error:badarg -> - ok + case whereis(ServerName) of + undefined -> + ok; + Pid -> + Pid ! Msg end. timing_now(Timestamp) -> From 5d16d49eba59c659cde1f4a3a7abd6514c645055 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Wed, 12 Oct 2016 12:13:58 -0400 Subject: [PATCH 2/3] Update DOC --- doc/statsderl.md | 11 ++++++++++- doc/statsderl_app.md | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/statsderl.md b/doc/statsderl.md index 6c93962..0fc7294 100644 --- a/doc/statsderl.md +++ b/doc/statsderl.md @@ -44,7 +44,7 @@ value() = number() ## Function Index ## -
counter/3
decrement/3
gauge/3
gauge_decrement/3
gauge_increment/3
increment/3
timing/3
timing_fun/3
timing_now/3
+
counter/3
decrement/3
gauge/3
gauge_decrement/3
gauge_increment/3
increment/3
timing/3
timing_fun/3
timing_now/3
timing_now_us/3
@@ -132,3 +132,12 @@ timing_now(Key::key(), Timestamp:: + +### timing_now_us/3 ### + +

+timing_now_us(Key::key(), Timestamp::erlang:timestamp(), SampleRate::sample_rate()) -> ok
+
+
+ diff --git a/doc/statsderl_app.md b/doc/statsderl_app.md index 39450d4..28fc24d 100644 --- a/doc/statsderl_app.md +++ b/doc/statsderl_app.md @@ -23,7 +23,7 @@ __Behaviours:__ [`application`](application.md). ### start/0 ###

-start() -> ok | {error, atom()}
+start() -> {ok, [atom()]} | {error, term()}
 

From 098493b050648c4c83600138c87a7331ebd142f5 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Wed, 12 Oct 2016 12:16:01 -0400 Subject: [PATCH 3/3] Bump version to 0.4.8 --- src/statsderl.app.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/statsderl.app.src b/src/statsderl.app.src index 6e8ee2e..fb5d00d 100644 --- a/src/statsderl.app.src +++ b/src/statsderl.app.src @@ -1,6 +1,6 @@ {application, statsderl, [ {description, "statsd client"}, - {vsn, "0.4.7"}, + {vsn, "0.4.8"}, {registered, []}, {applications, [ kernel,