Skip to content

Commit

Permalink
Add support for global tags (lexmag#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt authored and lexmag committed Nov 7, 2018
1 parent faae367 commit 98ee8df
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ defmodule Statix do
Defaults to `"127.0.0.1"`.
* `:port` - (integer) the port (on `:host`) where the StatsD-compatible
server is running. Defaults to `8125`.
* `:tags` - ([binary]) a list of global tags that will be sent with all
metrics. By default this option is not present.
By default, the configuration is evaluated once, at compile time. If you plan
on changing the configuration at runtime, you must specify the
Expand Down Expand Up @@ -347,7 +349,7 @@ defmodule Statix do
sample_rate = Keyword.get(options, :sample_rate)

if is_nil(sample_rate) or sample_rate >= :rand.uniform() do
Conn.transmit(conn, type, key, to_string(val), options)
Conn.transmit(conn, type, key, to_string(val), put_global_tags(conn.sock, options))
else
:ok
end
Expand Down Expand Up @@ -376,4 +378,16 @@ defmodule Statix do
{_p1, _p2} -> [part1, ?., part2, ?.]
end
end

defp put_global_tags(module, options) do
conn_tags =
:statix
|> Application.get_env(module, [])
|> Keyword.get(:tags, [])

app_tags = Application.get_env(:statix, :tags, [])
global_tags = conn_tags ++ app_tags

Keyword.update(options, :tags, global_tags, &(&1 ++ global_tags))
end
end
2 changes: 2 additions & 0 deletions lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ defmodule Statix.Packet do
[packet | ["|@", :erlang.float_to_binary(sample_rate, [:compact, decimals: 2])]]
end

defp set_option(packet, :tags, []), do: packet

defp set_option(packet, :tags, tags) when is_list(tags) do
[packet | ["|#", Enum.join(tags, ",")]]
end
Expand Down
24 changes: 24 additions & 0 deletions test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,28 @@ defmodule StatixTest do
OverridingStatix.set("sample", 3, tags: ["foo"])
assert_receive {:server, "sample-overridden:3|s|#foo"}
end

test "sends global tags when present" do
Application.put_env(:statix, :tags, ["tag:test"])

TestStatix.increment("sample", 3)
assert_receive {:server, "sample:3|c|#tag:test"}

TestStatix.increment("sample", 3, tags: ["foo"])
assert_receive {:server, "sample:3|c|#foo,tag:test"}
after
Application.delete_env(:statix, :tags)
end

test "sends global connection-specific tags" do
Application.put_env(:statix, TestStatix, tags: ["tag:test"])

TestStatix.increment("sample", 3)
assert_receive {:server, "sample:3|c|#tag:test"}

TestStatix.increment("sample", 3, tags: ["foo"])
assert_receive {:server, "sample:3|c|#foo,tag:test"}
after
Application.delete_env(:statix, TestStatix)
end
end

0 comments on commit 98ee8df

Please sign in to comment.