Exception Monitoring for Elixir Phoenix with AppSignal

We will show you how to set up exception monitoring for Elixir Phoenix with AppSignal. Once you have deployed your Phoenix app to production, next you want to set up exception monitoring. When you enable exception monitoring with AppSignal, it instantly notifies you when a user experiences any errors within your Phoenix application. AppSignal was awarded Best Onboarding and Easy Setup in our article covering the Best Error Monitoring Services for Elixir and Phoenix.

You will start by signing up for a free trial, create a new Phoenix application, then set up the exception monitoring tool, and finally test notifications in development on your local machine.

Overview

Here are the steps we’ll cover to set up and test Elixir exception monitoring with AppSignal.

  1. Sign Up for AppSignal
  2. Create New Elixir Phoenix App
  3. Add AppSignal config
  4. Add Error to Phoenix Homepage

Prerequisites

To follow this guide, make sure you have at least the following applications installed on your machine. In the brackets are the versions with which this guide was written.

  • Elixir (1.12.3-otp-24)
  • Erlang (OTP 24.1)
  • Phoenix (1.6.2)
  • Node.js with npm (15.14.0)

NOTE: You also need to install xcode-select if you are using macOS. If it is not installed, you will receive a Error loading NIF message.

$ xcode-select --install

If you run into problems, the finished project is available on GitHub as StakNine HelloAppSignal.

Sign Up for AppSignal

Before we get started with the Phoenix app, you need to sign up for a free trial with AppSignal. We’ll use the trial period to see how it works.

Sign Up for Free Trial

Now that you have an account, AppSignal will guide your through the setup process. There are a number of devops tools and supported languages. We intend to set up the exception monitoring tool in this guide. Select Elixir for your programming language.

AppSignal Elixir

Once you select Elixir, you’ll received instructions to set up you application. Before these steps, we need to create a new Phoenix app.

Create new Elixir Phoenix App

We are going to create a simple Phoenix project called HelloAppSignal. Use the command line to update your Phoenix version and create a new Phoenix application.

$ mix archive.install hex phx_new 1.6.2
$ mix phx.new hello_app_signal

Change into the project directory, create your database and start your server.

$ cd hello_app_signal
$ mix ecto.create
$ mix phx.server

Now visit http://localhost:4000 and you should see the Welcome to Phoenix! page.

phoenix homepage

Add AppSignal config

We need to make a few updates to our app source code to configure it to start monitoring for errors and sending exceptions to AppSignal. Since we’ll be using AppSignal with Phoenix, we’ll use the Phoenix installation instructions rather than the regular Elixir instructions you see below.

AppSignal Installation

Install package

The Phoenix integration is in a separate library named :appsignal_phoenix, which depends on the main :appsignal library. To use AppSignal in a Phoenix app, add :appsignal_phoenix to your dependencies. You do not need the :appsignal dependency.

defmodule HelloAppSignal.MixProject do
  # ...

  defp deps do
    [
      # ...
      {:appsignal_phoenix, "~> 2.0.0"}
    ]
  end

  # ...
end

Then run mix deps.get.

Finally run mix appsignal.install YOUR_PUSH_API_KEY. You can find the API key in the 3rd step of the standard Elixir AppSignal installation instructions, see image above.

The mix appsignal.install command will inject the required configuration into your application for you and then run you through a quick set up sequence in the command line. We used the default for all options.

To see the changes, check out this commit in our demo project.

Once you complete the command line installation setup, click Next Step to go to the Check for data step.

Install Check for Monitoring Data

Once AppSignal confirms your app generated the example issues, you will receive the following screen with a button to Go to app.

Error Monitoring Install Complete

Once you complete the installation sequence, you’ll see the AppSignal Dashboard.

AppSignal Dashboard

Our project only sent a performance event, which you can see below.

AppSignal Sample Performance Monitoring Event

Since we didn’t get a sample error, we’ll create one in our project. The default config sends errors in development, so we don’t need to modify the config in our project.

# config/dev.exs

config :appsignal, :config, active: true

Add error to Phoenix homepage

Before we can test reporting Elixir errors, we need to create them. If we want to specify the type and message, we need to use raise/2.

Lets add this to our lib/hello_app_signal_web/controllers/page_controller.ex.

defmodule HelloAppSignalWeb.PageController do
  use HelloAppSignalWeb, :controller

  def index(conn, _params) do
    raise ArgumentError, message: "We are testing error reporting with AppSignal"
    render(conn, "index.html")
  end
end

Test Error Reporting and Notifications

Finally, we’re going to see how AppSignal notifies developers when errors occur in your web service.

We’ve added an error to the homepage controller, so we will see errors when we start the server and visit the homepage.

$ mix phx.server

Now visit http://localhost:4000 and you should see the following screen in your browser.

Phoenix Browser Error

Now click on Errors in the AppSignal sidebar to see the list of errors. You’ll see the error we just sent. You should have also received an email notification with the error details.

AppSignal Error List

If you click on the error title, you’ll see the error details including the stacktrace. While the error is Open, you will not receive additional notifications for this specific isssue.

App Signal Error Details

Notification Integrations

You can add more notification channels in the settings. For example, you could notify other developers in your company using your project management tools.

AppSignal notifications settings

Clean up and close error

Now that we’ve seen how exception notifications work, you can remove the error from the PageController and update your config to stop monitoring for errors in development.

# config/dev.exs

config :appsignal, :config, active: false

Then mark the error as Closed in your dashboard.

AppSignal closed and resolved

Conclusion

Congratulations! You set up exception monitoring for Elixir Phoenix using AppSignal. It is a good tool to notify you and other developers on your team or company when there are exceptions in you system. Once you have this set up, check out their other developer tools with features like performance monitoring and uptime monitoring.

Want To Know How To Deploy Phoenix Apps Using A single Command?

This brand-new FREE training reveals the most powerful new way to reduce your deployment time and skyrocket your productivity… and truly see your programming career explode off the charts!