Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
188 lines (141 sloc) 5.76 KB
Integrating AppSignal into Phoenix

The AppSignal for Elixir package integrates with Phoenix. To set up the integration, please follow our installation guide.

This page will describe further ways of configuring AppSignal for the Phoenix framework. To add more custom instrumentation to your Phoenix application, read the Elixir instrumentation documentation.

More information can be found in the AppSignal Hex package documentation.

Table of Contents

Incoming HTTP requests

To start logging HTTP requests in your Phoenix app, make sure you use the Appsignal.Phoenix module in your endpoint.ex file.

defmodule AppsignalPhoenixExampleWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :appsignal_phoenix_example
  use Appsignal.Phoenix

  # ...

This will create a transaction for every HTTP request which is performed on the endpoint.

Phoenix instrumentation hooks

Phoenix comes with instrumentation hooks built-in. To send Phoenix' default instrumentation events to AppSignal, add the following to your config.exs (adjusting for your app's name!).

# config/config.exs
config :my_app, MyApp.Endpoint,
  instrumenters: [Appsignal.Phoenix.Instrumenter]

Using the Appsignal.Phoenix.Instrumenter it's possible to add custom instrumentation to your Phoenix applications.

This module can be used as a Phoenix instrumentation module. Adding this module to the list of Phoenix instrumenters will result in the phoenix_controller_call and phoenix_controller_render events to become part of your request timeline.

For more information on instrumentation please visit the AppSignal Hex package documentation.

Template rendering

It's possible to instrument how much time it takes each template render, including subtemplates (partials), in your Phoenix application.

To enable this for AppSignal you need to register the AppSignal template renderer, which augment the compiled templates with instrumentation hooks.

Put the following in your config.exs.

# config/config.exs
config :phoenix, :template_engines,
  eex: Appsignal.Phoenix.Template.EExEngine,
  exs: Appsignal.Phoenix.Template.ExsEngine


To enable query logging, add the Appsignal.Ecto module to your Repo's logger configuration. The Ecto.LogEntry logger is the default logger for Ecto and needs to be set as well to keep the original Ecto logger behavior intact.

# config/config.exs
config :my_app, MyApp.Repo,
  loggers: [Appsignal.Ecto, Ecto.LogEntry]

If you're using Ecto 3, attach Appsignal.Ecto to Telemetry query events in your application's start/2 function:

  [:my_app, :repo, :query],

For versions of Telemetry < 0.3.0, you'll need to call it slightly differently:

  [:my_app, :repo, :query],

-> Note: Telemetry support was added in version 1.8.2 of the AppSignal for Elixir integration.

Note that this is not Phoenix-specific but works for all Ecto queries. The process that performs the query however, must be associated with an Appsignal.Transaction, otherwise no event will be logged.


Channel instrumentation with a channel's handle

Incoming channel requests can be instrumented by adding code to the handle_in/3 function of your application. Function decorators are used to minimize the amount of code you have to add to your application's channels.

defmodule SomeApp.MyChannel do
  use Appsignal.Instrumentation.Decorators

  @decorate channel_action()
  def handle_in("ping", _payload, socket) do
    # your code here..

Channel events will be displayed under the "Background jobs" tab, showing the channel module and the action argument that you entered.

Channel instrumentation without decorators

You can also decide not to use function decorators. In that case, use the channel_action/3 function directly, passing in a name for the channel action, the socket, and the actual code that you are executing in the channel handler.

defmodule SomeApp.MyChannel do
  import Appsignal.Phoenix.Channel, only: [channel_action: 4]

  def handle_in("ping" = action, _payload, socket) do
    channel_action(__MODULE__, action, socket, fn ->
      # do some heave processing here...
      reply = perform_work()
      {:reply, {:ok, reply}, socket}

Instrumentation for included Plugs

Exceptions in included Plugs are automatically caught by AppSignal, but performance samples need to be set up manually using the custom instrumentation helpers or decorators. See the Plug documentation for more information.

Custom instrumentation

Add custom instrumentation to keep track of more complex code and get more complete breakdowns of slow requests.