Integrate prom_ex into elixir phoenix application for Prometheus instrumentation

coding guy
4 min readMar 25, 2021

Today we are going to integrate prom_ex for Prometheus instrumentation.

Create a new Phoenix project

Let’s create a new phoenix project.

mix phx.new tracker

Configure and add library

then just add the following dependency in mix.exs .

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

Let’s just download both Grafana and Prometheus from the following links.

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

after downloading, extract the 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.

http://localhost:3000/dashboards

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response