Alchemetrics v0.5.2 Alchemetrics.Annotation View Source

Annotations allow you to automatically report the amount of calls and time spent on a particular function.

They work with multiple clauses, guard clauses, recursion, function heads, and so on. However, only public functions can be annotated. The return value of annotated functions does not change.

Example

To annotate a function simply mark it with the tag @alchemetrics instrument_function: true. All functions defined in the module with the same name and arity will also be marked for instrumentation.

defmodule AnnotatedModule do
  use Alchemetrics.Annotation

  @alchemetrics instrument_function: true
  def annotated_function, do: IO.puts "I will be instrumented :)"

  def not_annotated, do: IO.puts "I will not be instrumented :("

  @alchemetrics instrument_function: true
  def multiple_clauses(a) when a > 3, do: a*2
  def multiple_clauses(a), do: a*4

  @alchemetrics instrument_function: true
  def recursive_function([]), do: nil
  def recursive_function([_|t]), do: recursive_function(t)

  @alchemetrics instrument_function: true
  def head(a \ 1)
  def head(a) when a > 2, do: a*2
  def head(a), do: a
end

Report Format

The annotated functions will be reported in the following formats:

  • function_time_spent: "Elixir.Module.function/arity"
  • function_calls: "Elixir.Module.function/arity"
iex(1)> Alchemetrics.ConsoleBackend.enable
iex(2)> AnnotatedModule.annotated_function
I will be instrumented :)
:ok
iex(3)>
%{datapoint: :last_interval, function_calls: "Elixir.AnnotatedModule.annotated_function/0", value: 1}
%{datapoint: :total, function_calls: "Elixir.AnnotatedModule.annotated_function/0", value: 1}
%{datapoint: :avg, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 0}
%{datapoint: :max, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 0}
%{datapoint: :min, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 0}
%{datapoint: :p95, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 0}
%{datapoint: :p99, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 0}
%{datapoint: :last_interval, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 4541}
%{datapoint: :total, function_time_spent: "Elixir.AnnotatedModule.annotated_function/0", value: 4541}