Skip to content

Commit

Permalink
Merge pull request #35 from lpgauth/fall-cleanup
Browse files Browse the repository at this point in the history
Fall cleanup
  • Loading branch information
lpgauth authored Nov 4, 2016
2 parents 846b086 + 098493b commit 644c054
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
11 changes: 10 additions & 1 deletion doc/statsderl.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ value() = number()
## Function Index ##


<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#counter-3">counter/3</a></td><td></td></tr><tr><td valign="top"><a href="#decrement-3">decrement/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge-3">gauge/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge_decrement-3">gauge_decrement/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge_increment-3">gauge_increment/3</a></td><td></td></tr><tr><td valign="top"><a href="#increment-3">increment/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing-3">timing/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing_fun-3">timing_fun/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing_now-3">timing_now/3</a></td><td></td></tr></table>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#counter-3">counter/3</a></td><td></td></tr><tr><td valign="top"><a href="#decrement-3">decrement/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge-3">gauge/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge_decrement-3">gauge_decrement/3</a></td><td></td></tr><tr><td valign="top"><a href="#gauge_increment-3">gauge_increment/3</a></td><td></td></tr><tr><td valign="top"><a href="#increment-3">increment/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing-3">timing/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing_fun-3">timing_fun/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing_now-3">timing_now/3</a></td><td></td></tr><tr><td valign="top"><a href="#timing_now_us-3">timing_now_us/3</a></td><td></td></tr></table>


<a name="functions"></a>
Expand Down Expand Up @@ -132,3 +132,12 @@ timing_now(Key::<a href="#type-key">key()</a>, Timestamp::<a href="erlang.md#typ
</code></pre>
<br />

<a name="timing_now_us-3"></a>

### timing_now_us/3 ###

<pre><code>
timing_now_us(Key::<a href="#type-key">key()</a>, Timestamp::<a href="erlang.md#type-timestamp">erlang:timestamp()</a>, SampleRate::<a href="#type-sample_rate">sample_rate()</a>) -&gt; ok
</code></pre>
<br />

2 changes: 1 addition & 1 deletion doc/statsderl_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ __Behaviours:__ [`application`](application.md).
### start/0 ###

<pre><code>
start() -&gt; ok | {error, atom()}
start() -&gt; {ok, [atom()]} | {error, term()}
</code></pre>
<br />

Expand Down
2 changes: 1 addition & 1 deletion src/statsderl.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, statsderl, [
{description, "statsd client"},
{vsn, "0.4.7"},
{vsn, "0.4.8"},
{registered, []},
{applications, [
kernel,
Expand Down
103 changes: 51 additions & 52 deletions src/statsderl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,107 +19,106 @@
]).

%% 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(),
Result = Fun(),
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) ->
Expand Down

0 comments on commit 644c054

Please sign in to comment.