Observability Practices with Prometheus and Grafana – Monitoring a Python App

Introduction

In today’s fast-paced digital world, software systems are becoming increasingly complex, distributed, and dynamic. From microservices architectures to serverless deployments, applications now span across various platforms and environments. In such scenarios, traditional logging and debugging techniques fall short when it comes to diagnosing issues or understanding system behavior in real time.

This is where observability comes into play. Observability refers to the ability to measure the internal state of a system based on the data it produces such as logs, metrics, and traces. With the right observability practices, developers and DevOps teams can proactively detect problems, analyze performance bottlenecks, ensure service reliability, and even make smarter decisions backed by real-time insights.

In this tutorial, we’ll implement observability in a simple Python Flask application using Prometheus to collect metrics and Grafana to visualize them. You’ll learn how to monitor request counts and response times, visualize trends, and prepare your app for production-level insights all with minimal setup.

Whether you’re working on a personal project, startup prototype, or production system, integrating observability from the beginning will give you a significant advantage.

Tools Used

  • Language: Python

  • Framework: Flask

  • Metrics: Prometheus

  • Dashboards: Grafana

  • Library: prometheus_client

Step 1: Install Dependencies

Open your terminal and run:

pip install flask prometheus_client

Step 2: Create the app. py file

from flask import Flask
from prometheus_client import start_http_server, Summary, Counter
import time
import random

app = Flask(__name__)

# Custom metrics
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing each request')
REQUEST_COUNTER = Counter('http_requests_total', 'Total number of HTTP requests')

@app.route("/")
@REQUEST_TIME.time()
def hello():
    REQUEST_COUNTER.inc()
    delay = random.uniform(0.1, 0.9)
    time.sleep(delay)
    return f"Hello! Response time: {delay:.2f} seconds"

if __name__ == "__main__":
    # Start metrics server for Prometheus to scrape
    start_http_server(8000)
    app.run(host="0.0.0.0", port=5000)

This script:

  • Exposes metrics at http://localhost:8000/metrics

  • Simulates a web app with random delays

  • Tracks how many times the route / is accessed

  • Measures how long each request takes

Step 3: Configure Prometheus (prometheus.yml)

Create a file named prometheus.yml with the following content:

Editarglobal:
  scrape_interval: 5s

scrape_configs:
  - job_name: 'app_python'
    static_configs:
      - targets: ['localhost:8000']

This tells Prometheus to scrape metrics from localhost:8000 every 5 seconds.

Step 4: Run Prometheus

  1. Download Prometheus: https://prometheus.io/download/

  2. Place prometheus.yml in the same directory as the executable

  3. Run Prometheus:

./prometheus --config.file=prometheus.yml
  1. Open in your browser:
    http://localhost:9090

Step 5: Install and Configure Grafana

  1. Download Grafana: https://grafana.com/grafana/download

  2. Run it:

./bin/grafana-server
  1. Access Grafana:
    http://localhost:3000
    Username: admin | Password: admin

  2. Go to Configuration → Data Sources → Add data source

  3. Select Prometheus

  4. In the URL field, enter:

http://localhost:9090
  1. Click Save & Test

Step 6: Create a Dashboard in Grafana

  1. Go to Dashboards → New → Add Panel

  2. In the query editor, type:

http_requests_total

This shows the total number of requests.

  1. Add another panel with:
rate(request_processing_seconds_sum[1m]) / rate(request_processing_seconds_count[1m])

This calculates the average response time per minute.

  1. Optionally, add a histogram panel with:
request_processing_seconds

Final Result

With everything up and running:

  • Visit http://localhost:5000 multiple times.

  • Prometheus will collect the metrics in real-time.

  • Grafana will display real-time visualizations of your app’s behavior.

GITHUB CODE:

andresebast161616/PrometheusGrafana: Observability Practices with Prometheus and Grafana – Monitoring a Python App

Conclusion

Thanks to Prometheus and Grafana, you can easily implement observability in your projects. These tools help you understand your application's behavior, catch issues early, and optimize performance in real time

0
Subscribe to my newsletter

Read articles from Andree Sebastian FLORES MELENDEZ directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Andree Sebastian FLORES MELENDEZ
Andree Sebastian FLORES MELENDEZ