Skip to content

Commit

Permalink
Fix prefix building when using connect options
Browse files Browse the repository at this point in the history
  • Loading branch information
lexmag committed Apr 27, 2020
1 parent b4a8b4e commit 00c9d69
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

* Fixed prefix building when connect options provided.

## v1.4.0

* Added support for connection pooling that is configurable via the `:pool_size` option.
Expand Down
66 changes: 30 additions & 36 deletions lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ defmodule Statix do
@doc """
Opens the connection to the StatsD-compatible server.
The configuration is read from the configuration for the `:statix` application
The configuration is read from the environment for the `:statix` application
(both globally and per connection).
The given `config` overrides the configuration read from the application environment.
The given `options` override the configuration read from the application environment.
"""
@callback connect(config :: keyword) :: :ok
@callback connect(options :: keyword) :: :ok

@doc """
Increments the StatsD counter identified by `key` by the given `value`.
Expand Down Expand Up @@ -267,8 +267,8 @@ defmodule Statix do
quote do
@statix_key Module.concat(__MODULE__, :__statix__)

def connect(config \\ []) do
statix = Statix.new(__MODULE__, config)
def connect(options \\ []) do
statix = Statix.new(__MODULE__, options)
Application.put_env(:statix, @statix_key, statix)

Statix.open(statix)
Expand All @@ -285,8 +285,8 @@ defmodule Statix do
quote do
@statix Statix.new(__MODULE__, [])

def connect(config \\ []) do
if @statix != Statix.new(__MODULE__, config) do
def connect(options \\ []) do
if @statix != Statix.new(__MODULE__, options) do
raise(
"the current configuration for #{inspect(__MODULE__)} differs from " <>
"the one that was given during the compilation.\n" <>
Expand Down Expand Up @@ -357,12 +357,8 @@ defmodule Statix do
defstruct [:conn, :tags, :pool]

@doc false
def new(module, config) do
config =
module
|> get_config()
|> Map.merge(Map.new(config))

def new(module, options) do
config = get_config(module, options)
conn = Conn.new(config.host, config.port)
header = IO.iodata_to_binary([conn.header | config.prefix])

Expand Down Expand Up @@ -411,40 +407,38 @@ defmodule Statix do
defp pick_name([name]), do: name
defp pick_name(pool), do: Enum.random(pool)

defp get_config(module) do
{conn_env, global_env} =
defp get_config(module, overrides) do
{module_env, global_env} =
:statix
|> Application.get_all_env()
|> Keyword.pop(module, [])

{global_prefix, global_env} = Keyword.pop_first(global_env, :prefix)
{conn_prefix, conn_env} = Keyword.pop_first(conn_env, :prefix)
prefix = build_prefix(global_prefix, conn_prefix)

{global_tags, global_env} = Keyword.pop_first(global_env, :tags, [])
{conn_tags, conn_env} = Keyword.pop_first(conn_env, :tags, [])
tags = global_tags ++ conn_tags
env = module_env ++ global_env
options = overrides ++ env

env = Keyword.merge(global_env, conn_env)
host = Keyword.get(env, :host, "127.0.0.1")
port = Keyword.get(env, :port, 8125)
pool_size = Keyword.get(env, :pool_size, 1)
tags =
Keyword.get_lazy(overrides, :tags, fn ->
env |> Keyword.get_values(:tags) |> Enum.concat()
end)

%{
prefix: prefix,
host: host,
port: port,
pool_size: pool_size,
prefix: build_prefix(env, overrides),
host: Keyword.get(options, :host, "127.0.0.1"),
port: Keyword.get(options, :port, 8125),
pool_size: Keyword.get(options, :pool_size, 1),
tags: tags
}
end

defp build_prefix(global_part, conn_part) do
case {global_part, conn_part} do
{nil, nil} -> ""
{_, nil} -> [global_part, ?.]
{nil, _} -> [conn_part, ?.]
{_, _} -> [global_part, ?., conn_part, ?.]
defp build_prefix(env, overrides) do
case Keyword.fetch(overrides, :prefix) do
{:ok, prefix} ->
[prefix, ?.]

:error ->
env
|> Keyword.get_values(:prefix)
|> Enum.map_join(&(&1 && [&1, ?.]))
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/statix/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Statix.ConfigTest do
use Statix, runtime_config: true

test "connect/1" do
connect(tags: ["tag:test"])
connect(tags: ["tag:test"], prefix: "foo")

increment("sample", 2)
assert_receive {:test_server, _, "sample:2|c|#tag:test"}
assert_receive {:test_server, _, "foo.sample:2|c|#tag:test"}
end

test "global tags when present" do
Expand Down

0 comments on commit 00c9d69

Please sign in to comment.