14 May 2020
Metrics in optimization process: Start to measure your app with the Prometheus tool (2/4)
Metrics are a great way to find useful information about our application and infrastructure. Before we start using metrics and preparing reports for the business, we need to integrate our app with some tools. That’s why today our focus is on how to measure, which means that we’re going to delve deep into how our application code works in the first place. We’re going to take a look at the Prometheus monitoring system and get deep into what is Prometheus monitoring.
In the previous article, we described how it is possible to uncover various problems with an app by finding bottlenecks in the project, why metrics are great to do this, and why we went for the Prometheus server monitoring to accomplish our objectives. Today, we’re going to actually start measuring the app.
Try using Prometheus metrics yourself!
Most of us prefer to check things by ourselves rather than implementing them without knowing if this whole thing works as expected. Luckily, there is a live demo that contains the default Prometheus dashboard and is integrated with Grafana.
What is Prometheus? All you need to know about integration with Prometheus
One of the best things about the Prometheus monitoring tool is the support for many languages. In most cases, we don’t need to worry about how to write integration and handle all the best practices. Here you can find a list of client libraries to work with the Prometheus software.
Currently, there are around 20 libraries for different technologies! If your technology is not on the list, you can follow a special guide that explains how to write your own integration.
The application which we needed to optimize and integrate metrics for was written in PHP/Symfony. We’re going to present examples of these technologies. Still, it would be easy enough to transfer it to something else.
Let’s get back to the application.
We wanted to save time and deliver a solution as soon as possible. We used the existing bundle instead of writing a new one. Our choice was the tweedgolf bundle. It’s a nice small library which has got everything that we needed.
Define the Prometheus data collection method
How does Prometheus work? Let’s take a closer look at monitoring Prometheus and Prometheus architecture.
Prometheus offers two different ways to collect data. Make sure you choose the one most suitable for your needs. More on that below:
- Push-gateway – the application pushes data to the special gateway (collector). It’s useful when we want to capture information for short-lived jobs.
- Scraped – the application is asked by a Prometheus instance for data that was collected.
In our case, we needed to focus on the general flow and behavior of an application. The second method was the best for us. We’re now going to talk about it in detail.
To achieve our main goal – measuring how our code works – we need to start measuring data just before we call the controller and stop it just after the code was executed.
Luckily, the Symfony Framework uses events so we’re able to create subscribers that handle two events.
Here is the basic concept for our code:
- validate data that we get from request/event (e.g. sometimes Symfony doesn’t return the correct action),
- check if we want to collect data for the specific endpoint (we don’t want to measure some actions because it doesn’t add any values),
- start collecting data,
- do the controller action,
- prepare the collected data,
- save the collected data to the storage.
Let’s take a look at a code example that shows how to start measuring the execution time for endpoints:
Now we need to add the configuration to know how we want to save everything:
And some additional config for subscriber:
Summary & Example Prometheus monitoring integration
As we promised in the first part of the series, we prepared a simple application to show you how it should all be implemented.
It contains all required validations, blacklists for endpoints, more metrics, and other important stuff required to set up metrics specified for PHP with the Symfony implementation.
This repository contains two ways to make the setup. The first is based on the Docker configuration file. If you work as a developer, it should be very easy to start and check how to integrate your application. The other way is set up with Kubernetes and kind (the Kubernetes Prometheus combination) – we are talking about it in the next part. On the whole, the third installment of the series focuses on Prometheus DevOps’ work. We will explain how to configure the Prometheus server and Prometheus database which will collect all our data in one place. You can also go right to the fourth part, which is about creating custom dashboards in Grafana for both devs and business folks.
So, what do you think about monitoring with the Prometheus tool and this kind of monitoring solution in general?