Skip to content

Commit

Permalink
Ensure start options take precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
lexmag committed Apr 27, 2020
1 parent 17842ab commit ab4cefe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# Changelog

* Fixed prefix building when start options provided.
* Dropped support for Elixir v1.2.

## v0.8.1

* Fixed port command for OTP versions that support ancillary data sending.

## v0.8.0

* Added support for module, function, arguments tuple in `Fluxter.measure/4`.
* Added support for module, function, arguments tuple in `measure/4`.

## v0.7.1

* Fixed Elixir v1.6 warnings.

## v0.7.0

* Added the `Fluxter.start_link/1` callback to support runtime configuration.
* Added the `c:Fluxter.start_link/1` callback to support runtime configuration.

__Deprecations:__

* Passing child specification options to `Fluxter.child_spec/1` is deprecated.
* Passing child specification options to `child_spec/1` is deprecated.

## v0.6.1

* Fixed a bug in the `Fluxter.child_spec/1` callback.
* Fixed a bug in the `c:Fluxter.child_spec/1` callback.

## v0.6.0

* Added the `Fluxter.child_spec/1` callback.
* Added the `c:Fluxter.child_spec/1` callback.
* Started flushing counters synchronously when calling `Fluxter.flush_counter/1`.
45 changes: 31 additions & 14 deletions lib/fluxter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,15 @@ defmodule Fluxter do
def start_link(options \\ []) do
import Supervisor.Spec

{host, port, prefix} = Fluxter.load_config(__MODULE__, options)
conn = Fluxter.Conn.new(host, port)
conn = %{conn | header: [conn.header | prefix]}
config = Fluxter.get_config(__MODULE__, options)

Enum.map(@worker_names, &worker(Fluxter.Conn, [conn, &1], id: &1))
conn =
config.host
|> Fluxter.Conn.new(config.port)
|> Map.update!(:header, &[&1 | config.prefix])

@worker_names
|> Enum.map(&worker(Fluxter.Conn, [conn, &1], id: &1))
|> Supervisor.start_link(strategy: :one_for_one)
end

Expand All @@ -325,7 +329,8 @@ defmodule Fluxter do
def write(measurement, tags \\ [], fields)

def write(measurement, tags, fields) when is_list(fields) do
System.unique_integer([:positive])
[:positive]
|> System.unique_integer()
|> rem(@pool_size)
|> worker_name()
|> Fluxter.Conn.write(measurement, tags, fields)
Expand Down Expand Up @@ -366,19 +371,31 @@ defmodule Fluxter do
end

@doc false
def load_config(module, options) do
{loc_env, glob_env} =
Application.get_all_env(:fluxter)
def get_config(module, overrides) do
{module_env, global_env} =
:fluxter
|> Application.get_all_env()

This comment has been minimized.

Copy link
@qcam

qcam Apr 28, 2020

Collaborator

Maybe let's deprecate global config altogether?

This comment has been minimized.

Copy link
@lexmag

lexmag Apr 28, 2020

Author Owner

Yeah, I think it is really better to drop global config.
Maybe also start using use Fluxter, otp_app: :foo approach also?

This comment has been minimized.

Copy link
@qcam

qcam Apr 29, 2020

Collaborator

that would be great.

This comment has been minimized.

Copy link
@lexmag

lexmag Apr 30, 2020

Author Owner

#31

This comment has been minimized.

Copy link
@qcam

qcam Apr 30, 2020

Collaborator

cool, I can self-assign on that.

|> Keyword.pop(module, [])

host = options[:host] || loc_env[:host] || glob_env[:host]
port = options[:port] || loc_env[:port] || glob_env[:port]
prefix = build_prefix(glob_env[:prefix], loc_env[:prefix], options[:prefix])
env = module_env ++ global_env
options = overrides ++ env

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

defp build_prefix(part1, part2, part3) do
Enum.map_join([part1, part2, part3], &(&1 && [&1, ?_]))
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
end

0 comments on commit ab4cefe

Please sign in to comment.