Reader small image

You're reading from  Building IoT Visualizations using Grafana

Product typeBook
Published inJul 2022
PublisherPackt
ISBN-139781803236124
Edition1st Edition
Right arrow
Author (1)
Rodrigo Juan Hernández
Rodrigo Juan Hernández
author image
Rodrigo Juan Hernández

Rodrigo Juan Hernández is an electronic engineer passionate about IoT even before it existed. He has been working on tech for more than 18 years until now. For several years he has been focusing on the IoT ecosystem. He is currently giving consultation about IoT systems to clients around the world. He also produces content online about IoT and related subjects and the content is available on his blog, Youtube channel, and social networks - mainly LinkedIn. He also writes for companies that need good quality content about their products and services. His main objective nowadays is helping others to understand and implement IoT solutions.
Read more about Rodrigo Juan Hernández

Right arrow

Chapter 11: Using Grafana with Prometheus

You can integrate many applications with Grafana. In this chapter, you will learn how to get data from a Prometheus instance.

In the previous chapter, you learned how to use Alertmanager, which is embedded in Grafana. This piece of software is the same one that’s used in the Prometheus system.

In this chapter, we will cover the following topics:

  • What is Prometheus?
  • Installing Prometheus
  • Feeding Prometheus with data
  • Integrating Prometheus and Grafana

Prometheus is a great partner for Grafana, and in this chapter, you will learn how to use both in IoT projects.

Technical requirements

To take advantage of this chapter, you will need the following:

  • A running instance of Grafana
  • A running instance of Prometheus
  • Data to feed to Prometheus

Let’s start by learning the basics about Prometheus.

What is Prometheus?

Prometheus is an open source monitoring and alerting system. It was developed by SoundCloud in 2012, but at the time of writing, it is a standalone project. So, it is entirely maintained by the community, independently of any company.

The main features of Prometheus are as follows:

  • It has a multidimensional data model, with metrics pairs in key/value form.
  • It uses a flexible query language called PromQL.
  • It collects metrics using pulls over HTTP. You can also push metrics using a push gateway.
  • You can build dashboards using the tools provided by Prometheus. However, in this book, you will learn how to use Grafana for that purpose.

The metric definition in Prometheus is the same one that we have been using throughout this book. It consists of pairs of keys and values that are delivered in a time sequence. These metrics are stored in a time-series database.

Architecture

Prometheus’s ecosystem has many components:

    ...

Installing Prometheus

You can follow three different methods to install Prometheus. Let’s look at each.

Installing from binaries

You can install Prometheus using precompiled binaries. All you have to do is download the binaries, copy them to some directory, and perform some configurations.

You can find the necessary pre-compiled binaries at https://prometheus.io/download/.

There, you will find also binaries for Alertmanager, pushgateway, and the official exporters – we will learn about exporters later.

You can install Prometheus for Windows 64 bits, Linux 64 bits, Darwin 64 bits, and even ARM7 systems, such as Raspberry Pi.

The following steps apply to installing Prometheus on an Ubuntu server:

  1. First, you must download the necessary binaries. You can find them at https://prometheus.io/download/.
  2. Extract the binaries, go to the directory where you extracted them, and move the binaries to the /usr/local/bin directory:
    $ tar xvf prometheus*.tar...

Feeding Prometheus with data

In this section, you will learn how to ingest data into Prometheus. Let’s look at all the different options that you have.

Prometheus clients

You can use client libraries to expose metrics on an endpoint that can be scraped by Prometheus using HTTP requests.

There are several language options you can select. You can find a complete list at https://prometheus.io/docs/instrumenting/clientlibs/.

You should choose the language that you used to implement your IoT application in your gateway or IoT device.

The idea behind the client libraries is to implement an endpoint that gets all the metrics that you need from the host and exposes them using HTTP. Then, Prometheus can scrap them by connecting to the endpoint through HTTP requests.

The client will serve the metrics using the metric types that we looked at in the previous section.

Exporters

You can expose metrics from endpoints using exporters. These are pieces of software that...

Integrating Prometheus and Grafana

So far, you have learned about the concepts surrounding Prometheus and the ways to get metrics into it. Now, let’s learn how to add the Prometheus data source and perform queries from Grafana.

Once you have a running instance of Prometheus, you can add the data source in Grafana, as shown in the following screenshot:

Figure 11.2 – Adding a Prometheus data source

Once you’ve added the data source, you will have to configure it so that it can access your Prometheus instance.

Let’s look at each of the configuration settings:

  • Name: Enter the name that you want to use to reference this data source.
  • Default: Select it if you want to set this data source as the default.
  • URL: The URL to access Prometheus.
  • Access: Here, you must select the server option (default) to be able to access the Prometheus server.
  • Basic Auth: Select this if you want to use basic authentication (user...

Summary

In this chapter, you learned about the fundamentals of Prometheus, as well as how to install and configure it.

First, you looked at different options for exposing metrics from IoT devices before learning how to use a Prometheus data source in Grafana. Finally, we went through a practical example, which consisted of using a DS18B20 sensor and a Raspberry Pi.

Without a doubt, Prometheus is a good option to consider when you want to implement an IoT system. And in combination with Grafana, you can obtain great flexibility.

In the next chapter, we will look at another integration option for Grafana: OpenSearch.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building IoT Visualizations using Grafana
Published in: Jul 2022Publisher: PacktISBN-13: 9781803236124
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rodrigo Juan Hernández

Rodrigo Juan Hernández is an electronic engineer passionate about IoT even before it existed. He has been working on tech for more than 18 years until now. For several years he has been focusing on the IoT ecosystem. He is currently giving consultation about IoT systems to clients around the world. He also produces content online about IoT and related subjects and the content is available on his blog, Youtube channel, and social networks - mainly LinkedIn. He also writes for companies that need good quality content about their products and services. His main objective nowadays is helping others to understand and implement IoT solutions.
Read more about Rodrigo Juan Hernández