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

Document functions #27

Closed
wants to merge 3 commits 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
30 changes: 29 additions & 1 deletion doc/statsderl.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@


# Module statsderl #
* [Description](#description)
* [Data Types](#types)
* [Function Index](#index)
* [Function Details](#functions)

Functions to send data to a StatsD server.

<a name="description"></a>

## Description ##
In this module, the
SampleRate parameter is a float value between 0 and 1 that
indicates how frequently the data should actually be sent. For
counters, the server takes the sample rate into account.
<a name="types"></a>

## Data Types ##
Expand Down Expand Up @@ -44,7 +54,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>Increment a counter (identical to increment/3).</td></tr><tr><td valign="top"><a href="#decrement-3">decrement/3</a></td><td>Decrement a counter.</td></tr><tr><td valign="top"><a href="#gauge-3">gauge/3</a></td><td>Set a gauge value.</td></tr><tr><td valign="top"><a href="#gauge_decrement-3">gauge_decrement/3</a></td><td>Decrement a gauge value.</td></tr><tr><td valign="top"><a href="#gauge_increment-3">gauge_increment/3</a></td><td>Increment a gauge value.</td></tr><tr><td valign="top"><a href="#increment-3">increment/3</a></td><td>Increment a counter.</td></tr><tr><td valign="top"><a href="#timing-3">timing/3</a></td><td>Record timer information.</td></tr><tr><td valign="top"><a href="#timing_fun-3">timing_fun/3</a></td><td>Run nullary function and record the time spent.</td></tr><tr><td valign="top"><a href="#timing_now-3">timing_now/3</a></td><td>Record time spent between the timestamp passed and now.</td></tr></table>


<a name="functions"></a>
Expand All @@ -60,6 +70,8 @@ counter(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value">value()
</code></pre>
<br />

Increment a counter (identical to increment/3).

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

### decrement/3 ###
Expand All @@ -69,6 +81,8 @@ decrement(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value">value
</code></pre>
<br />

Decrement a counter.

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

### gauge/3 ###
Expand All @@ -78,6 +92,8 @@ gauge(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value">value()</
</code></pre>
<br />

Set a gauge value.

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

### gauge_decrement/3 ###
Expand All @@ -87,6 +103,8 @@ gauge_decrement(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value"
</code></pre>
<br />

Decrement a gauge value.

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

### gauge_increment/3 ###
Expand All @@ -96,6 +114,8 @@ gauge_increment(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value"
</code></pre>
<br />

Increment a gauge value.

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

### increment/3 ###
Expand All @@ -105,6 +125,8 @@ increment(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value">value
</code></pre>
<br />

Increment a counter.

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

### timing/3 ###
Expand All @@ -114,6 +136,8 @@ timing(Key::<a href="#type-key">key()</a>, Value::<a href="#type-value">value()<
</code></pre>
<br />

Record timer information.

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

### timing_fun/3 ###
Expand All @@ -123,6 +147,8 @@ timing_fun(Key::<a href="#type-key">key()</a>, Fun::function(), SampleRate::<a h
</code></pre>
<br />

Run nullary function and record the time spent.

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

### timing_now/3 ###
Expand All @@ -132,3 +158,5 @@ timing_now(Key::<a href="#type-key">key()</a>, Timestamp::<a href="erlang.md#typ
</code></pre>
<br />

Record time spent between the timestamp passed and now.

33 changes: 19 additions & 14 deletions src/statsderl.erl
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
%% @doc Functions to send data to a StatsD server. In this module, the
%% SampleRate parameter is a float value between 0 and 1 that
%% indicates how frequently the data should actually be sent. For
%% counters, the server takes the sample rate into account.

-module(statsderl).
-include("statsderl.hrl").

%% public
-export([
counter/3,
increment/3,
decrement/3,
gauge/3,
gauge_decrement/3,
gauge_increment/3,
increment/3,
timing/3,
timing_fun/3,
timing_now/3
]).

%% public
-spec counter(key(), value(), sample_rate()) -> ok.

%% @doc Increment a counter (identical to increment/3).
counter(Key, Value, SampleRate) ->
maybe_cast(counter, Key, Value, SampleRate).

-spec decrement(key(), value(), sample_rate()) -> ok.
-spec increment(key(), value(), sample_rate()) -> ok.
%% @doc Increment a counter.
increment(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(counter, Key, Value, SampleRate).

-spec decrement(key(), value(), sample_rate()) -> ok.
%% @doc Decrement a counter.
decrement(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(counter, Key, -Value, SampleRate).

-spec gauge(key(), value(), sample_rate()) -> ok.

%% @doc Set a gauge value.
gauge(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(gauge, Key, Value, SampleRate).

-spec gauge_decrement(key(), value(), sample_rate()) -> ok.

%% @doc Decrement a gauge value.
gauge_decrement(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(gauge_decrement, Key, Value, SampleRate).

-spec gauge_increment(key(), value(), sample_rate()) -> ok.

%% @doc Increment a gauge value.
gauge_increment(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(gauge_increment, Key, Value, SampleRate).

-spec increment(key(), value(), sample_rate()) -> ok.

increment(Key, Value, SampleRate) when Value >= 0 ->
maybe_cast(counter, Key, Value, SampleRate).

-spec timing(key(), value(), sample_rate()) -> ok.

%% @doc Record timer information.
timing(Key, Value, SampleRate) ->
maybe_cast(timing, Key, Value, SampleRate).

-spec timing_fun(key(), fun(), sample_rate()) -> ok.

%% @doc Run nullary function and record the time spent.
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.

%% @doc Record time spent between the timestamp passed and now.
timing_now(Key, Timestamp, SampleRate) ->
Timestamp2 = statsderl_utils:timestamp(),
Value = timer:now_diff(Timestamp2, Timestamp) div 1000,
Expand Down