Skip to content
Open
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
13 changes: 9 additions & 4 deletions lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ defmodule Statix.Packet do
defp metric_type(unquote(name)), do: unquote(type)
end

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

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

defp set_option(packet, :tags, tags) when is_list(tags) do
[packet | ["|#", Enum.join(tags, ",")]]
[packet | ["|#", Enum.join(normalize_tags(tags), ",")]]
end

defp normalize_tags(tags) do
Enum.map(tags, fn
{name, value} -> "#{name}:#{value}"
value -> to_string(value)
end)
end
end
20 changes: 19 additions & 1 deletion test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ defmodule StatixTest do
TestStatix.increment("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|c|#foo:bar,baz"}

TestStatix.increment("sample", 3, tags: ["baz", foo: "bar"])
assert_receive {:server, "sample:3|c|#baz,foo:bar"}

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

Expand All @@ -114,8 +117,11 @@ defmodule StatixTest do

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

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

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

TestStatix.decrement("sample", 3, sample_rate: 0.0)
Expand All @@ -133,6 +139,9 @@ defmodule StatixTest do
TestStatix.gauge("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|g|#foo:bar,baz"}

TestStatix.gauge("sample", 3, tags: ["baz", foo: "bar"])
assert_receive {:server, "sample:3|g|#baz,foo:bar"}

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

Expand All @@ -151,6 +160,9 @@ defmodule StatixTest do
TestStatix.histogram("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|h|#foo:bar,baz"}

TestStatix.histogram("sample", 3, tags: ["baz", foo: "bar"])
assert_receive {:server, "sample:3|h|#baz,foo:bar"}

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

Expand All @@ -169,6 +181,9 @@ defmodule StatixTest do
TestStatix.timing("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|ms|#foo:bar,baz"}

TestStatix.timing("sample", 3, tags: ["baz", foo: "bar"])
assert_receive {:server, "sample:3|ms|#baz,foo:bar"}

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

Expand Down Expand Up @@ -204,6 +219,9 @@ defmodule StatixTest do
TestStatix.set("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|s|#foo:bar,baz"}

TestStatix.set("sample", 3, tags: ["baz", foo: "bar"])
assert_receive {:server, "sample:3|s|#baz,foo:bar"}

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

Expand Down