Custom instrumentation for Elixir
In order to find out what specific pieces of code are causing performance problems it's useful to add custom instrumentation to your application. This allows us to create better breakdowns of which code runs slowest and what type of action the most amount of time was spent on.
Custom instrumentation is possible in two ways: using function decorators and instrumentation helper functions. The function decorators are easiest to use, but are less flexible than the instrumentation helper functions.
This short guide will help you set up custom instrumentation. More details on the usage of certain helpers can be found in the Hex docs for the AppSignal package.
Appsignal.instrument/2-3
The instrument/2
function is used to add instrumentation by wrapping a piece
of code in a span. A span eventually becomes a sample, or an event in another
span's sample in AppSignal.
Add spans to traces
In the following example we have a Phoenix controller with an index/2
function which calls a slow function. The slow
function is instrumented using
the Appsignal.instrument/2
function which records it as a separate event in
this Phoenix request. It will show up on AppSignal.com in the event timeline of
this sample to provide more insight in where the most time was spent during the
request.
Here, we wrap our function's contents with a call to Appsignal.instrument3
,
and we pass "slow"
as the name for the event.
Starting new traces
In the Phoenix example an AppSignal trace has already been started, thanks to the first-party support for Phoenix in the AppSignal package. Not all frameworks and packages are currently supported directly and automatically start traces. The same is true for your own pure Elixir applications.
Like when adding spans to already-instrumented traces, a new root span is
created using the Appsignal.instrument/2
function:
This example creates a sample named "instrument" in the "background" namespace
in AppSignal. The name will also be used as the category name for the main
span. To use a different category name, use instrument/3
instead:
When passing a function that takes an argument, the instrument/2-3
function calls it with the opened span to allow further customization. See the docs on hex.pm for all available Span functions.
Exception handling
To report errors using custom instrumentation please read more in our exception handling guide.
Function decorators
Using the Appsignal.Instrumentation.Decorators
decorator module, it's
possible add custom instrumentation to your Elixir applications without
changing the functions' contents.
Transaction events
In the following example we have a Phoenix controller with an index/2
function which calls a slow function. The slow
function is instrumented using
the AppSignal transaction_event
decorator which records it as a separate
event in this Phoenix request. It will show up on AppSignal.com in the event
timeline of this transaction sample to provide more insight in where the most
time was spent during the request.
If you want to group certain events together under the same event group (other
group are phoenix_controller
, phoenix_render
, ecto
, etc.) you can also
supply a group name to the transaction_event
decorator.
This will create an event get_data_from_github.github_api
in the event
timeline. For more information on how event names are used, please read
our event naming guidelines.
Transactions
In the Phoenix example an AppSignal transaction has already been started, thanks to the first-party support for Phoenix in the AppSignal package. Not all frameworks and packages are currently supported directly and automatically start transactions. The same is true for your own pure Elixir applications.
In order to track transaction_event
decorators we will need to start an
AppSignal transaction beforehand. We can start a transaction with the
transaction
function decorator.
Note: When using pure Elixir applications, make sure that the AppSignal application is started before you start a transaction. For more information, see how to integrate AppSignal.
Namespaces
In order to differentiate between HTTP requests and background jobs we can pass a namespace to the transaction once we start it.
The following two namespaces are official namespaces supported by AppSignal.
http_request
- the default - is called the "web" namespacebackground_job
- creates the "background" namespace
For more information about what namespaces are, please see our namespaces documentation.
Custom namespaces
You can also create your own namespaces to track transactions in a separate part of your application such as an administration panel. This will group all the transactions with this namespace in a separate section on AppSignal.com so that slow admin controllers don't interfere with the averages of your application's speed.
Phoenix channels
There is a custom function decorator for Phoenix channels. This decorator is
meant to be put before the handle_in/3
function of a Phoenix.Channel
module.
Channel events will be displayed under the "background" namespace, showing the channel module and the action argument that it's used on.
Manually creating and closing spans
In some cases it can be useful to manually handle spans. You can use the span API to instrument tasks for example: