1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | -module(statsd).
|
45 |
|
46 | -author("Daniel Schauenberg <d@unwiredcouch.com>").
|
47 | -author("Sean Johnson <sean@talkto.com>").
|
48 |
|
49 | -define(HOST, "localhost").
|
50 | -define(PORT, 8125).
|
51 |
|
52 | -export([increment/1,increment/2,increment/3,
|
53 | decrement/1, decrement/2, decrement/3,
|
54 | timing/2, timing/3,
|
55 | gauge/2]).
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | increment(Stat) -> increment(Stat, 1, 1.0).
|
63 | increment(Stat, Delta) when is_integer(Delta) ->
|
64 | increment(Stat, Delta, 1.0);
|
65 | increment(Stat, Samplerate) when is_float(Samplerate) ->
|
66 | increment(Stat, 1, Samplerate).
|
67 | increment(Stat, Delta, Samplerate) when is_integer(Delta), is_float(Samplerate) ->
|
68 | send({counter, Stat, Delta, Samplerate}).
|
69 |
|
70 |
|
71 | decrement(Stat) -> decrement(Stat, 1, 1.0).
|
72 | decrement(Stat, Delta) when is_integer(Delta) ->
|
73 | decrement(Stat, Delta, 1.0);
|
74 | decrement(Stat, Samplerate) when is_float(Samplerate) ->
|
75 | decrement(Stat, 1, Samplerate).
|
76 | decrement(Stat, Delta, Samplerate) when is_integer(Delta), is_float(Samplerate) ->
|
77 | send({counter, Stat, -abs(Delta), Samplerate}).
|
78 |
|
79 |
|
80 | timing(Stat, Time) when is_float(Time); is_integer(Time) ->
|
81 | timing(Stat, Time, 1.0).
|
82 | timing(Stat, Time, Samplerate) when is_float (Time); is_integer(Time),
|
83 | is_float(Samplerate) ->
|
84 | send({timer, Stat, Time, Samplerate}).
|
85 |
|
86 |
|
87 | gauge(Stat, Reading) ->
|
88 | send({gauge, Stat, Reading}).
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | send({counter, [H|Stats], Delta, Samplerate}) ->
|
96 | send({counter, H, Delta, Samplerate}),
|
97 | send({counter, Stats, Delta, Samplerate});
|
98 | send({counter, [], _, _}) -> {ok, "Success."};
|
99 |
|
100 | send({timer, [H|Stats], Delta, Samplerate}) ->
|
101 | send({timer, H, Delta, Samplerate}),
|
102 | send({timer, Stats, Delta, Samplerate});
|
103 | send({timer, [], _, _}) -> {ok, "Success."};
|
104 |
|
105 | send({gauge, [H|Stats], Reading}) ->
|
106 | send({gauge, H, Reading}),
|
107 | send({gauge, Stats, Reading});
|
108 | send({gauge, [], _}) -> {ok, "Success."};
|
109 |
|
110 |
|
111 | send({counter, Stat, Delta, Samplerate}) when Samplerate < 1.0, is_atom(Stat) ->
|
112 | Rand = random:uniform(),
|
113 | if
|
114 | Rand =< Samplerate ->
|
115 | send_udp_message(?HOST, ?PORT, io_lib:format("~p:~p|c|@~p",
|
116 | [Stat, Delta, Samplerate]));
|
117 | true -> {ok, "Success."}
|
118 | end;
|
119 |
|
120 | send({counter, Stat, Delta, _}) ->
|
121 | send_udp_message(?HOST, ?PORT, io_lib:format("~p:~p|c", [Stat, Delta]));
|
122 |
|
123 | send({timer, Stat, Time, Samplerate}) when Samplerate < 1.0 ->
|
124 | Rand = random:uniform(),
|
125 | if
|
126 | Rand =< Samplerate ->
|
127 | send_udp_message(?HOST, ?PORT, io_lib:format("~p:~p|ms|@~p",
|
128 | [Stat, Time, Samplerate]));
|
129 | true -> {ok, "Success."}
|
130 | end;
|
131 |
|
132 | send({timer, Stat, Time, _}) ->
|
133 | send_udp_message(?HOST, ?PORT, io_lib:format("~p:~p|ms", [Stat, Time]));
|
134 |
|
135 | send({gauge, Stat, Reading}) ->
|
136 | send_udp_message(?HOST, ?PORT, io_lib:format("~p:~p|g", [Stat, Reading])).
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | send_udp_message(Host, Port, Msg) when is_integer(Port),
|
142 | is_list(Host) ->
|
143 | {ok, Socket} = gen_udp:open(0, [binary]),
|
144 | ok = gen_udp:send(Socket, Host, Port, Msg),
|
145 | gen_udp:close(Socket),
|
146 | {ok, "Success."}.
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | -ifdef(TEST).
|
153 | -include_lib("eunit/include/eunit.hrl").
|
154 |
|
155 | increment_test_() ->
|
156 | [?_assert(increment(stat1) =:= {ok, "Success."}),
|
157 | ?_assert(increment(stat1, 1) =:= {ok, "Success."}),
|
158 | ?_assert(increment(stat1, 1, 0.5) =:= {ok, "Success."}),
|
159 | ?_assert(increment([stat1, stat2]) =:= {ok, "Success."}),
|
160 | ?_assert(increment([stat1, stat2], 1) =:= {ok, "Success."}),
|
161 | ?_assert(increment([stat1, stat2], 1, 0.5) =:= {ok, "Success."})
|
162 | ].
|
163 | decrement_test_() ->
|
164 | [?_assert(decrement(stat1) =:= {ok, "Success."}),
|
165 | ?_assert(decrement(stat1, 1) =:= {ok, "Success."}),
|
166 | ?_assert(decrement(stat1, -1) =:= {ok, "Success."}),
|
167 | ?_assert(decrement(stat1, 1, 0.5) =:= {ok, "Success."}),
|
168 | ?_assert(decrement([stat1, stat2]) =:= {ok, "Success."}),
|
169 | ?_assert(decrement([stat1, stat2], 1) =:= {ok, "Success."}),
|
170 | ?_assert(decrement([stat1, stat2], -1) =:= {ok, "Success."}),
|
171 | ?_assert(decrement([stat1, stat2], 1, 0.5) =:= {ok, "Success."})
|
172 | ].
|
173 | timing_test_() ->
|
174 | [
|
175 | ?_assert(timing(stat1, 100) =:= {ok, "Success."}),
|
176 | ?_assert(timing(stat1, 100, 0.5) =:= {ok, "Success."}),
|
177 | ?_assert(timing([stat1, stat2], 100) =:= {ok, "Success."}),
|
178 | ?_assert(timing([stat1, stat2], 100, 0.5) =:= {ok, "Success."})
|
179 | ].
|
180 | gauge_test_() ->
|
181 | [
|
182 | ?_assert(gauge(stat1, 100) =:= {ok, "Success."}),
|
183 | ?_assert(gauge([stat1, stat2], 100) =:= {ok, "Success."})
|
184 | ].
|
185 |
|
186 | -endif. |
\ | No newline at end of file |