Skip to content

Commit

Permalink
Run the formatter on the codebase (lexmag#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
raksonibs authored and lexmag committed Nov 5, 2018
1 parent 10b5cf9 commit d436a19
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
44 changes: 28 additions & 16 deletions lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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`.
Expand All @@ -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`.
Expand All @@ -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`.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/statix/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
20 changes: 11 additions & 9 deletions lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Statix.Mixfile do
elixir: "~> 1.2",
description: description(),
package: package(),
deps: deps(),
deps: deps()
]
end

Expand All @@ -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

Expand Down
26 changes: 17 additions & 9 deletions test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit d436a19

Please sign in to comment.