Metrics with Prometheus & Grafana
Set up Prometheus for collecting cluster and application metrics and visualize them with Grafana.
Metrics with Prometheus & Grafana is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Power of Metrics
Metrics are numerical measurements that describe the state and performance of your applications and infrastructure. Think of them as vital signs for your software!
They provide valuable insights into:
- Performance: How fast is your app responding?
- Resource Usage: Is your server running out of memory?
- Availability: Is your service up and running for users?
By tracking metrics, you can understand trends, detect anomalies, and troubleshoot issues proactively.
Meet Prometheus
Prometheus is an open-source monitoring system designed for reliability and scalability. It's a key tool in the cloud-native observability stack.
Its primary job is to:
- Scrape: Collect metrics from configured targets (like your applications).
- Store: Save these metrics in a time-series database.
- Query: Allow you to ask questions about the collected data.
Prometheus helps you get a clear picture of what's happening inside your Kubernetes cluster.
Prometheus's "Scraping" Model
Prometheus uses a "pull" model to collect metrics. Instead of applications pushing data to Prometheus, Prometheus actively "scrapes" or pulls metrics from them.
Here's how it generally works:
- Your application exposes metrics on a specific HTTP endpoint (often
/metrics). - Prometheus is configured with a list of targets (like your application's IP and port).
- At regular intervals, Prometheus sends an HTTP request to each target's
/metricsendpoint. - The target responds with its current metrics, which Prometheus then stores.
Metrics: Key-Value Pairs
Prometheus stores data as time series: streams of timestamped values. Each time series is uniquely identified by a metric name and a set of key-value pairs called labels.
Example: http_requests_total{method="GET", path="/api"}
Prometheus has core metric types:
- Counter: A cumulative metric that only increases (e.g., total requests).
- Gauge: A value that can go up or down (e.g., current CPU usage).
- Histogram/Summary: For sampling observations and counting them in configurable buckets (e.g., request durations).
Making Apps "Speak" Metrics
For Prometheus to scrape metrics, your application needs to expose them in a format Prometheus understands. This is usually done via an HTTP endpoint.
Many programming languages have client libraries that make it easy to instrument your code:
- Increment counters on events.
- Set gauges for current states.
- Observe durations of operations.
Let's look at a super simple example of an app exposing a basic counter metric.
Python App Exposing Metrics
Here's a tiny Python Flask app that exposes a simple counter metric. It simulates counting page views.
Run this code and then try accessing /metrics in your browser/terminal (e.g., after running, visit http://localhost:5000/metrics).
from flask import Flask
from prometheus_client import Counter, generate_latest
import time
app = Flask(__name__)
c = Counter('my_app_requests_total', 'Total requests to my app')
@app.route('/')
def hello():
c.inc() # Increment the counter
return "Hello, CoddyKit!"
@app.route('/metrics')
def metrics():
return generate_latest().decode('utf-8')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)Prometheus in Your Cluster
In Kubernetes, deploying Prometheus efficiently often involves the Prometheus Operator. This operator helps manage Prometheus instances and configurations.
Key Kubernetes resources for Prometheus:
- ServiceMonitor: Tells Prometheus *where* to find metrics endpoints (e.g., which Services and Pods to scrape).
- PodMonitor: Similar to ServiceMonitor but targets Pods directly.
These resources allow Prometheus to automatically discover and scrape metrics from your deployed applications.
Grafana: The Dashboard Maestro
While Prometheus collects and stores metrics, Grafana is your go-to tool for visualizing them. It's an open-source analytics and interactive visualization web application.
Grafana allows you to:
- Create beautiful and informative dashboards.
- Query multiple data sources (like Prometheus).
- Set up alerts based on metric thresholds.
It transforms raw metric data into actionable insights.
Grafana & Prometheus: A Powerful Duo
Grafana doesn't store metric data itself; it queries data from external data sources. To visualize your Kubernetes metrics, you'll configure Prometheus as a data source in Grafana.
Steps:
- In Grafana, add a new data source.
- Select "Prometheus" as the type.
- Provide the URL of your Prometheus server.
- Start building dashboards using Prometheus's query language, PromQL!
This pairing gives you full control over monitoring your cluster.
Test Your Knowledge!
Time to check your understanding of Prometheus and Grafana!
Recap: Metrics & Visualization
Great job! In this lesson, you learned about:
- The importance of metrics for observability.
- Prometheus as a powerful system for scraping and storing time-series metrics.
- How applications expose metrics for Prometheus to collect.
- Grafana for creating insightful dashboards and visualizing your metric data.
Understanding these tools is crucial for keeping your Kubernetes applications healthy and performant. Keep exploring how to instrument your own applications!
Frequently asked questions
Is the “Metrics with Prometheus & Grafana” lesson free?
Yes — the full text of “Metrics with Prometheus & Grafana” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Metrics with Prometheus & Grafana”?
Set up Prometheus for collecting cluster and application metrics and visualize them with Grafana. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Metrics with Prometheus & Grafana” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Logging with kubectl logs
- Metrics with Prometheus & Grafana
- Health Checks: Liveness & Readiness Probes
- Distributed Tracing and Events