Skip to content

Commit 00c9d69

Browse files
committed
Fix prefix building when using connect options
1 parent b4a8b4e commit 00c9d69

3 files changed

Lines changed: 34 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
* Fixed prefix building when connect options provided.
4+
35
## v1.4.0
46

57
* Added support for connection pooling that is configurable via the `:pool_size` option.

lib/statix.ex

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ defmodule Statix do
117117
@doc """
118118
Opens the connection to the StatsD-compatible server.
119119
120-
The configuration is read from the configuration for the `:statix` application
120+
The configuration is read from the environment for the `:statix` application
121121
(both globally and per connection).
122122
123-
The given `config` overrides the configuration read from the application environment.
123+
The given `options` override the configuration read from the application environment.
124124
"""
125-
@callback connect(config :: keyword) :: :ok
125+
@callback connect(options :: keyword) :: :ok
126126

127127
@doc """
128128
Increments the StatsD counter identified by `key` by the given `value`.
@@ -267,8 +267,8 @@ defmodule Statix do
267267
quote do
268268
@statix_key Module.concat(__MODULE__, :__statix__)
269269

270-
def connect(config \\ []) do
271-
statix = Statix.new(__MODULE__, config)
270+
def connect(options \\ []) do
271+
statix = Statix.new(__MODULE__, options)
272272
Application.put_env(:statix, @statix_key, statix)
273273

274274
Statix.open(statix)
@@ -285,8 +285,8 @@ defmodule Statix do
285285
quote do
286286
@statix Statix.new(__MODULE__, [])
287287

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

359359
@doc false
360-
def new(module, config) do
361-
config =
362-
module
363-
|> get_config()
364-
|> Map.merge(Map.new(config))
365-
360+
def new(module, options) do
361+
config = get_config(module, options)
366362
conn = Conn.new(config.host, config.port)
367363
header = IO.iodata_to_binary([conn.header | config.prefix])
368364

@@ -411,40 +407,38 @@ defmodule Statix do
411407
defp pick_name([name]), do: name
412408
defp pick_name(pool), do: Enum.random(pool)
413409

414-
defp get_config(module) do
415-
{conn_env, global_env} =
410+
defp get_config(module, overrides) do
411+
{module_env, global_env} =
416412
:statix
417413
|> Application.get_all_env()
418414
|> Keyword.pop(module, [])
419415

420-
{global_prefix, global_env} = Keyword.pop_first(global_env, :prefix)
421-
{conn_prefix, conn_env} = Keyword.pop_first(conn_env, :prefix)
422-
prefix = build_prefix(global_prefix, conn_prefix)
423-
424-
{global_tags, global_env} = Keyword.pop_first(global_env, :tags, [])
425-
{conn_tags, conn_env} = Keyword.pop_first(conn_env, :tags, [])
426-
tags = global_tags ++ conn_tags
416+
env = module_env ++ global_env
417+
options = overrides ++ env
427418

428-
env = Keyword.merge(global_env, conn_env)
429-
host = Keyword.get(env, :host, "127.0.0.1")
430-
port = Keyword.get(env, :port, 8125)
431-
pool_size = Keyword.get(env, :pool_size, 1)
419+
tags =
420+
Keyword.get_lazy(overrides, :tags, fn ->
421+
env |> Keyword.get_values(:tags) |> Enum.concat()
422+
end)
432423

433424
%{
434-
prefix: prefix,
435-
host: host,
436-
port: port,
437-
pool_size: pool_size,
425+
prefix: build_prefix(env, overrides),
426+
host: Keyword.get(options, :host, "127.0.0.1"),
427+
port: Keyword.get(options, :port, 8125),
428+
pool_size: Keyword.get(options, :pool_size, 1),
438429
tags: tags
439430
}
440431
end
441432

442-
defp build_prefix(global_part, conn_part) do
443-
case {global_part, conn_part} do
444-
{nil, nil} -> ""
445-
{_, nil} -> [global_part, ?.]
446-
{nil, _} -> [conn_part, ?.]
447-
{_, _} -> [global_part, ?., conn_part, ?.]
433+
defp build_prefix(env, overrides) do
434+
case Keyword.fetch(overrides, :prefix) do
435+
{:ok, prefix} ->
436+
[prefix, ?.]
437+
438+
:error ->
439+
env
440+
|> Keyword.get_values(:prefix)
441+
|> Enum.map_join(&(&1 && [&1, ?.]))
448442
end
449443
end
450444

test/statix/config_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ defmodule Statix.ConfigTest do
44
use Statix, runtime_config: true
55

66
test "connect/1" do
7-
connect(tags: ["tag:test"])
7+
connect(tags: ["tag:test"], prefix: "foo")
88

99
increment("sample", 2)
10-
assert_receive {:test_server, _, "sample:2|c|#tag:test"}
10+
assert_receive {:test_server, _, "foo.sample:2|c|#tag:test"}
1111
end
1212

1313
test "global tags when present" do

0 commit comments

Comments
 (0)