Continuous Integration (CI) is a vital part of the software development process that helps teams detect and solve issues early in the development cycle. Drone is a popular open-source CI/CD tool that automates building, testing, and deploying applications. In this article, we will explore how to monitor and visualise Drone CI metrics using Prometheus and Grafana.

To get started, you'll need Drone Server. If setup and installation are yet to be done, you can refer to my blogtorial.

About Prometheus and Grafana

Prometheus is a popular open-source monitoring system that collects and stores metrics from various sources. It is designed to be highly scalable and has a powerful query language for data analysis. Grafana is a data visualisation tool that helps visualise and analyse metrics stored in Prometheus. It provides a rich set of visualisation options, such as graphs, dashboards, and alerts.

Setting up Prometheus & Grafana

Using Docker, we will set up Prometheus and Grafana. For that, we'll use the following docker-compose file.

Create a docker-compose.yml file

touch docker-compose.yml

Copy the contents below into the created file.

version: '3.9'
services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    ports:
      - '9090:9090'
    networks:
      - prometheus-grafana-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
  grafana:
    image: grafana/grafana
    ports:
      - '3000:3000'
    volumes:
      - grafana-data:/var/lib/grafana
      - ${PWD}/drone-dashboard.json:/etc/grafana/provisioning/dashboards/drone-dashboard.json
    networks:
      - prometheus-grafana-network
      
volumes:
 prometheus-data:
   driver: bridge
 grafana-data:
   driver: bridge
   
networks:
  prometheus-grafana-network:
    external:false
Docker compose file content 

Create a prometheus.yml file

touch prometheus.yml

We'll create a Prometheus configuration file, which will be used by the Prometheus container to scrape data from the targets we specify. The configuration file contains the target to scrape, the scrape interval, and other settings.

Copy the contents below into the prometheus.yml file

scrape_configs:
  - job_name: 'drone'
    metrics_path: '/metrics'
    static_configs:
      - targets: ['host.docker.internal:8080']
Prometheus scraping config
✍️
This article assumes that you have a drone CI/CD Server hosted and exposed at 127.0.0.1:8080. If the server is hosted at any other URL/PORT, please change the URL in the Prometheus config accordingly.

Create a Grafana drone-dashboard.json file

touch drone-dashboard.json

Copy the following contents into the drone-dashboard.json file.

{
  "title": "Drone Build Metrics",
  "tags": [],
  "style": "dark",
  "timezone": "browser",
  "editable": true,
  "rows": [
    {
      "title": "Build Metrics",
      "height": "300px",
      "panels": [
        {
          "title": "Build Status",
          "type": "graph",
          "datasource": "Prometheus",
          "targets": [
            {
              "expr": "drone_build_status",
              "legendFormat": "{{build_number}}",
              "refId": "A"
            }
          ],
          "xaxis": {
            "show": true
          },
          "yaxes": [
            {
              "format": "short",
              "label": null,
              "logBase": 1,
              "max": null,
              "min": null,
              "show": true
            },
            {
              "format": "short",
              "label": null,
              "logBase": 1,
              "max": null,
              "min": null,
              "show": true
            }
          ],
          "bars": false,
          "fill": 1,
          "lines": true,
          "linewidth": 1,
          "points": false,
          "stack": false,
          "steppedLine": false,
          "tooltip": {
            "shared": true,
            "sort": 0,
            "value_type": "individual"
          },
          "aliasColors": {}
        }
      ]
    }
  ],
  "nav": [
    {
      "type": "timepicker"
    },
    {
      "type": "refresh"
    },
    {
      "type": "dropdown",
      "text": "Dashboard",
      "icon": "fa fa-folder-open",
      "canSave": true,
      "canEdit": true,
      "canStar": true,
      "items": []
    },
    {
      "type": "dropdown",
      "text": "Panel",
      "icon": "fa fa-cube",
      "canSave": true,
      "canEdit": true,
      "canStar": true,
      "items": []
    },
    {
      "type": "dropdown",
      "text": "Help",
      "icon": "fa fa-question",
      "canSave": false,
      "canEdit": false,
      "canStar": false,
      "items": [
        {
          "text": "Grafana Documentation",
          "href": "http://docs.grafana.org"
        }
      ]
    }
  ],
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "templating": {
    "list": []
  },
  "annotations": {
    "list": []
  },
  "schemaVersion": 14,
  "version": 0
}
Grafana Drone dashboard JSON

This JSON data defines a dashboard with a single panel that shows the build status over time. The data source for the panel is set to Prometheus, and the drone_build_status metric is used to generate the graph. The legend format is set to show the build number, and the graph is displayed as a line chart with the fill option enabled.

Accessing Prometheus and Grafana

Finally, we can start Prometheus and Grafana by running the command below. This will start Prometheus and Grafana with their respective configurations.

docker compose up -d

You can now access Prometheus at localhost:9090 and Grafana at localhost:3000 .

Frequently Asked Questions

Why use Prometheus and Grafana for monitoring Drone CI/CD build metrics?

Prometheus and Grafana are well-suited for monitoring Drone CI/CD build metrics for several reasons:

  • Prometheus provides a highly scalable and flexible way to collect and store time-series data, making it easy to monitor a large number of builds in real time.
  • Grafana provides a wide range of visualisation tools, making it easy to create custom dashboards that display the specific metrics you are interested in.
  • Together, Prometheus and Grafana provide a powerful and highly customisable monitoring solution that can be tailored to your organisation's specific needs.

What metrics can be monitored using Prometheus and Grafana in Drone CI/CD?

Drone CI/CD provides a variety of metrics that can be monitored using Prometheus and Grafana, including:

  • Build status: Whether a build is successful, failed, or still running
  • Build duration: How long each build takes to complete
  • Build triggers: What triggers each build, such as a commit or a pull request
  • Build steps: How many steps each build has, and how long each step takes to complete
  • Build artefacts: What artefacts are generated by each build, and how large they are

In this article,

we explored how to monitor and visualise Drone CI metrics using Prometheus and Grafana. Together, they provide a powerful toolset for monitoring and analysing Drone CI metrics.

Subscribe and comment below if you have any queries! I try to regularly update my articles to ensure legibility.