DORA METRICS EXPORTER

Dimeji Ojewunmi - Aug 8 - - Dev Community

Introduction to Dora Metrics

DORA Metrics are key performance indicators used to measure the effectiveness of a software delivery process. They help teams understand the efficiency of their DevOps practices by tracking key aspects of the CI/CD pipeline.

Below are the outlined reason stipulate why we need to leverage the need of Dora Metrics in an organization

  • Deployment Frequency: Measures how often deployments occur. High frequency indicates a fast, iterative development process.

  • Lead Time for Changes: The time it takes from committing code to deploying it in production. Short lead times suggest an efficient pipeline.

  • Change Failure Rate: The percentage of deployments that result in a failure in production. Lower rates indicate more stable and reliable releases.

  • Time to Restore Service: The time it takes to recover from a failure in production. Faster recovery times imply a more resilient system.

Installation Setup

The following git command below are directories and files to be opened in the cause of our Dora metric exporter installation

mkdir dora_metrics_exporter
cd dora_metrics_exporter
python3 -m venv venv
touch requirements.txt .env exporter.py
source venv/bin/activate  # On Windows use `venv\Scripts\activate`
touch requirements.txt .env exporter.py
Enter fullscreen mode Exit fullscreen mode

requirements.txtcontext below

flask
prometheus_client
requests
python-dotenv
Enter fullscreen mode Exit fullscreen mode

Run the script below right after the opening of 'requirements.txt'

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

.env context below

GITHUB_TOKEN=your_github_token
REPO_OWNER=holadmex (GitHub Username)
REPO_NAME=hng_boilerplate_expressjs (Project Repository Name)
Enter fullscreen mode Exit fullscreen mode

exporter.py context below

# exporter.py
from flask import Flask
from prometheus_client import start_http_server, Gauge
import requests
import time
import os
import logging
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Configure logging
logging.basicConfig(level=logging.INFO)

app = Flask(__name__)

# Metrics
DEPLOYMENT_FREQUENCY = Gauge('deployment_frequency', 'Frequency of deployments')
LEAD_TIME_FOR_CHANGES = Gauge('lead_time_for_changes', 'Lead time for changes')
CHANGE_FAILURE_RATE = Gauge('change_failure_rate', 'Rate of change failures')
TIME_TO_RESTORE_SERVICE = Gauge('time_to_restore_service', 'Time to restore service')

# GitHub Configuration
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
REPO_OWNER = os.getenv('REPO_OWNER')
REPO_NAME = os.getenv('REPO_NAME')

HEADERS = {
    'Authorization': f'token {GITHUB_TOKEN}'
}

# Function to fetch data from GitHub API
def fetch_github_data():
    logging.info("Fetching GitHub data")
    url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/stats/code_frequency'
    response = requests.get(url, headers=HEADERS)
    response.raise_for_status()
    logging.info("Data fetched successfully")
    return response.json()

def calculate_metrics():
    logging.info("Calculating metrics")
    try:
        data = fetch_github_data()
        # Perform calculations for DORA metrics here

        # Dummy values for demonstration
        DEPLOYMENT_FREQUENCY.set(10)
        LEAD_TIME_FOR_CHANGES.set(5)
        CHANGE_FAILURE_RATE.set(0.1)
        TIME_TO_RESTORE_SERVICE.set(2)
        logging.info("Metrics calculated and set")
    except Exception as e:
        logging.error(f"Error calculating metrics: {e}")

@app.route('/metrics')
def metrics():
    return ""

if __name__ == '__main__':
    # Start Prometheus metrics server
    logging.info("Starting Prometheus metrics server")
    start_http_server(8001)
    logging.info("Prometheus metrics server started on port 8001")
    # Schedule metric calculations
    while True:
        calculate_metrics()
        time.sleep(300)  # Fetch data every 5 minutes
Enter fullscreen mode Exit fullscreen mode

The code below execute the Dora Metric Exporter installation

python exporter.py
OR
nohup python exporter.py & (This cmd runs your application in the background without your terminal being held hostage)
Enter fullscreen mode Exit fullscreen mode

NB:

On completion of the installation, Dora metrics job needs to be configured or added to Prometheus config file in order to export the generated metrics from Dora exporter to Prometheus which will be visualized on Grafana dashboard through Grafana data source.

scrape_configs:
  - job_name: 'dora_metrics'
    static_configs:
      - targets: ['localhost:8001']
Enter fullscreen mode Exit fullscreen mode

DORA EXPORTER METRICS QUERY TO BE VISUALISED ON GRAFANA DASHBOARD

deployment_frequency
lead_time_for_changes
change_failure_rate
time_to_restore_service
Enter fullscreen mode Exit fullscreen mode

Thank you for reading to the end,
Happy Hands On Practice!.

. . .
Terabox Video Player