Skip to content

Commit

Permalink
Add Datadog tagging support
Browse files Browse the repository at this point in the history
  • Loading branch information
fertel authored and lexmag committed Oct 31, 2016
1 parent 5bd3955 commit 5463396
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
32 changes: 16 additions & 16 deletions lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ defmodule Statix do
:ok
end

def increment(key, val \\ "1") do
def increment(key, val \\ "1", options \\ []) do
@statix_conn
|> Statix.transmit(:counter, key, val)
|> Statix.transmit(:counter, key, val, options)
end

def decrement(key, val \\ "1") do
def decrement(key, val \\ "1", options \\ []) do
@statix_conn
|> Statix.transmit(:counter, key, [?-, to_string(val)])
|> Statix.transmit(:counter, key, [?-, to_string(val)], options)
end

def gauge(key, val) do
def gauge(key, val, options \\ [] ) do
@statix_conn
|> Statix.transmit(:gauge, key, val)
|> Statix.transmit(:gauge, key, val, options)
end

def histogram(key, val) do
def histogram(key, val, options \\ []) do
@statix_conn
|> Statix.transmit(:histogram, key, val)
|> Statix.transmit(:histogram, key, val, options)
end

def timing(key, val) do
def timing(key, val, options \\ []) do
@statix_conn
|> Statix.transmit(:timing, key, val)
|> Statix.transmit(:timing, key, val, options)
end

@doc """
Expand All @@ -43,23 +43,23 @@ defmodule Statix do
It returns the result of the function call, making it suitable
for pipelining and easily wrapping existing code.
"""
def measure(key, fun) when is_function(fun, 0) do
def measure(key, options \\ [], fun) when is_function(fun, 0) do
{elapsed, result} = :timer.tc(fun)

timing(key, div(elapsed, 1000))
timing(key, div(elapsed, 1000), options)

result
end

def set(key, val) do
def set(key, val, options \\ []) do
@statix_conn
|> Statix.transmit(:set, key, val)
|> Statix.transmit(:set, key, val, options)
end
end
end

def transmit(conn, type, key, val) when is_binary(key) or is_list(key) do
Statix.Conn.transmit(conn, type, key, to_string(val))
def transmit(conn, type, key, val, options \\ []) when is_binary(key) or is_list(key) do
Statix.Conn.transmit(conn, type, key, to_string(val), options)
end

def config(module) do
Expand Down
4 changes: 2 additions & 2 deletions lib/statix/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ defmodule Statix.Conn do
%__MODULE__{conn | sock: sock}
end

def transmit(%__MODULE__{} = conn, type, key, val) when is_binary(val) do
Packet.build(conn.header, type, key, val)
def transmit(%__MODULE__{} = conn, type, key, val, options) when is_binary(val) do
Packet.build(conn.header, type, key, val, options)
|> transmit(conn.sock)
end

Expand Down
13 changes: 11 additions & 2 deletions lib/statix/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ defmodule Statix.Packet do
]
end

def build(header, name, key, val) do
[header, key, ?:, val, ?| | metric_type(name)]
def build(header, name, key, val, options) do
[header, key, ?:, val, ?| , metric_type(name)]
|> set_tags_option(options[:tags])
end

metrics = %{
Expand All @@ -29,4 +30,12 @@ defmodule Statix.Packet do
for {name, type} <- metrics do
defp metric_type(unquote(name)), do: unquote(type)
end

defp set_tags_option(packet, nil) do
packet
end

defp set_tags_option(packet, tags) do
[packet | ["|#", Enum.join(tags, ",")]]
end
end
37 changes: 29 additions & 8 deletions test/statix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule StatixTest do
Sample.connect
end

test "increment/1,2" do
test "increment/1,2,3" do
Sample.increment("sample")
assert_receive {:server, "sample:1|c"}

Expand All @@ -43,10 +43,13 @@ defmodule StatixTest do
Sample.increment("sample", 2.1)
assert_receive {:server, "sample:2.1|c"}

Sample.increment("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|c|#foo:bar,baz"}

refute_received _any
end

test "decrement/1,2" do
test "decrement/1,2,3" do
Sample.decrement("sample")
assert_receive {:server, "sample:-1|c"}

Expand All @@ -56,58 +59,76 @@ defmodule StatixTest do
Sample.decrement("sample", 2.1)
assert_receive {:server, "sample:-2.1|c"}

Sample.decrement("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:-3|c|#foo:bar,baz"}

refute_received _any
end

test "gauge/2" do
test "gauge/2,3" do
Sample.gauge(["sample"], 2)
assert_receive {:server, "sample:2|g"}

Sample.gauge("sample", 2.1)
assert_receive {:server, "sample:2.1|g"}

Sample.gauge("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|g|#foo:bar,baz"}

refute_received _any
end

test "histogram/2" do
test "histogram/2,3" do
Sample.histogram("sample", 2)
assert_receive {:server, "sample:2|h"}

Sample.histogram("sample", 2.1)
assert_receive {:server, "sample:2.1|h"}

Sample.histogram("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|h|#foo:bar,baz"}

refute_received _any
end

test "timing/2" do
test "timing/2,3" do
Sample.timing(["sample"], 2)
assert_receive {:server, "sample:2|ms"}

Sample.timing("sample", 2.1)
assert_receive {:server, "sample:2.1|ms"}

Sample.timing("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|ms|#foo:bar,baz"}

refute_received _any
end

test "measure/2" do
expected_result = "the stuff."

fun_result = Sample.measure(["sample"], fn ->
:timer.sleep(100)
expected_result
end)

assert_receive {:server, <<"sample:10", _, "|ms">>}
assert fun_result == expected_result

Sample.measure("sample", [tags: ["foo:bar", "baz"]], fn ->
:timer.sleep(100)
end)
assert_receive {:server, <<"sample:10", _, "|ms|#foo:bar,baz">>}
end

test "set/2" do
test "set/2,3" do
Sample.set(["sample"], 2)
assert_receive {:server, "sample:2|s"}

Sample.set("sample", 2.1)
assert_receive {:server, "sample:2.1|s"}

Sample.set("sample", 3, tags: ["foo:bar", "baz"])
assert_receive {:server, "sample:3|s|#foo:bar,baz"}

refute_received _any
end
end

0 comments on commit 5463396

Please sign in to comment.