From 98ee8df694cf7d6862dca27256856a6535342fc2 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 7 Nov 2018 11:48:32 -0800 Subject: [PATCH] Add support for global tags (#19) --- lib/statix.ex | 16 +++++++++++++++- lib/statix/packet.ex | 2 ++ test/statix_test.exs | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/statix.ex b/lib/statix.ex index 52ef229..182bdbe 100644 --- a/lib/statix.ex +++ b/lib/statix.ex @@ -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 @@ -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 @@ -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 diff --git a/lib/statix/packet.ex b/lib/statix/packet.ex index adc10b2..96a269a 100644 --- a/lib/statix/packet.ex +++ b/lib/statix/packet.ex @@ -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 diff --git a/test/statix_test.exs b/test/statix_test.exs index de9b83b..6d14837 100644 --- a/test/statix_test.exs +++ b/test/statix_test.exs @@ -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