diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d304ff3 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/lib/statix.ex b/lib/statix.ex index dff5a5c..52ef229 100644 --- a/lib/statix.ex +++ b/lib/statix.ex @@ -82,7 +82,7 @@ defmodule Statix do alias __MODULE__.Conn @type key :: iodata - @type options :: [sample_rate: float, tags: [String.t]] + @type options :: [sample_rate: float, tags: [String.t()]] @type on_send :: :ok | {:error, term} @doc """ @@ -150,12 +150,12 @@ defmodule Statix do :ok """ - @callback gauge(key, value :: String.Chars.t, options) :: on_send + @callback gauge(key, value :: String.Chars.t(), options) :: on_send @doc """ Same as `gauge(key, value, [])`. """ - @callback gauge(key, value :: String.Chars.t) :: on_send + @callback gauge(key, value :: String.Chars.t()) :: on_send @doc """ Writes `value` to the histogram identified by `key`. @@ -169,12 +169,12 @@ defmodule Statix do :ok """ - @callback histogram(key, value :: String.Chars.t, options) :: on_send + @callback histogram(key, value :: String.Chars.t(), options) :: on_send @doc """ Same as `histogram(key, value, [])`. """ - @callback histogram(key, value :: String.Chars.t) :: on_send + @callback histogram(key, value :: String.Chars.t()) :: on_send @doc """ Writes the given `value` to the StatsD timing identified by `key`. @@ -187,12 +187,12 @@ defmodule Statix do :ok """ - @callback timing(key, value :: String.Chars.t, options) :: on_send + @callback timing(key, value :: String.Chars.t(), options) :: on_send @doc """ Same as `timing(key, value, [])`. """ - @callback timing(key, value :: String.Chars.t) :: on_send + @callback timing(key, value :: String.Chars.t()) :: on_send @doc """ Writes the given `value` to the StatsD set identified by `key`. @@ -203,12 +203,12 @@ defmodule Statix do :ok """ - @callback set(key, value :: String.Chars.t, options) :: on_send + @callback set(key, value :: String.Chars.t(), options) :: on_send @doc """ Same as `set(key, value, [])`. """ - @callback set(key, value :: String.Chars.t) :: on_send + @callback set(key, value :: String.Chars.t()) :: on_send @doc """ Measures the execution time of the given `function` and writes that to the @@ -257,14 +257,16 @@ defmodule Statix do def connect() do conn = @statix_conn current_conn = Statix.new_conn(__MODULE__) + if conn.header != current_conn.header do - message = + raise( "the current configuration for #{inspect(__MODULE__)} differs from " <> - "the one that was given during the compilation.\n" <> - "Be sure to use :runtime_config option " <> - "if you want to have different configurations" - raise message + "the one that was given during the compilation.\n" <> + "Be sure to use :runtime_config option " <> + "if you want to have different configurations" + ) end + Statix.open_conn(conn) :ok end @@ -289,7 +291,7 @@ defmodule Statix do Statix.transmit(current_conn(), :counter, key, [?-, to_string(val)], options) end - def gauge(key, val, options \\ [] ) do + def gauge(key, val, options \\ []) do Statix.transmit(current_conn(), :gauge, key, val, options) end @@ -313,7 +315,15 @@ defmodule Statix do Statix.transmit(current_conn(), :set, key, val, options) end - defoverridable [increment: 3, decrement: 3, gauge: 3, histogram: 3, timing: 3, measure: 3, set: 3] + defoverridable( + increment: 3, + decrement: 3, + gauge: 3, + histogram: 3, + timing: 3, + measure: 3, + set: 3 + ) end end @@ -335,6 +345,7 @@ defmodule Statix do def transmit(conn, type, key, val, options) when (is_binary(key) or is_list(key)) and is_list(options) 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) else @@ -346,6 +357,7 @@ defmodule Statix do {env2, env1} = Application.get_all_env(:statix) |> Keyword.pop(module, []) + {prefix1, env1} = Keyword.pop_first(env1, :prefix) {prefix2, env2} = Keyword.pop_first(env2, :prefix) env = Keyword.merge(env1, env2) diff --git a/lib/statix/conn.ex b/lib/statix/conn.ex index 8dc4c77..2eb623f 100644 --- a/lib/statix/conn.ex +++ b/lib/statix/conn.ex @@ -16,7 +16,7 @@ defmodule Statix.Conn do end def open(%__MODULE__{} = conn) do - {:ok, sock} = :gen_udp.open(0, [active: false]) + {:ok, sock} = :gen_udp.open(0, active: false) %__MODULE__{conn | sock: sock} end @@ -28,11 +28,12 @@ defmodule Statix.Conn do defp transmit(packet, sock) do Port.command(sock, packet) + receive do {:inet_reply, _port, status} -> status end end - + if Version.match?(System.version(), ">= 1.3.0") do defp string_to_charlist(string), do: String.to_charlist(string) else diff --git a/lib/statix/packet.ex b/lib/statix/packet.ex index c163246..adc10b2 100644 --- a/lib/statix/packet.ex +++ b/lib/statix/packet.ex @@ -7,18 +7,19 @@ defmodule Statix.Packet do @addr_family if(otp_release >= '19', do: [1], else: []) def header({n1, n2, n3, n4}, port) do - @addr_family ++ [ - band(bsr(port, 8), 0xFF), - band(port, 0xFF), - band(n1, 0xFF), - band(n2, 0xFF), - band(n3, 0xFF), - band(n4, 0xFF) - ] + @addr_family ++ + [ + band(bsr(port, 8), 0xFF), + band(port, 0xFF), + band(n1, 0xFF), + band(n2, 0xFF), + band(n3, 0xFF), + band(n4, 0xFF) + ] end def build(header, name, key, val, options) do - [header, key, ?:, val, ?|, metric_type(name)] + [header, key, ?:, val, ?|, metric_type(name)] |> set_option(:sample_rate, options[:sample_rate]) |> set_option(:tags, options[:tags]) end @@ -30,6 +31,7 @@ defmodule Statix.Packet do timing: "ms", set: "s" } + for {name, type} <- metrics do defp metric_type(unquote(name)), do: unquote(type) end diff --git a/mix.exs b/mix.exs index 5624e1f..0402626 100644 --- a/mix.exs +++ b/mix.exs @@ -9,7 +9,7 @@ defmodule Statix.Mixfile do elixir: "~> 1.2", description: description(), package: package(), - deps: deps(), + deps: deps() ] end @@ -25,7 +25,7 @@ defmodule Statix.Mixfile do [ maintainers: ["Aleksei Magusev", "Andrea Leopardi"], licenses: ["ISC"], - links: %{"GitHub" => "https://github.com/lexmag/statix"}, + links: %{"GitHub" => "https://github.com/lexmag/statix"} ] end diff --git a/test/statix_test.exs b/test/statix_test.exs index 0ba5d25..de9b83b 100644 --- a/test/statix_test.exs +++ b/test/statix_test.exs @@ -32,9 +32,12 @@ defmodule StatixTest do end runtime_config? = System.get_env("STATIX_TEST_RUNTIME_CONFIG") in ["1", "true"] - content = quote do - use Statix, runtime_config: unquote(runtime_config?) - end + + content = + quote do + use Statix, runtime_config: unquote(runtime_config?) + end + Module.create(TestStatix, content, Macro.Env.location(__ENV__)) defmodule OverridingStatix do @@ -76,8 +79,8 @@ defmodule StatixTest do setup do :ok = Server.set_current_test(self()) - TestStatix.connect - OverridingStatix.connect + TestStatix.connect() + OverridingStatix.connect() on_exit(fn -> Server.set_current_test(nil) end) end @@ -179,16 +182,20 @@ defmodule StatixTest do test "measure/2,3" do expected = "the stuff" - result = TestStatix.measure(["sample"], fn -> - :timer.sleep(100) - expected - end) + + result = + TestStatix.measure(["sample"], fn -> + :timer.sleep(100) + expected + end) + assert_receive {:server, <<"sample:10", _, "|ms">>} assert result == expected TestStatix.measure("sample", [sample_rate: 1.0, tags: ["foo", "bar"]], fn -> :timer.sleep(100) end) + assert_receive {:server, <<"sample:10", _, "|ms|@1.0|#foo,bar">>} refute_received _any @@ -231,6 +238,7 @@ defmodule StatixTest do OverridingStatix.measure("sample", [tags: ["foo"]], fn -> :timer.sleep(100) end) + assert_receive {:server, <<"sample-measure-overridden:10", _, "|ms|#foo">>} OverridingStatix.set("sample", 3, tags: ["foo"])