chronex v1.0.3 Chronex
Chronex
A small library to seamlessly add code instrumentation to your Elixir projects.
The documentation for this project is available at HexDocs.
Quick Start
Chronex
implements a dead simple API consisting of only three functions,
bind/3
, unbind/3
and bound?/3
. All three functions take 3 arguments
as input. A module name, a function name and an arity. These three arguments
are used to identify the target function.
bind/3
is used to attach a stopwatch to the given function. unbind/3
is
used to detach a stopwatch from the given function. Last but not least,
bound?/3
is used to check if the given function has a stopwatch attach to
it.
Chronex needs write access to the beam files where the target functions are implemented. Thus, you will need to run as root should you want to experiment adding code instrumentation to core functions.
str = "Hello, world!"
String2.length(str)
# => 13
# Attach a stopwatch to String2.length/1
:ok = Chronex.bind(String2, :length, 1)
true = Chronex.bound?(String2, :length, 1)
String2.length(str)
# STDOUT: 15:53:09.917 [debug] chronex | hook=:before mfa="Elixir.String2.length/1" args=["Hello, world!"] uuid="39c120fa-3264-11e8-afec-600308a32e10"
# STDOUT: chronex | hook=:after_return mfa="Elixir.String2.length/1" return=13 duration=0.003 uuid="39c120fa-3264-11e8-afec-600308a32e10"
# => 13
# Detach the stopwatch from String2.length/1
:ok = Chronex.unbind(String2, :length, 1)
false = Chronex.bound?(String2, :length, 1)
String2.length(str)
# => 13
Backends
Chronex
implements four hooks:
before
- Executed before the target function is called.after_return
- Executed right after the target function returns.after_throw
- Executed right after the target function throws a value (i.e.catch
clause in atry
block).after_raise
- Executed right after the target function raises an error (i.e.rescue
clause in atry
block).
Chronex
ships with a Logger
backend that can be used to log interesting
information surrounding the invocation of the instrumented functions. New
backends can easily be implemented by simply writting a module that implements
the four hooks listed above.
To configure the list of backends, just do as follows:
config :chronex, backends: [
Chronex.Backends.Logger
]
Note that you can have more than one backend enabled at the same time.
To configure the log level of the Logger backend, just add the following line to your config file:
config :chronex, Chronex.Backends.Logger,
log_level: :debug
Link to this section Summary
Functions
Attach a stopwatch to the given function. The stopwatch is started on every function call targetting the given function. It measures how long it takes for the given function to run. All measurements are handled by the Logger application
Check if the given function has a stopwatch attached to it
Detach a stopwatch from the given function
Link to this section Functions
bind(atom(), atom(), non_neg_integer()) :: :ok | {:error, :bound}
Attach a stopwatch to the given function. The stopwatch is started on every function call targetting the given function. It measures how long it takes for the given function to run. All measurements are handled by the Logger application.
Check if the given function has a stopwatch attached to it.
unbind(atom(), atom(), non_neg_integer()) :: :ok | {:error, :unbound}
Detach a stopwatch from the given function.