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 anindex/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.
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 theAppsignal.instrument/2 function:
instrument/3 instead:
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 theAppsignal.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 anindex/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.
phoenix_controller, phoenix_render, ecto, etc.) you can also
supply a group name to the transaction_event decorator.
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 tracktransaction_event decorators we will need to start an
AppSignal transaction beforehand. We can start a transaction with the
transaction function decorator.
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
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 thehandle_in/3 function of a Phoenix.Channel module.