We are excited to share that you can now send exception information from your Elixir applications to Honeybadger! installation and configuration are very straight forward so we're only going to look at how you use the Honeybadger package.
Let's dive in and see how easy this is to get set up.
Phoenix Applications
There are many things to like about Phoenix but one of the very best is that core to Phoenix's design is staying close to the Plug virtue of functions. Since Phoenix is so close to Plug, we can easily notify Honeybadger of any exceptions in your Phoenix application's web requests. Just use
the Honeybadger.Plug
module in your Phoenix application's router and you're done!
defmodule MyApp.Router do
use MyApp.Web, :router
use Honeybadger.Plug
pipeline :browser do
...
end
pipeline :api do
...
end
end
For debugging and customer experience purposes it can also be really helpful to send extra information about the request along with the exception. We provide the Honeybadger.context/1
function to accomplish this. A great technique is to add a plug
function that sets up the context for a request.
# in your application's router
plug :set_honeybadger_context
def set_honeybadger_context(conn, opts) do
user_id = current_user(conn)
context = %{user_id: user_id, account: user.account_id}
Honeybadger.context(context)
conn
end
OTP Processes
Sometimes we have work that needs to be done outside of the web process so that we can return a response as fast as possible. Examples of this are sending email or updating third party APIs. If you follow the guidelines of OTP architecture then Honeybadger rewards you.
Any process that conforms to SASL error reporting requirements and crashes will send an error message to the logger. Examples of these process types are GenServers, GenEvents, Supervisors, and any processes spawned using proc_lib
such as Tasks. This will be nearly all of the processes you have in any given application.
Simply set use_logger
to true when configuring Honeybadger and we will automatically take care of notifying Honeybadger of these crashes.
Manual Error Handling
While Elixir users inherit the Let It Crash mantra from our Erlang ancestors, the often untold, unsexy part of this is that we need to observe, understand and react to how our applications fail.
Sometimes we have critical code paths that need to succeed. If they don't, we need to know why. You can manually call Honeybadger.notify
to support these cases.
require Honeybadger
try do
File.read!("i_dont_exist")
rescue
ex ->
context = %{user_id: 1}
Honeybadger.notify(ex, context)
end
As a side note, the reason you need to require
the Honeybadger module before calling Honeybadger.notify
is because this function is actually a macro. With the power of a macro we can wipe away the function calls to notify in environments that you aren't interested in having exception tracking, such as test and development. Pretty cool, eh?!
Conclusion
At ElixirConf Europe Michael Schäfermeyer gave an excellent talk on using Elixir in a high scale web application. One of the key takeaways from that talk for me was that Elixir is missing some of the tools we might be accustomed to in other languages. We hope that by supporting exception tracking for Elixir applications we are doing our part to help get the Elixir community one step closer.