Step 5: Visualization with Grafana
Learn how to create beautiful dashboards to understand your app
📊 What is Grafana?
Grafana is a visualization platform that creates dashboards from your metrics and logs:
| Feature | What It Does |
|---|---|
| Dashboards | Visual panels showing metrics |
| Explore | Ad-hoc queries for debugging |
| Alerting | Visual alert rules |
| Datasources | Connect to Prometheus, Loki |
🌐 Accessing Grafana
- Open http://localhost:3000
- Login with:
- Username: admin
- Password: admin
🔍 Using Explore
Explore is great for ad-hoc queries:
Query Prometheus (Metrics)
- Click "Explore" in sidebar
- Select "Prometheus" datasource
- Enter a PromQL query:
# Request rate
rate(django_http_requests_total[5m])
# Latency
histogram_quantile(0.95, rate(django_http_requests_latency_seconds_bucket[5m]))
# CPU usage
100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
- Click "Run Query"
Query Loki (Logs)
- Click "Explore" in sidebar
- Select "Loki" datasource
- Enter a LogQL query:
# All Django logs
{app="django"}
# Error logs
{app="django", level="ERROR"}
# Search logs
{app="django"} |= "POST"
- Click "Run Query"
📈 Creating Your First Dashboard
Step 1: Create Dashboard
- Click "Dashboards" in sidebar
- Click "New Dashboard"
- Click "Add visualization"
- Select "Prometheus" datasource
Step 2: Add Request Rate Panel
- Enter query:
- Click "Run Query"
- In panel options:
- Title: "Request Rate"
- Unit: "requests/sec"
- Click "Apply"
Step 3: Add Latency Panel
- Click "Add panel"
- Enter query:
- Set title: "P95 Latency"
- Set unit: "seconds"
- Click "Apply"
Step 4: Add Error Rate Panel
- Click "Add panel"
- Enter query:
- Set title: "5xx Error Rate"
- Set unit: "errors/sec"
- Click "Apply"
Step 5: Save Dashboard
- Click "Save dashboard" (disk icon)
- Name: "My Django Dashboard"
- Click "Save"
🎨 Dashboard Best Practices
Layout Tips
┌─────────────────────────────────────────────────────────────────┐
│ Row 1: Key Metrics (Request Rate, Latency, Errors) │
├─────────────────────────────────────────────────────────────────┤
│ Row 2: Resource Metrics (CPU, Memory, Disk) │
├─────────────────────────────────────────────────────────────────┤
│ Row 3: Database Metrics (Query Time, Connections) │
├─────────────────────────────────────────────────────────────────┤
│ Row 4: Logs (Recent errors, Recent requests) │
└─────────────────────────────────────────────────────────────────┘
Panel Types
| Type | Best For |
|---|---|
| Graph | Time-series data (rates, latencies) |
| Stat | Single value (current CPU, total requests) |
| Table | Detailed data (top endpoints) |
| Logs | Log entries |
🔬 Hands-On Exercise
Create a Complete Dashboard
- Request Rate -
sum(rate(django_http_requests_total[5m])) - P95 Latency -
histogram_quantile(0.95, sum(rate(django_http_requests_latency_seconds_bucket[5m])) by (le)) - Error Rate -
sum(rate(django_http_responses_total_by_status_total{status=~"5.."}[5m])) - CPU Usage -
100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) - Memory Usage -
(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 - Recent Logs -
{app="django", level="ERROR"}
🔧 Troubleshooting
No data in panels
Check datasource:
- Go to Configuration → Data Sources
- Verify Prometheus URL:
http://obs-prometheus:9090 - Click "Save & Test"
Check if metrics exist:
Loki datasource not working
Verify Loki datasource:
- Go to Configuration → Data Sources
- Click Loki datasource
- URL should be:
http://obs-loki:3100 - Click "Save & Test"
📚 What You Learned
In this step, you learned:
- ✅ How to access and login to Grafana
- ✅ How to use Explore for ad-hoc queries
- ✅ How to create dashboards
- ✅ How to add panels for metrics and logs
- ✅ Dashboard best practices
🎉 Tutorial Complete!
Congratulations! You've completed the Django Observability tutorial. You now know how to:
- ✅ Set up the observability stack
- ✅ Monitor metrics with Prometheus
- ✅ Aggregate logs with Loki
- ✅ Set up alerts with Alertmanager
- ✅ Visualize data with Grafana
🎓 Continue Learning
Explore the detailed module documentation:
| Module | What You'll Learn |
|---|---|
| Django App | Deep dive into metrics & logging |
| Prometheus | Advanced PromQL queries |
| Grafana | Advanced dashboards |
| Loki | Advanced LogQL queries |
| Alertmanager | Advanced alert routing |
| MCP Server | AI integration |
Thank you for following the tutorial! 🎉