Starting with Prometheus — easy ‘hello-world’ projects

This blog was originally written for and published at Grafana’s blog. 🧡

In this blog post, I would like to share with you some of the projects that I used to to get a better sense of what Prometheus can do. I am a very hands-on type of learner, and usually when I want to explore new technologies, I start with “hello world” apps and small toy projects. Therefore, the main goal of this blog is to share with you how easily you can set up Prometheus and how quickly you can create simple projects that can be monitored with Prometheus and visualized in Grafana. 🧡 I have found a lot of great materials and inspiration on this curated list. 🚀

Setting up Prometheus

As a first step, I would suggest to create dedicated Prometheus folder to store everything Prometheus-relevant and all of your toy projects. In Prometheus folder, I have created server folder, where I've downloaded and set up Prometheus monitoring system.

mkdir Prometheus/server

Prometheus starting guide is amazing and very straight forward. I don’t think it makes much sense to repeat what’s there (because I would literally just copy-paste it) and therefore, I am just going to link it here. Follow the steps to set up Prometheus (download → extract → configure → start → explore and play). Prometheus is serving metrics about itself and therefore you can start to explore its metrics right away.

Prometheus serving metrics about itself
Prometheus serving metrics about itself

Collecting metrics from Prometheus alone isn’t the greatest and the most interesting representation of Prometheus capabilities. That’s why, you can find following projects that I’ve used to to get a better sense of what Prometheus can actually do:

  • Monitor your own computer system with Node Exporter
  • Monitor express application with Prometheus Middleware
  • Monitor Github repos with Github Exporter

1. Monitor your own computer system with Node Exporter

Set up Node Exporter

For well-known applications, servers or databases, Prometheus and its amazing ❤️community have built exporters that you can use in order to monitor your targets. This is the main way of monitoring targets with Prometheus. Node Exporter is Prometheus exporter that exposes a wide variety of hardware and kernel related metrics. This means, that we can use Node Exporter to monitor filesystems, disks, CPUs, network statistics (and others) of our own computer system. For Node Exporter, I have created a new folder to set it up.

mkdir Prometheus/node_exporter

You can follow (another) amazing guide by Prometheus to set up Node Exporter (once again download → extract → configure → start → explore and play). The end result of the guide is Node Exporter running and exposing metrics on http://localhost:9100/ and Prometheus scraping metrics from that Node Exporter at http://localhost:9090/.

Visualise the metrics from your system with Grafana

If you are new to Grafana, as a first step, please follow the Grafana installation guide. As soon as you have your Grafana up and running, connect your Prometheus Datasource ( I have named mine datasource Prometheus - Node Exporter), that is served at http://localhost:9090/.

Setting up Prometheus as a datasource in Grafana

After connecting your datasource, you can import Node Exporter Server Metrics Dashboard that contains pre-made dashboards to visualise metrics from your computer. To import the dashboard, just copy its Dashboard ID (405), and use Import.

Import Node Exporter Server Metrics Dashboard
Set up Node Exporter Server Metrics Dashboard

You are now able to see visualised metrics from your own computer system.

Node Exporter Server Metrics Dashboard

2. Monitor express application with Prometheus Middleware

As a second project, I have decided to add Prometheus Middleware to the express application and monitor its performance. If you don’t have any particular application ready, you can use the boilerplate below to create the toy app. For this project, I have once again created a new folder.

mkdir Prometheus/prom_middleware
cd Prometheus/prom_middleware
code .

In the prom_middleware I have created index.js file and run yarn init. After that, I have run yarn add express express-prometheus-middleware to add packages that we are going to use. You can use the boilerplate below to create your toy app.

const express = require('express');
const promMid = require('express-prometheus-middleware');
const app = express();

const PORT = 9091;
app.use(promMid({
metricsPath: '/metrics',
collectDefaultMetrics: true,
requestDurationBuckets: [0.1, 0.5, 1, 1.5],
}));
app.get('/', (req, res) => {
console.log('GET /');
});
app.get('/hello', (req, res) => {
const { name = 'you' } = req.query;
res.json({ message: `Hello, ${name}!` });
console.log('GET /hello');
});
app.get('/hi', (req, res) => {
const { name = 'you' } = req.query;
res.json({ message: `Hi, ${name}!` });
console.log('GET /hi');
});
app.listen(PORT, () => {
console.log(`App listening at <http://localhost>:${PORT}`);
});

It is important to not to forget to add prom_middleware job to prometheus config file and restart prometheus server by re-running ./prometheus --config.file=prometheus.yml. You can then run this app by node index.js, and open its routes (/hi and /hello) several times, to be able to see apps metrics.

- job_name: 'prom_middleware'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9091']
prom_middleware app metrics
prom_middleware app metrics

Visualise the metrics from the app with Grafana

If you have connected your Prometheus to Grafana in the previous step, you are now able to create dashboard and panels for the prom_middleware application metrics. See example below.

Grafana dashboard with prom_middleware app metrics
Grafana dashboard with prom_middleware app metrics

3. Monitor Github repos with Github Exporter

As a last project, I’ve decided to try Prometheus to monitor Github repos using github-exporter. To do this, you are going to need Docker Compose. You can install Docker Hub (containing Docker Compose) via its official site.

As soon as your Docker is up and running, you can follow with the next steps — running docker image below. You can replace prometheus/prometheus repo with any repo of your choice.

docker run -d --restart=always -p 9171:9171 -e REPOS="prometheus/prometheus" infinityworks/github-exporte

After running this docker image, you need to add github_exporter to prometheus config file and restart prometheus server by re-running ./prometheus --config.file=prometheus.yml.

- job_name: 'github_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9171']

You can go to http://localhost:9171/metrics to see the available metrics.

You can now use Prometheus to run the queries.

Unfortunately, Github limits the number of queries from each IP address, and you can make only 60 queries/hour from unauthorised users. That’s why I would suggest to create your own docker image and supply your github token.

mkdir Prometheus/github_exporter
cd Prometheus/github_exporter
touch docker-compose.yml
code .

Create docker-compose file (for more information, visit github-exporter README):

github-exporter:
tty: true
stdin_open: true
expose:
- 9171
ports:
- 9171:9171
image: infinityworks/github-exporter:latest
environment:
- REPOS=prometheus/prometheus
- GITHUB_TOKEN=yourGITHUBtoken

Run docker-compose up and visit http://localhost:9171/metric to see the available metrics.

Visualise the metrics from the app with Grafana

And once again. 🙂 If you have added your Prometheus as a datasource to Grafana in one of the previous steps, you are now able to create dashboards and panels for the github_exporter metrics. See example below.

And that’s it!

If you have any other tips or tricks on how to use Prometheus, please leave a note in the comments. 🙂

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

jQuery: replace() in html()

Learn Javascript EventListener while traveling in the desert

React Native App — It’s a Redux Thing

Simple Classes

Radar Chart with Recharts

Introduction to NodeJs

How Much Does It Cost To Create A Matrimonial Site App

Angular 7 released! What’s new

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
Ivana Huckova

Ivana Huckova

More from Medium

About vSAN — 2 / Node Management

From zero to hero — JWT

Millions Of Secrets Exposed Via Web Application Frontend — An Internet-Wide Study — RedHunt Labs

Fundamentals of Top 10 Open Web Application Security Project

source: https://owasp.org/www-project-top-ten/