Skip to content

Commit

Permalink
Merge pull request #58 from lpgauth/feature/shackle
Browse files Browse the repository at this point in the history
Re-write statsderl using shackle
  • Loading branch information
lpgauth authored Aug 18, 2020
2 parents 5fcc8fc + 975cb6a commit 3d34380
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 225 deletions.
Binary file modified bin/rebar3
Binary file not shown.
9 changes: 6 additions & 3 deletions include/statsderl.hrl
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
%% macros
-define(APP, statsderl).
-define(CHILD(Name, Mod), {Name, {Mod, start_link, [Name]}, permanent, 5000, worker, [Mod]}).
-define(CLIENT, statsderl_client).
-define(MAX_UNSIGNED_INT_32, 4294967295).

%% env vars
-define(ENV(Key, Default), application:get_env(?APP, Key, Default)).
-define(ENV_BASEKEY, base_key).
-define(ENV_HOSTNAME, hostname).
-define(ENV_PORT, port).
-define(ENV_VARS, [?ENV_BASEKEY, ?ENV_HOSTNAME, ?ENV_PORT]).
-define(MAX_UNSIGNED_INT_32, 4294967295).
-define(SERVER, statsderl_server).

%% defaults
-define(DEFAULT_BACKLOG_SIZE, 4096).
-define(DEFAULT_BASEKEY, undefined).
-define(DEFAULT_HOSTNAME, {127, 0, 0, 1}).
-define(DEFAULT_HOSTNAME, "127.0.0.1").
-define(DEFAULT_POOL_SIZE, 4).
-define(DEFAULT_PORT, 8125).

Expand Down
7 changes: 4 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{cover_export_enabled, true}.

{deps, [
{foil, "0.1.0"},
{granderl, "0.1.5"},
{metal, "0.1.1"},
{parse_trans, "3.0.0"}
{parse_trans, "3.0.0"},
{shackle, "0.6.11"}
]}.

{edoc_opts, [
Expand All @@ -21,6 +20,8 @@
debug_info
]}.

{plugins, [rebar3_hex]}.

