Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 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
3 changes: 2 additions & 1 deletion lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,9 @@ 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, Application.get_env(:statix, :sample_rate))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of fetching the value from env each call we should read it once and store in the connection struct.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Let me update PR and see if I'm on right track.

if is_nil(sample_rate) or sample_rate >= :rand.uniform() do
options = Keyword.put_new(options, :sample_rate, sample_rate)
Conn.transmit(conn, type, key, to_string(val), options)
else
:ok
Expand Down