Jenkins monitoring with Prometheus and Grafana

In this post, we’ll explain how to configure Prometheus and Grafana to monitor the Jenkins CI-CD instance.

· Prerequisites
· Overview
∘ What is Prometheus?
∘ What is Grafana Open Source?
∘ Why is it important to monitor Jenkins?
· Set Up Jenkins
∘ Install the Prometheus Plugin for Jenkins
∘ Configure Jenkins for Prometheus metrics
· Set Up Prometheus and Grafana
∘ Set Up Prometheus
∘ Configure Grafana datasources
· Visualizing
∘ Explore Prometheus
∘ Explore Grafana
∘ Grafana Alerts
· Conclusion
· References

Prerequisites

This is the list of all the prerequisites:

  • An installed Jenkins server
  • Docker / Docker compose installed (optional if you’ve already installed Prometheus and Grafana)
  • Basic knowledge of Prometheus and Grafana

Overview

What is Prometheus?

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open-source project and maintained independently of any company. To emphasize this, and to clarify the project’s governance structure, Prometheus joined the Cloud Native Computing Foundation in 2016 as the second hosted project, after Kubernetes.

https://prometheus.io/docs/introduction/overview

Prometheus collects and stores its metrics as time series data, i.e. metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.

What is Grafana Open Source?

Grafana open source is open-source visualization and analytics software. It allows you to query, visualize, alert on, and explore your metrics, logs, and traces no matter where they are stored. It provides you with tools to turn your time-series database (TSDB) data into insightful graphs and visualizations.

Why is it important to monitor Jenkins?

Monitoring Jenkins is essential and provides a wealth of information about the health of your CI/CD infrastructure.

  • Resource Management: Monitoring system memory, Garbage Collector (GC), JVM, and CPU usage.
  • Issue Detection: Jenkins monitoring helps detect potential issues early.
    For example, it will give you enough time to detect space issues before they become major problems.
  • Collaboration: Keeping the team informed about build status and metrics fosters better collaboration and accountability.
  • High availability: Proper monitoring can prevent system failures. It ensures that your CI/CD infrastructure is always available.

Set Up Jenkins

The first step consists of setting the Jenkins instance.

Install the Prometheus Plugin for Jenkins

  1. Login to Jenkins
  2. Go to Manage Jenkins > Manage Plugins.
  3. In the Available tab, search for the prometheus plugin.
  4. Install the plugin and restart Jenkins if necessary.

Configure Jenkins for Prometheus metrics

Once we have installed the plugin Prometheus metric, we need to configure it.

Jenkins Prometheus Plugin exposes an endpoint (default /prometheus/) with metrics that a Prometheus Server can scrape.

  1. Go to Manage Jenkins > System.
  2. Find the Prometheus section.

Apply and Save the configuration.

For further information about the Prometheus plugin, consult the official documentation.

Set Up Prometheus and Grafana

This story uses docker containers to run Prometheus and Grafana instances. Here is the full content of Docker-compose:

version: "3.8"
services:
prometheus:
image: prom/prometheus
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
networks:
- monitoring-network

grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
volumes:
- ./grafana:/etc/grafana/provisioning/datasources
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
networks:
- monitoring-network

networks:
monitoring-network:

Set Up Prometheus

By default, Prometheus looks for the file prometheus.yml in the current working directory. This behavior can be changed via the --config.file command line flag. For example, some Prometheus installers use it to set the configuration file to /etc/prometheus/prometheus.yml

We need to edit the prometheus.ymlconfiguration file inside the container to configure the Prometheus instance.

global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.

# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's our Spring Boot api
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'jenkins'

metrics_path: '/prometheus'
static_configs:
- targets: ['<jenkins-ip>:<jenkins-port>'] # e.g., 'localhost:8080'
labels:
application: 'jenkins monitoring'

Replace <jenkins-ip> and <jenkins-port> with yours.

For a complete specification of configuration options, see the configuration documentation.

Configure Grafana datasources

Grafana ships with built-in support for Prometheus. We can manage data sources in Grafana by adding YAML configuration files in the provisioning/datasources directory. Each config file can contain a list of datasources to add or update during startup. If the data source already exists, Grafana reconfigures it to match the provisioned configuration file.

Here is the data source YAML that allows Grafana to connect with Prometheus.

apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
# Access mode - proxy (server in the UI) or direct (browser in the UI).
url: http://prometheus:9090
# use it as the default data source
isDefault: true

Visualizing

Let’s start the services by running the below command:

$ docker compose up -d

Prometheus is accessible via http://localhost:9090 and Grafana is accessible via http://localhost:3000.

Explore Prometheus

Explore Grafana

Default username and password is admin as specified in the docker-compose file. We can reset the default password for the first login.

We have already configured Prometheus data sources. > Connections > Data sources.

We can create our Dashboard or use them from the Grafana marketplace.

Go to dashboard > Import. Next, Add the Grafana dashboard ID (eg: 9964) to set it up.

For this story, we have used the following dashboard:

Grafana Alerts

In Grafana, we can also set up alerts based on specific thresholds for our metrics.

Alerts are crucial to maintaining health because we can be notified of resource usage, pipeline failure, and duration by setting an alert on the CI-CD instances.

Conclusion

Well done !!. This post explored using Prometheus and Grafana to monitor a Jenkins instance.

The complete source code is available on GitHub.

You can reach out to me and follow me on MediumTwitterGitHubLinkedln

Support me through GitHub Sponsors.

Thank you for Reading !! See you in the next story.

References

👉 Link to Medium blog

Related Posts