Instrumenting Ecto queries
AppSignal uses Ecto’s Telemetry instrumentation to gain information about queries ran in your app by attaching a handler that gets called whenever a query is executed.
Automatic instrumentation
The Ecto instrumentation automatically hooks into your Ecto repos if the :otp_app
configuration is configured to your app's OTP app name. The installer automatically configures this for you.
The integration then uses your app's configuration to find out which repos are configured, as your app will have a configuration line like this in config/config.exs
:
Disable automatic instrumentation
To disable automatic Ecto instrumentation, set the instrument_ecto
config option to false
.
Instrumenting Ecto preloads
Since Ecto preload queries internally use several Elixir processes to carry out the different database queries involved, some manual changes to your codebase are necessary for AppSignal to properly instrument them.
At the top of your Ecto repo module, replace use Ecto.Repo
with use Appsignal.Ecto.Repo
, as in the following example:
If your Ecto repo module has its own implementation of the default_options/1
function, make sure to call super
within it to merge its default options with those of AppSignal's Ecto repo instrumentation:
Manual handler attachment
For repos that aren't listed in the :ecto_repos
configuration, you can attach a handler manually when starting your app’s supervisor. In most applications, this is done in your application’s start/2
function.
In this example, we’ve attached the Telemetry handler to our Phoenix application by calling Appsignal.Ecto.attach/2
. The first argument is the name of the OTP app that the repo belongs to, and the second argument is the repo's module.