Monitoring CI Metrics
local graph
Chapter 18 — Monitoring CI Metrics
Section titled “Chapter 18 — Monitoring CI Metrics”In this chapter, we will show you how to configure GitLab and its Runners to expose service metrics. These statistics are then collected by a system that specializes in data with a time dimension. GitLab uses Prometheus to do this and so will we.
Prometheus also provides the Alertmanager application, where you can define alert rules that trigger customizable actions, such as sending a mail or triggering a webhook, as described in Chapter 13, Integrating GitLab with CI/CD Tools. You can then either silence or deal with the alert. We will provide an example of how you can use this to enable an alert when some threshold you set is breached and the GitLab Runner is malfunctioning or not doing what you expect it to do.
In this chapter, we will cover the following topics:
- Enabling monitoring for Runners
- Enabling the GitLab Runner configuration file
- Runner business logic metrics
- General processing metrics
For managing omnibus installation, there is one central configuration file, called gitlab.rb. You need to create it or copy an example. There is a template available at https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template. It is not updated after upgrades. In large parts of this chapter, we will quote and discuss parts of this file. Furthermore, we will use code examples from the GitHub repository that accompanies this book and you will need Prometheus and the Alertmanager software to run the samples:
- Code examples (https://github.com/PacktPublishing/Mastering-GitLab-12).
- Prometheus monitoring server (https://prometheus.io).
- Prometheus Alertmanager (https://prometheus.io/docs/alerting/alertmanager/).
- There are also containerized versions (find them with Docker by searching Prometheus):
- Alertmanager: https://hub.docker.com/r/prom/alertmanager/
- Prometheus: https://hub.docker.com/r/prom/prometheus/
- Alertmanager: https://hub.docker.com/r/prom/alertmanager/
You can find the code file for this chapter in this book’s GitHub repository at https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter18.
The GitLab omnibus installation package supports defining several monitoring components. At the time of writing, it does not have a built-in way to deploy GitLab Runners. You can deploy Runners with the Kubernetes GitLab Runner helm chart and monitor those, but for Runners that you created yourself, you are responsible for enabling the monitoring of them. What do you achieve when monitoring is enabled? Well, especially in an autoscaling environment, monitoring can keep you informed about how your fleet of Runners is doing. Monitoring can give you some insights into how your resources are used. The metrics are stored historically, so you can notice trends after some time.
The following is a high-level example of a monitoring setup for the architecture we created in the previous chapter. We have a GitLab instance with continuous integration (CI) enabled on the left-hand side:

There is a GitLab Runner instance using a Docker Machine executor that can spawn GitLab Runner containers. The driver that is used can be of any type that Docker Machine supports. We used Amazon and VirtualBox to create a small-scale infrastructure that always has one idle Runner and scales up when there are more jobs in the queue. In this chapter, we’ll talk about an example where we start up a separate Prometheus monitoring Docker container, and then make that monitoring application scrape the metrics that are generated by the GitLab Runner. The Prometheus monitoring system can also send alerts when certain thresholds are reached. There is a separate binary for this (the Alertmanager) and we’ll choose to start it in a separated Docker container. It can also be manually installed on a dedicated server.
The preceding information is about the basic monitoring setup with alerting, which will give you more insight into the metrics of your GitLab Runners.
First, we have to configure the Runner to expose information, which is quite easy to do. The config.toml file, which resides at ~/.gitlab.runner on my system (macOS), has to be edited. We can enable the built-in server to serve information by adding a line with listen_address to this file, as shown in the following code block:
listen_address = ":9252"After saving this file, we must restart our Runner with the following command (on macOS and a Homebrew installed Runner):
$ brew services restart gitlab-runnerOn Linux or on a bare macOS installation, we can restart the Runner via the following command:
$ sudo killall -SIGHUP gitlab-runnerWe can view the exposed information by opening the URL where it is running (in my case, http://192.168.178.82:9252), and then appending the metrics path (/metrics):

Now that we have exposed this information, we want to capture it in a database that checks the data every X seconds. We are going to start a Docker container with Prometheus to do this. We will start the container with the following configuration file:
global: scrape_interval: 15s scrape_timeout: 10s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: [] scheme: http timeout: 10s scrape_configs: - job_name: prometheus honor_timestamps: true scrape_interval: 15s scrape_timeout: 10s metrics_path: /metrics scheme: http static_configs: - targets: - localhost:9090 - targets: - 192.168.178.82:9252The important part right now is as follows:
- targets: - 192.168.178.82:9252As you can see, this is the exact URL of our Runner’s metrics data. Now, we start the Prometheus container with the config file as an argument (it will download the container from Docker Hub if you do not have it locally):
$ docker run -p 9090:9090 -v /Users/joostevertse/srv/prometheus:/etc/prometheus prom/prometheus level=info ts=2019-05-26T20:08:31.338Z caller=main.go:286 msg="no time or size retention was set so using the default time retention" duration=15d level=info ts=2019-05-26T20:08:31.339Z caller=main.go:322 msg="Starting Prometheus" version="(version=2.10.0, branch=HEAD, revision=d20e84d0fb64aff2f62a977adc8cfb656da4e286)"We can interrupt the container with Ctrl+ C, but we started it this way as an example. If we use the -d argument to the Docker run command, the container will run in the background.
In this example, the Prometheus container is running on port 9090, and the port is published via Docker -p 9090:9090.
If we visit the page (in my case, http://localhost:9090), we will be presented with a fresh Prometheus installation:

To find out whether it is actually getting information from the Runner, click on -insert metric at cursor -. We will see some Runner-related metrics, as follows:

In the next section, we’ll demonstrate some metrics that have value in determining the state of your builds.
Imagine that you have several developers working on a system and you have several integration tests that should pass before you can deploy to a production environment. If speed is important for your business, then you need to build your system quickly. To do this, there have to be GitLab Runners available and ready to run jobs. If they are unavailable or they perform badly, your development will slow down and so will your business. What follows are some business logic metrics that are viewable in Prometheus.
There are a lot of metrics available, but let’s look at some that have proven useful in the past:
- gitlab_runner_jobs: It is wise to monitor the number of pending jobs:

If this number gets too high, consider adding a higher limit for autoscalable Runners.
- gitlab_autoscaling_options_total: Another interesting metric is the type and number of scaling actions that are performed on the Runner:

The actions created and removed should be about the same (too many created is expensive, and too many removed would signal a problem). When it is busy, you want a growing number of used instances, as this would indicate efficient resource use.
- gitlab_runner_jobs_total: The gitlab_runner_jobs_total information is nice to have during capacity planning:

If there is an ever-increasing number of jobs, scale the number of Runners up before you run out of capacity.
- gitlab_runner_autoscaling_machine_creation_duration_seconds_count: The Runners in this example are autoscaled on different platforms (Amazon and VirtualBox). The main GitLab Runner instance kept track of how many machines were created and how much time it took to spin up new machines:

This can help you to determine whether you are running on the right platform.
- gitlab_runner_limit: If you are going to experiment with the limit of Runners that can be spawned by the Docker Machine executor, this metric is useful to record and you can use it in queries later on in order to compare data:

If you see the limit growing, it means your business is growing.
- gitlab_runner_errors: The next graph demonstrates the rate function, which can be used to query metrics and show how much the metric has increased or decreased for a chosen interval. The function is part of PromQL, which is the language for querying data in Prometheus:

Here, we check the total number of errors that are generated by different Runners.
- gitlab_runner_failed_jobs_total: A potentially interesting metric is the rate of the number of failed jobs. In the following graph for our Runner, we can see that there is a sudden spike:

We should have no failed jobs at all, so this is a candidate for alerting, which we will discuss in the Alert management section of this chapter.
It is helpful to have metrics about the number of jobs processed, or the speed of the jobs. But, sometimes, we may also want to know whether the machine that is hosting the Runner is experiencing operational issues, such as low memory or CPU contention. The Prometheus exporter also records general process metrics such as these.
The following metrics only cover the host in which the Docker Machine executor is running:
- process_cpu_seconds_total: If you have a small machine, it is useful to know whether the CPU is coping with the load. There is a count that records the total number of CPU seconds used by the Runner process:

A big increase in a short period of time could indicate issues.
- process_open_fds: There is a maximum number of open file descriptors on a Unix system. If this number is reached, the system will generate errors and won’t open files anymore:

If the maximum is reached, you can expect your jobs to fail.
- process_resident_memory_bytes: This is the amount of memory Prometheus is using from the kernel that is real memory and not virtual or swap memory. Big changes in this value can indicate issues:

As you can see, there are no problems on our demonstration server.
- process_virtual_memory_bytes: This metric represents all memory (including RAM and swap) that is managed by the Runner process:

Once again, there are no issues on our demonstration server.
- process_start_time_seconds: If there are issues, it is nice to know the time when the issues started and how they have developed:

Recording the start time of the process can help debug issues.
- scrape_duration_seconds: The Prometheus server scrapes (downloads) the information that is exposed by the exporter. It also records how much time the scraping took:

Long scraping times could indicate a network problem or a slow Runner.
- go_gc_duration_seconds_count: There are also metrics about the Golang runtime environment that the Runner is running in; for example; the amount of time it takes to do garbage collection:

These metrics are very detailed, and are more interesting for developers of the GitLab Runner than for users.
All the general process metrics combined can give you an idea of how the Docker Machine Runner is behaving, and can be used to discover trends. Of course, it is also nice to be alerted to a value that has reached a certain threshold; that is the subject of the next section.
Prometheus’ Alertmanager (https://prometheus.io/docs/alerting/alertmanager/) is a tool that is especially designed for bigger infrastructures. Pager and alerting programs have been around for a long time, but this one and Prometheus itself are highly optimized for working in a scaling infrastructure. It is also available as a Docker container: https://hub.docker.com/r/prom/alertmanager/.
As we saw in the infrastructure diagram at the beginning of this chapter, we use a separate Prometheus and Alertmanager containers to demonstrate our Runner metrics collection.
The Alertmanager container has a configuration file (srv/prometheus/alertmanager.yml):
global: # The smarthost and SMTP sender used for mail notifications. smtp_smarthost: 'smtp.xs4all.nl:25' smtp_from: '[email protected]'
route: repeat_interval: 3h receiver: joustie
routes: - match_re: service: ^(.*)$ receiver: joustie
receivers: - name: 'joustie' email_configs: - to: '[email protected]'The configuration has an email host defined, and it routes alerts via email to [email protected] every 3 hours if it is not silenced.
The Alertmanager Docker container can be started with the following command:
docker run -dp 9093:9093 --name=prom_alertmanager -v /Users/joostevertse/srv/prometheus/alertmanager.yml:/alertmanager.yml prom/alertmanager --config.file=/alertmanager.ymlWe have already started a Prometheus container, but to make it connect to the Alertmanager container, we need to alter its configuration in two ways. We need an alerts file in which we determine what event should trigger an alert. In our example, we check the number of failed Runner metrics and make it turn red when there are more than two failed jobs. The alert.rules file for this is as follows:
groups: - name: joberror rules: - alert: HighErrorRate expr: gitlab_runner_failed_jobs_total > 2 for: 1m labels: severity: email annotations: summary: High job errorsThis configuration shows that [email protected] will receive an email when there are more than two failed jobs.
The second change is to the main Prometheus configuration file (srv/prometheus/prometheus.yml). This new section is all about adding an alerting configuration (an Alertmanager is added that is running on 192.168.178.82 port 9093):
alerting: alertmanagers: - static_configs: - targets: ["192.168.178.82:9093"] scheme: http timeout: 10sAfter changing the file, we have to activate the new configuration. We have to restart the container from scratch for this:
$ docker stop prom_server ff55a556772 $ docker rm prom_server ff55a556772 $ docker run -dp 9090:9090 --name=prom_server -v /Users/joostevertse/srv/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /Users/joostevertse/srv/prometheus/alert.rules:/etc/prometheus/alert.rules prom/prometheus --config.file=/etc/prometheus/prometheus.ymlNotice that the container is recreated because we have to link the alert rules into the container.
After the containers have started, and we have kicked off some jobs and made them fail, there should be an alert triggered. You can find the alert in Prometheus in the web interface (in our case, http://localhost:9090/alerts):

We can also view this file in the Alertmanager instance. In our example, we go to http://localhost:9093/#/alerts to find the alert:

The Alertmanager was configured to send emails, so we received the following email:

The Alertmanager has the concept of a Silence, which means you can suppress alerts from being sent depending on certain matches. If, for instance, we temporarily want to suppress alerts for HighErrorRate, we can set a duration of 2 hours, during which those alerts will be silenced:

With Alertmanager enabled and the alerts defined, it is much clearer how GitLab Runners perform, and if they are generating errors. You certainly need monitoring like this to learn from errors and create a faster and higher-quality CI/CD pipeline.
In this chapter, we continued with the Runner architecture from the previous chapter and showed you a way to monitor the autoscaling Runner, no matter which Docker Machine drivers are being used. The Prometheus monitoring solution offers a highly scalable time series database, and together with the Alertmanager, you can create sophisticated monitoring. In the example we used, Docker containers are used extensively as Prometheus is cloud-native software (https://prometheus.io/blog/2016/05/09/prometheus-to-join-the-cloud-native-computing-foundation/), but the software can also be run on dedicated hardware.
In the next chapter, we will learn about the options available to create a GitLab highly available (HA) infrastructure. In the first section of that chapter, we will discuss the basic setup for that.
- What is the collection of metrics done by the Prometheus server called?
- What is the Prometheus client program called?
- In which section do you define a server to be monitored?
- What is the default Prometheus port?
- Name the first option to try when there are a lot of pending jobs.
- Which PromQL function is used to query the average rate of increase for a dataset?
- Practical Site Reliability Engineering by Pethuru Raj Chelliah, Saravanan Pitchaimani, and Babu Jayaraj:
- Cloud Native Programming with Golang, by Mina Andrawos and Martin Helmich: