S entry’s real-time error tracking gives you insight into production deployments and information to reproduce and fix crashes.

It’s a great tool for error tracking and really helps you monitor your Elixir / Phoenix applications. In this tutorial I’d like to explain you how to integrate sentry with Elixir project and run it inside a Docker container.

Library

We’re gonna use sentry-elixir library because it provides out-of-the-box integration with sentry.io which exposes a simple API to capture exceptions, automatically handle Plug Exceptions and provides a backend for the Elixir Logger.

Configuration

The configuration itself is no different than the official one presented in the documentation. If you follow all the steps you should have your application up, running and reporting all errors to sentry.io dashboard.

The only important thing to get from the dashboard itself is the DNS address you need to configure:

config :sentry, dsn: "https://public:secret@app.getsentry.com/1"

You can find it under the following link: https://sentry.io/YOUR_ORGANIZATION/YOUR_APP/settings/keys/

Dockerization

There are a couple of important things to keep in mind when building a Docker image.

If you follow Best practices for writing Dockerfiles, you probably copy just mix.exs and mix.lock files firstly to leverage caching. Then, you install and compile all dependencies.

COPY mix.* ./

RUN mix do deps.get, deps.compile

In such case, your build will result in the following error:

== Compilation error on file lib/sentry/event.ex ==

** (ArgumentError) application :sentry is not loaded, or the configuration parameter :enable_source_code_context is not set

(elixir) lib/application.ex:261: Application.fetch_env!/2

lib/sentry/event.ex:38: (module)

(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6 could not compile dependency :sentry, “mix compile” failed. You can recompile this dependency with “mix deps.compile sentry”, update it with “mix deps.update sentry” or clean it with “mix deps.clean sentry”

This happens because no config files are available for sentry compilation. To fix the problem, you need an additional step before fetching and compiling sentry-elixir :

# …

COPY config/*.exs ./config/ RUN mix do deps.get, deps.compile

With the entry above you will be able to start compiling sentry dependency.

### HOME directory

However, sentry itself recursively reads files at compile time in the root project directory and use File.cwd!/1 for that, so if you put your project in root directory ( / ), it will take a lot of time to complete.

To “fix” that, here’s an entry to add in your Dockerfile :

WORKDIR /opt/your-application-name

Once you have this added, you should be able to successfully build a Docker image: