ex_data_dog
Installation
If available in Hex, the package can be installed
by adding ex_data_dog to your list of dependencies in mix.exs:
def deps do
[{:ex_data_dog, "~> 0.1.0"}]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ex_data_dog.
Configuration
Configure ex_data_dog using Mix.Config as usual (probably in your config/):
use Mix.Config
config :ex_data_dog,
host: "your.datadog.host.com",
port: 1234,
namespace: "your-app"The defaults are:
-
host:
127.0.0.1 -
port:
8125 -
namespace:
nil
API
Counters
Counters can be manipulated with ExDataDogTest.increment/1 and
ExDataDogTest.decrement/1 for simple counts:
if passed? do
ExDataDogTest.increment("cases.passed")
endif cancelled_account? do
ExDataDogTest.decrement("users")
end
You can also provide a sample_rate with ExDataDogTest.increment/2 and
ExDataDogTest.decrement/2. For example, in this case an increment for
cart.added will only be sent 50% of the time:
ExDataDogTest.increment("cart.added", sample_rate: 0.5)
To set a counter explicitly, use ExDataDogTest.counter/2:
ExDataDogTest.counter(3, "cart.removed")
You can also send a sample_rate:
ExDataDogTest.counter(3, "cart.removed", sample_rate: 0.25)
Note that the function returns the value (eg, 3 here), making it
suitable for pipelining.
Timers
Manually timed values can be recorded with ExDataDogTest.timer/2:
elapsed_ms = # something manually timed in
ExDataDogTest.timer(elapsed_ms, "foobar")
The value passed to ExDataDogTest.timer/2 (eg, elapsed_ms here) is
returned from it, making this suitable for pipelining.
For convenience, you can also time a function call with
ExDataDogTest.timing/2:
ExDataDogTest.timing("foo.bar", fn ->
# Time something
end)
To sample (ie, 50% of the time), pass a sample_rate:
ExDataDogTest.timing("foo.bar", fn ->
# Time something, some of the time
end, sample_rate: 0.5)Note that, regardless of the sample rate, the function is always called -- it's just not always measured. Also note that the return value of the measured function is returned, making this suitable for pipelining.
Sets
A value can be recorded in a set with ExDataDogTest.set/2:
ExDataDogTest.set(user_id, "users")Note that the function returns the value, making it suitable for pipelining.
####Tags
All metrics support the
Datadog-specific tags extension
so you may provide a tags option, eg:
ExDataDogTest.increment("cart.added", tags: ~w(foo bar))Histograms
The histogram extension is supported as well:
ExDataDogTest.histogram(42, "database.query.time", tags: ["db", "perf"])Note that the function returns the value, making it suitable for pipelining.
Histogram Timing
A histogram version of the ExDataDogTest.timing function is also supported:
ExDataDogTest.histogram_timing("foo.bar", fn ->
# Time something
end)Decorators
The decorators allow you to quickly and easily time function calls in
your code. Simply replace def with deftimed for those functions
you wish to time.
defmodule MyModule.Data do
use ExDataDog.Decorator
deftimed slow_function do
# This is a suspect function we wish to time.
end
end
Now all calls to MyModule.Data.slow_function/0 will be timed and
reported to your statsd server. By default the metric key used for
each call will be PREFIX.function_call.MODULE.FUNCTION_ARITY. So in
this example it would have been function_call.mymodule.data.slow_function_0.
You can change the metric name by setting the @metric attribute
ahead of the function. The metric will apply to other function
definitions of the same arity unless specifically changed again. Other
following functions of different name or arity will use the default.
deftimed init, do: nil # PREFIX.function_call.mymodule.data.init_0
@metric "trace.some_function"
deftimed some_function(1), do: nil # PREFIX.trace.some_function
deftimed some_function(2), do: nil # PREFIX.trace.some_function
@metric "trace.some_function_catchall"
deftimed some_function(x) when is_list(x), do: nil # PREFIX.trace.some_function_catchall
deftimed some_function(x), do: nil # PREFIX.trace.some_function_catchall
deftimed some_function(x,y), do: nil # PREFIX.function_call.mymodule.data.some_function_2
You can set options using the @metric_options attribute. This follows the same rules as with the @metric example abobe.
Here we use Datadog's "tag" extension to StatD:
@metric_options [tags: ["basic"]]
deftimed some_function(), do: nilThere are 2 global options available. Both will apply to all functions that follow it unless locally overridden.
@default_metric_options: Metric options to use unless overridden with@metric_options. Defaults to [].@use_histogram: Send results using histograms instead of gauges. Defaults to false.