TelemetryWrappers (telemetry_wrappers v1.1.0)
Documentation for TelemetryWrappers.
Link to this section Summary
Functions
Defines a function that will have its execution time measured and sent as a telemetry event.
Defines a private function that will have its execution time measured and sent as a telemetry event.
Link to this section Functions
deftimed(function_name, metric_name \\ [], metadata \\ quote do %{} end, list)
(macro)Defines a function that will have its execution time measured and sent as a telemetry event.
As an example you can define the following module
defmodule TelemetryWrappers.Support.TestModule do
use TelemetryWrappers
deftimed timed_function(a, b), [:a, :b] do
a + b
end
deftimed timed_function2(a, b) do
a + b
endend
Then both functions will work as expected:
iex> TelemetryWrappers.Support.TestModule.timed_function(1, 2)
3
iex> TelemetryWrappers.Support.TestModule.timed_function2(1, 2)
3but it will also emit a :telemetry event [:a, :b] with the measurement %{call: timing}
where timing is the time the function took to execute in microseconds (measured using :timer.tc/1).
The metric name is optional and will default to [:timing, name] where name is the name of the function (without arity).
Note that type specs can still be defined for function defined with deftimed just as you would normally, e.g.
@spec timed_function(number(), number()) :: number()
deftimed timed_function(a, b), [:a, :b] do
a + b
endYou can also add metadata to the metrics.
defmodule TelemetryWrappers.Support.TestModule do
use TelemetryWrappers
deftimed timed_function_with_meta(a, b), [:a, :b], %{a: a} do
a + b
endend
This will emit a :telemetry event [:a, :b] with the contents %{call: timing} and the metadata '%{a: a}'
deftimedp(function_name, metric_name \\ [], metadata \\ quote do %{} end, list)
(macro)Defines a private function that will have its execution time measured and sent as a telemetry event.
In principle, same as deftimed/3 but defines a private function instead.
As an example you can define the following module
defmodule TelemetryWrappers.Support.TestModule do
use TelemetryWrappers
def invoke_private(a) do
private_fun(a)
end
deftimedp(private_fun(a), [:something], do: a)end
And then invoke the function:
iex> TelemetryWrappers.Support.TestModule.invoke_private(15)
15which will also emit a :telemetry event [:something] with the measurement %{call: timing}
where timing is the time the function took to execute in microseconds (measured using :timer.tc/1).
The metric name is optional and will default to [:timing, name] where name is the name of the function (without arity),
just like in deftimed/3
You can also add metadata to private functions.