Integrate prom_ex into elixir phoenix application for Prometheus instrumentation

Today we are going to integrate prom_ex for Prometheus instrumentation.

Create a new Phoenix project

mix tracker

Configure and add library

defp deps do[
{:prom_ex, "~> 1.0.1"}

Then run the following command

mix prom_ex.gen.config --datasource Prometheus

Here data source should be the name of the Prometheus name configured in the Grafana data source.

The command generates required files, now we have to just add some lines into our code and we’ll be done.

Add the following line into endpoint.ex having TrackerWeb.Endpoint module.

Open the application.ex and add the following line

Also if you want you can uncomment the following lines from prom_ex.ex file

Now if you run your application iex -S mix phx.server and then go to localhost:4000/metrics you should see something like this.

These are the metrics of your application, wow we just need to configure Grafana and Prometheus.

Setup Grafana and Prometheus

grafana download | Prometheus download

They are easy to download and quite straightforward.

tar -xzvf grafana-7.4.5.darwin-amd64.tar.gz

This would extract the tar.gz file then we’ll go to the extracted folder and run the ./bin/grafana-server and now localhost:3000 give the default user name and password, admin and admin it’ll ask you to reset the password do it and it’ll show you a screen something like this.

Setup Prometheus

tar -xzvf prometheus-2.26.0-rc.0.darwin-amd64.tar.gz

go to folder

cd prometheus-2.26.0-rc.0.darwin-amd64

open the prometheus.yml and edit

Open the localhost:9090/targets you should see the following.

These will scrape our project on localhost:4000/metrics

Now let’s generate a new API key so that prom_ex can create the require dashboards and folders into grafana.

You’ll get API key, just give editor/admin permissions and copy the key.

After copying the key go to config.exs and add the config.

Your API key should be in auth_token

Now restart the project, then it’ll create the required folders and dashboards into grafana go to the following link.


You should see something like this.

let’s checkout the dashboards 😄

if you have setup the repo properly you’ll see some data related to ecto too but I didn’t so I’m getting something like this :) I hope you’ll see the right data.

Let me know if you are facing any issues in setup, I’ll be happy to help.

Be happy have a great day 🙏 Namaste.

I write code