Exception Monitoring for Elixir with Honeybadger

We are going to show you how to set up exception monitoring for Elixir Phoenix with Honeybadger. Once you have deployed your Phoenix app to production, next you want to set up exception monitoring. When you enable exception monitoring with Honeybadger, it instantly notifies you when a user experiences any errors within your Phoenix application. Honeybadger was awarded Editor’s Choice 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 Honeybadger.

  1. Sign Up for Honeybadger
  2. Create New Elixir Phoenix App
  3. Add Honeybadger 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)

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

Sign Up for Honeybadger

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

Sign Up for Free Trial

Now that you have an account, Honeybadger 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.

Don’t forget to confirm your email address, so you’ll receive email notifications when we start sending errors later. Next we need to create a new Phoenix app.

Create new Elixir Phoenix App

We are going to create a simple Phoenix project called HelloHoneybadger. 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_honeybadger

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

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

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

Welcome to Phoenix

Add Honeybadger config

We need to make a few updates to our app to configure it to start monitoring for errors and sending them to Honeybadger.

Install package

Add the Honeybadger package in your application’s mix.exs file and run mix do deps.get, deps.compile

  def application do
    [
      mod: {HelloHoneybadger.Application, []},
      extra_applications: [:honeybadger, :logger, :runtime_tools]
    ]
  end


  defp deps do
    [{:honeybadger, "~> 0.16"}]
  end

Next we’ll configure our app with the API key and environment variables.

Set your API Key

After you sign up, Honeybadger will provide you a copy your API key.

By default your app will use the environment variable HONEYBADGER_API_KEY to find your API key. We will set it in our environment using the command line.

$ export HONEYBADGER_API_KEY=sup3rs3cr3tk3y

Alternatively, you can set it in config.exs. However, we recommend avoiding committing any api keys into your Git repo, especially if hosted publicly on GitHub.

config :honeybadger, api_key: "sup3rs3cr3tk3y"

Enable exception monitoring and error reporting

The Honeybadger package can be used as a Plug in our Phoenix application. The Honeybadger.Plug adds a Plug.ErrorHandler to your pipeline.

Simply use the Honeybadger.Plug module inside of your Phoenix.Router and any errors will be automatically reported during each request. It’s best to use Honeybadger.Plug after the Router plugs so that exceptions due to non-matching routes are not reported.

defmodule HelloHoneybadger.Router do
  use HelloHoneybadger.Web, :router
  use Honeybadger.Plug

  pipeline :browser do
    [...]
  end
end

Complete setup

Once you finish updating your code, click Complete Setup within the onboarding process to see the following screen.

exceptions and errors monitoring set up success
Successful Onboarding

Click on View my project to see your dashboard. At this point, it should be empty since you haven’t sent any errors. Next we’ll create an exception to see how everything works.

blank dashboard

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_honeybadger_web/controllers/page_controller.ex.

defmodule HelloHoneybadgerWeb.PageController do
  use HelloHoneybadgerWeb, :controller

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

By default, Honeybadger will only report errors in :prod environment and you normally would only run it in production. However, we want to test in development without deploying to production.

We need to update the config.exs.

config :honeybadger, exclude_envs: [:test]

The default list is [:dev, :test] if exclude_envs is not included.

Test Error Reporting and Notifications

Finally, we’re going to see how Honeybadger notifies developers when errors occur in your web service. Before we go further, make sure you’ve confirmed your email address otherwise you won’t receive the email notification.

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 ArgumentError

Your dashboard will list the error. While the error is Unresolved, you will not receive additional notifications for this specific issue.

Dashboard with Notification

If you click on the title of the error, you will get more details.

error details

You should also receive an email notification with details about the error and a link to sign in to your dashboard report.

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

Notification Settings

Now that we’ve seen how exception notifications work, you can remove the error from the PageController and remove the config we added for exclude_envs. Then mark the error as Resolved in your dashboard.

Dashboard resolved

Conclusion

Congratulations! You set up exception monitoring for Elixir Phoenix using Honeybadger. It is an excellent 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 uptime checks and deployment tracking.

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!