AppSignal  Docs

Visit AppSignal.com
Jump to navigation

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 Phoenix 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, just before the line where your router module gets called (which should read something like plug MyApp.Router).

1
2
3
4
# lib/my_app/endpoint.ex
# ...
use Appsignal.Phoenix
plug MyApp.Router

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!).

1
2
3
# config/config.exs
config :phoenix_app, PhoenixApp.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.

1
2
3
4
# config/config.exs
config :phoenix, :template_engines,
  eex: Appsignal.Phoenix.Template.EExEngine,
  exs: Appsignal.Phoenix.Template.ExsEngine

Queries

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.

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

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.

Channels

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.

1
2
3
4
5
6
7
8
defmodule SomeApp.MyChannel do
  use Appsignal.Instrumentation.Decorators

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

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.

1
2
3
4
5
6
7
8
9
10
11
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}
    end)
  end
end

Custom instrumentation

You might be using your endpoint’s instrument/4 macro to create custom instrumentation. If you want those events to become part of the AppSignal timeline as well, you need to create a custom instrumenter module with the help of Appsignal.Phoenix.InstrumenterDSL.

1
2
3
4
5
6
7
8
defmodule PhoenixApp.CustomInstrumenter do
  import Appsignal.Phoenix.InstrumenterDSL

  instrumenter :phoenix_controller_call
  instrumenter :phoenix_controller_render
  instrumenter :custom_event
  instrumenter :another_custom_event
end

And then, use that instead of the AppSignal instrumenter in your config.exs.

1
2
3
# config/config.exs
config :phoenix_app, PhoenixApp.Endpoint,
  instrumenters: [PhoenixApp.CustomInstrumenter]

Want to help us improve this documentation page? Create a pull request