Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Sampling is supported via the `:sample_rate` option:
MyApp.Statix.increment("page_view", 1, sample_rate: 0.5)
```

Or by setting global `:sample_rate` in config:
```elixir
config :statix,
sample_rate: 0.1
```

The UDP packet will only be sent to the server about half of the time,
but the resulting value will be adjusted on the server according to the given sample rate.

Expand Down Expand Up @@ -113,6 +119,7 @@ The defaults are:
* prefix: `nil`
* host: `"127.0.0.1"`
* port: `8125`
* sample_rate: `1.0`

__Note:__ by default, configuration is evaluated once, at compile time.
If you plan using other configuration at runtime, you must specify the `:runtime_config` option:
Expand Down
16 changes: 10 additions & 6 deletions lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ defmodule Statix do
if Keyword.get(opts, :runtime_config, false) do
quote do
@statix_header_key Module.concat(__MODULE__, :__statix_header__)
@statix_sample_rate_key Module.concat(__MODULE__, :__statix_sample_rate__)

def connect() do
conn = Statix.new_conn(__MODULE__)
Application.put_env(:statix, @statix_header_key, conn.header)
Application.put_env(:statix, @statix_sample_rate_key, conn.sample_rate)

Statix.open_conn(conn)
:ok
Expand All @@ -248,7 +250,8 @@ defmodule Statix do
@compile {:inline, [current_conn: 0]}
defp current_conn() do
header = Application.fetch_env!(:statix, @statix_header_key)
%Statix.Conn{header: header, sock: __MODULE__}
sample_rate = Application.fetch_env!(:statix, @statix_sample_rate_key)
%Statix.Conn{header: header, sock: __MODULE__, sample_rate: sample_rate}
end
end
else
Expand Down Expand Up @@ -320,10 +323,10 @@ defmodule Statix do

@doc false
def new_conn(module) do
{host, port, prefix} = load_config(module)
conn = Conn.new(host, port)
{host, port, prefix, sample_rate} = load_config(module)
conn = Conn.new(host, port, sample_rate)
header = IO.iodata_to_binary([conn.header | prefix])
%{conn | header: header, sock: module}
%{conn | header: header, sock: module, sample_rate: sample_rate}
end

@doc false
Expand All @@ -335,7 +338,7 @@ defmodule Statix do
@doc false
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)
sample_rate = Keyword.get(options, :sample_rate, conn.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 @@ -353,8 +356,9 @@ defmodule Statix do

host = Keyword.get(env, :host, "127.0.0.1")
port = Keyword.get(env, :port, 8125)
sample_rate = Keyword.get(env, :sample_rate, 1.0)
prefix = build_prefix(prefix1, prefix2)
{host, port, prefix}
{host, port, prefix, sample_rate}
end

defp build_prefix(part1, part2) do
Expand Down
12 changes: 6 additions & 6 deletions lib/statix/conn.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
defmodule Statix.Conn do
@moduledoc false

defstruct [:sock, :header]
defstruct [:sock, :header, :sample_rate]

alias Statix.Packet

def new(host, port) when is_binary(host) do
new(string_to_charlist(host), port)
def new(host, port, sample_rate) when is_binary(host) do
new(string_to_charlist(host), port, sample_rate)
end

def new(host, port) when is_list(host) or is_tuple(host) do
def new(host, port, sample_rate) when is_list(host) or is_tuple(host) do
{:ok, addr} = :inet.getaddr(host, :inet)
header = Packet.header(addr, port)
%__MODULE__{header: header}
%__MODULE__{header: header, sample_rate: sample_rate}
end

def open(%__MODULE__{} = conn) do
Expand All @@ -32,7 +32,7 @@ defmodule Statix.Conn 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