Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom DISTRIBUTION metric #3

Merged
merged 2 commits into from
Apr 25, 2024
Merged
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
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ jobs:
name: Code linting
uses: lexmag/elixir-actions/.github/workflows/lint.yml@v2
with:
otp-version: "24"
elixir-version: "1.14"
otp-version: "26"
elixir-version: "1.16"

test:
name: Test suite
runs-on: ubuntu-20.04

strategy:
matrix:
otp: ["24"]
elixir: ["1.14"]
otp: ["26"]
elixir: ["1.16"]
runtime_config: [true, false]

include:
- otp: "20"
elixir: "1.6"
- otp: "26"
elixir: "1.16"
runtime_config: false

env:
Expand Down
25 changes: 24 additions & 1 deletion lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ defmodule Statix do
"""
@callback timing(key, value :: String.Chars.t(), options) :: on_send

@doc """
Same as `distribution(key, value, [])`.
"""
@callback distribution(key, value :: String.Chars.t()) :: on_send

@doc """
Writes the given `value` to the StatsD distribution identified by `key`.

`value` is expected in milliseconds.

## Examples

iex> MyApp.Statix.distribution("rendering", 12, [])
:ok

"""
@callback distribution(key, value :: String.Chars.t(), options) :: on_send

@doc """
Writes the given `value` to the StatsD set identified by `key`.

Expand Down Expand Up @@ -330,6 +348,10 @@ defmodule Statix do
Statix.transmit(current_statix(), :timing, key, val, options)
end

def distribution(key, val, options \\ []) do
Statix.transmit(current_statix(), :distribution, key, val, options)
end

def measure(key, options \\ [], fun) when is_function(fun, 0) do
{elapsed, result} = :timer.tc(fun)

Expand All @@ -349,7 +371,8 @@ defmodule Statix do
histogram: 3,
timing: 3,
measure: 3,
set: 3
set: 3,
distribution: 3
)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ defmodule Statix.Packet do
gauge: "g",
histogram: "h",
timing: "ms",
set: "s"
set: "s",
distribution: "d"
}

for {name, type} <- metrics do
Expand Down
9 changes: 9 additions & 0 deletions test/statix/overriding_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ defmodule Statix.OverridingTest do
super([key, "-overridden"], value, options)
end

def distribution(key, value, options) do
super([key, "-overridden"], value, options)
end

setup do
connect()
end
Expand Down Expand Up @@ -64,6 +68,11 @@ defmodule Statix.OverridingTest do
assert_receive {:test_server, _, "sample-overridden:3|ms|#foo"}
end

test "distribution/3" do
distribution("sample", 3, tags: ["foo"])
assert_receive {:test_server, _, "sample-overridden:3|d|#foo"}
end

test "measure/3" do
measure("sample", [tags: ["foo"]], fn ->
:timer.sleep(100)
Expand Down
3 changes: 2 additions & 1 deletion test/statix/pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule Statix.PoolingTest do
{:histogram, [3]},
{:timing, [3]},
{:measure, [fn -> nil end]},
{:set, [3]}
{:set, [3]},
{:distribution, [3]}
]
|> Enum.map(fn {function, arguments} ->
apply(__MODULE__, function, ["sample" | arguments])
Expand Down
32 changes: 27 additions & 5 deletions test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,49 @@ defmodule StatixTest do
refute_received _any
end

test "distribution/2,3" do
__MODULE__.distribution(["sample"], 2)
assert_receive {:test_server, _, "sample:2|d"}

distribution("sample", 2.1)
assert_receive {:test_server, _, "sample:2.1|d"}

distribution("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:test_server, _, "sample:3|d|#foo:bar,baz"}

distribution("sample", 3, sample_rate: 1.0, tags: ["foo", "bar"])
assert_receive {:test_server, _, "sample:3|d|@1.0|#foo,bar"}

distribution("sample", 3, sample_rate: 0.0)

refute_received _any
end

test "port closed" do
close_port()

assert capture_log(fn ->
assert {:error, :port_closed} == increment("sample")
end) =~ "counter metric \"sample\" lost value 1 error=:port_closed\n\e[0m"
end) =~ "counter metric \"sample\" lost value 1 error=:port_closed"

assert capture_log(fn ->
assert {:error, :port_closed} == decrement("sample")
end) =~ "counter metric \"sample\" lost value -1 error=:port_closed\n\e[0m"
end) =~ "counter metric \"sample\" lost value -1 error=:port_closed"

assert capture_log(fn ->
assert {:error, :port_closed} == gauge("sample", 2)
end) =~ "gauge metric \"sample\" lost value 2 error=:port_closed\n\e[0m"
end) =~ "gauge metric \"sample\" lost value 2 error=:port_closed"

assert capture_log(fn ->
assert {:error, :port_closed} == histogram("sample", 3)
end) =~ "histogram metric \"sample\" lost value 3 error=:port_closed\n\e[0m"
end) =~ "histogram metric \"sample\" lost value 3 error=:port_closed"

assert capture_log(fn ->
assert {:error, :port_closed} == timing("sample", 2.5)
end) =~ "timing metric \"sample\" lost value 2.5 error=:port_closed\n\e[0m"
end) =~ "timing metric \"sample\" lost value 2.5 error=:port_closed"

assert capture_log(fn ->
assert {:error, :port_closed} == distribution("sample", 2.5)
end) =~ "distribution metric \"sample\" lost value 2.5 error=:port_closed"
end
end
Loading