Skip to content

Commit

Permalink
Add sample rate support
Browse files Browse the repository at this point in the history
  • Loading branch information
fertel authored and lexmag committed Oct 31, 2016
1 parent 5463396 commit 5908cd2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ defmodule Statix do
end

def transmit(conn, type, key, val, options \\ []) when is_binary(key) or is_list(key) do
Statix.Conn.transmit(conn, type, key, to_string(val), options)
if Keyword.get(options, :sample_rate, 1.0) >= :rand.uniform() do
Statix.Conn.transmit(conn, type, key, to_string(val), options)
else
:ok
end
end

def config(module) do
Expand Down
13 changes: 9 additions & 4 deletions lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ defmodule Statix.Packet do
end

def build(header, name, key, val, options) do
[header, key, ?:, val, ?| , metric_type(name)]
|> set_tags_option(options[:tags])
[header, key, ?:, val, ?|, metric_type(name)]
|> set_option(:sample_rate, options[:sample_rate])
|> set_option(:tags, options[:tags])
end

metrics = %{
Expand All @@ -31,11 +32,15 @@ defmodule Statix.Packet do
defp metric_type(unquote(name)), do: unquote(type)
end

defp set_tags_option(packet, nil) do
defp set_option(packet, _kind, nil) do
packet
end

defp set_tags_option(packet, tags) do
defp set_option(packet, :sample_rate, sample_rate) do
[packet | ["|@", :erlang.float_to_binary(sample_rate, [:compact, decimals: 2])]]
end

defp set_option(packet, :tags, tags) do
[packet | ["|#", Enum.join(tags, ",")]]
end
end
38 changes: 35 additions & 3 deletions test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ defmodule StatixTest do
Sample.increment("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|c|#foo:bar,baz"}

Sample.increment("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:server, "sample:3|c|@1.0|#foo,bar"}

Sample.increment("sample", 3, sample_rate: 0.0)

refute_received _any
end

Expand All @@ -61,6 +66,11 @@ defmodule StatixTest do

Sample.decrement("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:-3|c|#foo:bar,baz"}
Sample.decrement("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])

assert_receive {:server, "sample:-3|c|@1.0|#foo,bar"}

Sample.decrement("sample", 3, sample_rate: 0.0)

refute_received _any
end
Expand All @@ -75,6 +85,11 @@ defmodule StatixTest do
Sample.gauge("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|g|#foo:bar,baz"}

Sample.gauge("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:server, "sample:3|g|@1.0|#foo,bar"}

Sample.gauge("sample", 3, sample_rate: 0.0)

refute_received _any
end

Expand All @@ -88,6 +103,11 @@ defmodule StatixTest do
Sample.histogram("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|h|#foo:bar,baz"}

Sample.histogram("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:server, "sample:3|h|@1.0|#foo,bar"}

Sample.histogram("sample", 3, sample_rate: 0.0)

refute_received _any
end

Expand All @@ -101,10 +121,15 @@ defmodule StatixTest do
Sample.timing("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|ms|#foo:bar,baz"}

Sample.timing("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:server, "sample:3|ms|@1.0|#foo,bar"}

Sample.timing("sample", 3, sample_rate: 0.0)

refute_received _any
end

test "measure/2" do
test "measure/2,3" do
expected_result = "the stuff."
fun_result = Sample.measure(["sample"], fn ->
:timer.sleep(100)
Expand All @@ -113,10 +138,12 @@ defmodule StatixTest do
assert_receive {:server, <<"sample:10", _, "|ms">>}
assert fun_result == expected_result

Sample.measure("sample", [tags: ["foo:bar", "baz"]], fn ->
Sample.measure("sample", [sample_rate: 1.0, tags: ["foo", "bar"]], fn ->
:timer.sleep(100)
end)
assert_receive {:server, <<"sample:10", _, "|ms|#foo:bar,baz">>}
assert_receive {:server, <<"sample:10", _, "|ms|@1.0|#foo,bar">>}

refute_received _any
end

test "set/2,3" do
Expand All @@ -129,6 +156,11 @@ defmodule StatixTest do
Sample.set("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|s|#foo:bar,baz"}

Sample.set("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:server, "sample:3|s|@1.0|#foo,bar"}

Sample.set("sample", 3, sample_rate: 0.0)

refute_received _any
end
end

0 comments on commit 5908cd2

Please sign in to comment.