From 546339602cbd77be51088069773e3ae61c6178d2 Mon Sep 17 00:00:00 2001 From: Jason Fertel Date: Fri, 14 Oct 2016 10:31:43 -0700 Subject: [PATCH] Add Datadog tagging support --- lib/statix.ex | 32 ++++++++++++++++---------------- lib/statix/conn.ex | 4 ++-- lib/statix/packet.ex | 13 +++++++++++-- test/statix_test.exs | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/lib/statix.ex b/lib/statix.ex index 7fc9dd9..b774d98 100644 --- a/lib/statix.ex +++ b/lib/statix.ex @@ -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 """ @@ -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 diff --git a/lib/statix/conn.ex b/lib/statix/conn.ex index f88f228..9484567 100644 --- a/lib/statix/conn.ex +++ b/lib/statix/conn.ex @@ -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 diff --git a/lib/statix/packet.ex b/lib/statix/packet.ex index e8db9bf..57233d4 100644 --- a/lib/statix/packet.ex +++ b/lib/statix/packet.ex @@ -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 = %{ @@ -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 diff --git a/test/statix_test.exs b/test/statix_test.exs index b2d4d96..d52e599 100644 --- a/test/statix_test.exs +++ b/test/statix_test.exs @@ -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"} @@ -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"} @@ -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