{profiles, [
{compile, [
{erl_opts, [
Expand Down
8 changes: 3 additions & 5 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ case erlang:function_exported(rebar3, main, 1) of
CONFIG;
false ->
[{deps, [
{foil, ".*",
{git, "https://github.com/lpgauth/foil.git", {tag, "0.1.0"}}},
{granderl, ".*",
{git, "https://github.com/tokenrove/granderl.git", {tag, "v0.1.5"}}},
{metal, ".*",
{git, "https://github.com/lpgauth/metal.git", {tag, "0.1.1"}}},
{parse_trans, ".*",
{git, "https://github.com/uwiger/parse_trans.git", {tag, "3.0.0"}}}
{git, "https://github.com/uwiger/parse_trans.git", {tag, "3.0.0"}}},
{shackle, ".*",
{git, "https://github.com/lpgauth/shackle.git", {tag, "0.6.11"}}}
]} | lists:keydelete(deps, 1, CONFIG)]
end.
6 changes: 4 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
[{<<"foil">>,{pkg,<<"foil">>,<<"0.1.0">>},0},
{<<"granderl">>,{pkg,<<"granderl">>,<<"0.1.5">>},0},
{<<"metal">>,{pkg,<<"metal">>,<<"0.1.1">>},0},
{<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.0.0">>},0}]}.
{<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.0.0">>},0},
{<<"shackle">>,{pkg,<<"shackle">>,<<"0.6.11">>},0}]}.
[
{pkg_hash,[
{<<"foil">>, <<"16406493144743F633505E51406F7BE6BF17A3772DFD62BCDE3107C654704BEE">>},
{<<"granderl">>, <<"F20077A68BD80B8D8783BD15A052813C6483771DEC1A5B837D307CBE92F14122">>},
{<<"metal">>, <<"5D3D1322DA7BCD34B94FED5486F577973685298883954F7A3E517EF5EF6953F5">>},
{<<"parse_trans">>, <<"9E96B1C9C3A0DF54E7B76F8F685D38BFA1EB21B31E042B1D1A5A70258E4DB1E3">>}]}
{<<"parse_trans">>, <<"9E96B1C9C3A0DF54E7B76F8F685D38BFA1EB21B31E042B1D1A5A70258E4DB1E3">>},
{<<"shackle">>, <<"949245E1AE690DECAD887B046CCCA595E5F3FC798A317131466985F5BBA64298">>}]}
].
2 changes: 1 addition & 1 deletion src/statsderl.app.src
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{application, statsderl, [
{applications, [kernel, stdlib, granderl, foil, metal]},
{applications, [kernel, stdlib, granderl, shackle]},
{description, "High Performance Erlang StatsD Client"},
{env, []},
{licenses, ["MIT"]},
Expand Down
18 changes: 9 additions & 9 deletions src/statsderl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@
ok.

counter(Key, Value, Rate) ->
statsderl_pool:sample(Rate, {counter, Key, Value, Rate}).
statsderl_sample:rate(Rate, {counter, Key, Value, Rate}).

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

decrement(Key, Value, Rate) when Value >= 0 ->
statsderl_pool:sample(Rate, {counter, Key, -Value, Rate}).
statsderl_sample:rate(Rate, {counter, Key, -Value, Rate}).

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

gauge(Key, Value, Rate) when Value >= 0 ->
statsderl_pool:sample(Rate, {gauge, Key, Value}).
statsderl_sample:rate(Rate, {gauge, Key, Value}).

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

gauge_decrement(Key, Value, Rate) when Value >= 0 ->
statsderl_pool:sample(Rate, {gauge_decrement, Key, Value}).
statsderl_sample:rate(Rate, {gauge_decrement, Key, Value}).

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

gauge_increment(Key, Value, Rate) when Value >= 0 ->
statsderl_pool:sample(Rate, {gauge_increment, Key, Value}).
statsderl_sample:rate(Rate, {gauge_increment, Key, Value}).

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

increment(Key, Value, Rate) when Value >= 0 ->
statsderl_pool:sample(Rate, {counter, Key, Value, Rate}).
statsderl_sample:rate(Rate, {counter, Key, Value, Rate}).

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

timing(Key, Value, Rate) ->
statsderl_pool:sample(Rate, {timing, Key, Value}).
statsderl_sample:rate(Rate, {timing, Key, Value}).

-spec timing_fun(key(), fun(), sample_rate()) ->
any().
Expand All @@ -74,10 +74,10 @@ timing_fun(Key, Fun, Rate) ->
ok.

timing_now(Key, Timestamp, Rate) ->
statsderl_pool:sample(Rate, {timing_now, Key, Timestamp}).
statsderl_sample:rate(Rate, {timing_now, Key, Timestamp}).

-spec timing_now_us(key(), erlang:timestamp(), sample_rate()) ->
ok.

timing_now_us(Key, Timestamp, Rate) ->
statsderl_pool:sample(Rate, {timing_now_us, Key, Timestamp}).
statsderl_sample:rate(Rate, {timing_now_us, Key, Timestamp}).
1 change: 1 addition & 0 deletions src/statsderl_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ start(_StartType, _StartArgs) ->
ok.

stop(_State) ->
shackle_pool:stop(?APP),
ok.
64 changes: 64 additions & 0 deletions src/statsderl_client.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-module(statsderl_client).
-include("statsderl.hrl").

-compile(inline).
-compile({inline_size, 512}).

-behavior(shackle_client).
-export([
init/1,
setup/2,
handle_request/2,
handle_data/2,
terminate/1
]).

-record(state, {
base_key :: iodata()
}).

-type state() :: #state {}.

%% shackle_server callbacks
-spec init(undefined) ->
{ok, state()}.

init(_Opts) ->
BaseKey = ?ENV(?ENV_BASEKEY, ?DEFAULT_BASEKEY),

{ok, #state {
base_key = statsderl_utils:base_key(BaseKey)
}}.

-spec setup(inet:socket(), state()) ->
{ok, state()}.

setup(_Socket, State) ->
{ok, State}.

-spec handle_request(term(), state()) ->
{ok, undefined, iolist(), state()}.

handle_request({cast, Data}, #state {
base_key = BaseKey
} = State) ->

{ok, undefined, [BaseKey, Data], State};
handle_request(Request, #state {
base_key = BaseKey
} = State) ->

Data = statsderl_protocol:encode(Request),

{ok, undefined, [BaseKey, Data], State}.

-spec handle_data(binary(), state()) ->
{ok, [], state()}.

handle_data(_Data, State) ->
{ok, [], State}.

-spec terminate(state()) -> ok.

terminate(_State) ->
ok.
114 changes: 0 additions & 114 deletions src/statsderl_pool.erl

This file was deleted.

48 changes: 48 additions & 0 deletions src/statsderl_sample.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-module(statsderl_sample).
-include("statsderl.hrl").

-compile(inline).
-compile({inline_size, 512}).

-export([
rate/2,
rate_scaled/2
]).

%% public
-spec rate(sample_rate(), operation()) ->
ok.

rate(1, Operation) ->
operation(Operation);
rate(1.0, Operation) ->
operation(Operation);
rate(Rate, Operation) ->
RateInt = trunc(Rate * ?MAX_UNSIGNED_INT_32),
rate_scaled(RateInt, Operation).

-spec rate_scaled(non_neg_integer(), operation()) ->
ok.

rate_scaled(RateInt, Operation) ->
Rand = granderl:uniform(?MAX_UNSIGNED_INT_32),
case Rand =< RateInt of
true ->
operation(Operation);
false ->
ok
end.

%% private
cast(Request) ->
shackle:cast(?APP, Request, undefined),
ok.

operation({timing_now, Key, Value}) ->
Value2 = statsderl_utils:timing_now(Value),
cast({timing, Key, Value2});
operation({timing_now_us, Key, Value}) ->
Value2 = statsderl_utils:timing_now_us(Value),
cast({timing, Key, Value2});
operation(Operation) ->
cast(Operation).
Loading

0 comments on commit 3d34380

Please sign in to comment.