Instrumenting phoenix with Prometheus

Why?

  • Instrumentation is about collecting metrics for your application. You can collect database query time, response time, basically anything you want. If you have a particular thing to measure, prometheus elixir package makes it really easy to collect metrics for your phoenix app.
  • Even if there isn’t anything in particular you want to measure, some prometheus adapters provide you with more granular metrics out of the box (Ecto’s query’s average time, controller average response time, view average render time…)

In this small article We will cover the installation of the prometheus adapters on your phoenix app and the deployment with docker.

We will be

How

add the following dependencies to your mix.exs

(here we are using override just to use the latest version of prometheus itself)

in your config/config.exs add

MyApp.RepoInstrumenter , MyApp.PhoenixInstrumenter and MyApp.PipelineInstrumenter correspond to three modules that we will create next (of course you can name the modules whatever you want). I put these modules under myapp/instrumenters/repo_instrumenter for example, but that is also a matter of taste.

add a myapp/instrumenters/repo_instrumenter.ex with the following

add a myapp/instrumenters/phoenix_instrumenter.ex with the following

add a myapp/instrumenters/pipeline_instrumenter.ex with the following

now in myapp/application.ex add

Setting up prometheus and grafana

prometheus will collect the metrics, but you need something to visualize them. Let’s use grafana and let’s set everything up with docker.

if you haven’t set up your deployment with docker, check this for help.

once you have a docker-compose.yml file, add the following under the services list

add a prometheus/prometheus.yml file on your local with (this is just the default prometheus config file, feel free to customize)

so basically we are creating a volume to store the prometheus config and we are linking prometheus and grafana services together. Here is how your docker-compose file should look like.

verify that your docker setup is working with docker-compose up

you can go to localhost:3000 in your browser and should see the grafana interface. Default user and password is admin (for both).

Next you need to add a datasource to Grafana. On the left, on the settings icon, click on datasource to add a new datasource. Name it whatever you want (ex MyApp Instrumentation). choose prometheus as the source type. Use http://prometheus:9090 as the url. And click on save & test. It should confirm that the setup is correct.

Next you need to add a dashboard. You can create your own dashboard, but let’s use some open source one at first. On Grafana’s interface on the left, click on the + and import. You can directly paste some JSON in there. There is a repo with lots of wonderful dashboards. Three are particularly useful

the BEAM dashboard (you can directly copy and paste the json from the repo into the import part of grafana)

The beam allocators dashboard

and the elixir dashboard (which is the one we want for phoenix)

These three dashboard will work out of the box with our setup.

The packages we installed, give you a lot more metrics, so you can make different dashboard. Dashboard customization will be for another article :-)

This article is part of the phoenix unofficial guide ( a guide that relates to everything requiring third parties and phoenix (deployement, continuous integration, instrumentation…)). This article will be updated on the github repo here.

based on the really good article written in 2016 (I wrote an article, just because I got a stuck when I tried to apply that article). By the way, if you ever get stuck, the #prometheus channel on elixir slack, is just awesome.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store