Grafana dashboards
Build a dashboard for latency, errors and saturation.
By Invicly Tech, , in Full DevOps with Python. About 10 minutes to read.
In short
Grafana turns Prometheus queries into dashboards. A good service dashboard answers four questions at a glance: how much traffic, how many errors, how slow, and how full.
Explained simply
Grafana is a web app for charts. It does not store metrics itself. Instead it asks other systems, called data sources, for data and draws the answers. With Prometheus as the data source, every chart is a PromQL query.
A dashboard is a page of panels. Each panel shows one thing: a line chart of requests per second, a single number for the error percentage, or a gauge for memory use. You arrange panels in rows so the most important ones are at the top.
Good service dashboards answer four questions, often called the golden signals: how much traffic, how many errors, how slow (latency) and how full (saturation). If you can see those four in five seconds, the dashboard is doing its job.
Dashboards are also code. Grafana saves each one as a JSON document. You can export that JSON, keep it in Git, review changes in pull requests, and load it automatically into new Grafana servers.
Press Play on a diagram to walk through it one step at a time.
- Traffic
- Requests per second
- Split by route
- Errors
- 5xx rate
- Error percentage
- Latency
- p50
- p95
- p99
- Saturation
- CPU vs limit
- Memory vs limit
- Pod restarts
- 1Variables bar
Data source, namespace, route
- 2Stat row
Requests/s, error %, p99, Pods up
- 3Latency row
p50, p95, p99 per route over time
- 4Errors row
5xx rate by route and status
- 5Saturation row
CPU and memory vs limit, restarts
In depth
Connecting Grafana to Prometheus
Grafana listens on port 3000; a fresh install logs in as admin with password admin and asks you to change it. Under Connections, then Data sources, add Prometheus and enter its URL as seen from the Grafana server. That detail trips people up: in Docker Compose it is http://prometheus:9090 (the service name), not localhost, because localhost inside the Grafana container is Grafana itself. Save and test should report success.
For repeatable setups, skip the clicking and use provisioning. Grafana reads YAML files from /etc/grafana/provisioning/datasources/ at startup, so the data source exists every time the container starts. The same mechanism under provisioning/dashboards/ loads dashboard JSON files from a folder. In Kubernetes, the kube-prometheus-stack Helm chart does both for you and ships ready-made cluster dashboards.
- Browser to Grafana: Load dashboard, time range last 6h1Load dashboard, time range last 6h
- 2Replace $namespace and $__rate_intervalGrafana to Grafana: Replace $namespace and $__rate_interval
- Grafana to Prometheus: query_range with start, end and step3query_range with start, end and step
- Prometheus to Grafana: Matrix of time series4Matrix of time series
- Grafana to Browser: Data drawn as a time series panel5Data drawn as a time series panel
Latency percentiles and error rate, done right
Latency panels should show percentiles from a histogram, usually p50, p95 and p99 as three queries on one time series panel. The query pattern is histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[$__rate_interval]))). Keeping le in the sum is required, because histogram_quantile needs the buckets. Add route to the by clause to split by endpoint.
Grafana's $__rate_interval variable picks a rate() window that is safe for the current zoom level and scrape interval, so you do not need to hard-code [5m]. Set the panel unit to seconds and Grafana prints 250 ms instead of 0.25.
Show errors as a percentage of traffic, not a raw count: 50 errors per second is a disaster at 100 requests per second and noise at 100,000. Use a Stat panel with thresholds (green, then yellow at 1%, red at 5%) so a glance is enough. Remember that histogram percentiles are estimates within a bucket, so choose buckets around your target latency.
- Unit seconds (s) for latency, percent (0-100) for error ratio, requests/sec for traffic
- Thresholds turn a number into a traffic light
- Legend {{route}} shows the label value instead of the full series name
Saturation panels and dashboard variables
Saturation means how close a resource is to its limit. In Kubernetes, CPU usage comes from cAdvisor as container_cpu_usage_seconds_total (a counter, so use rate()), memory as container_memory_working_set_bytes, and limits from kube-state-metrics as kube_pod_container_resource_limits. Dividing usage by the limit gives a 0 to 1 ratio that means the same thing for every Pod. Memory near the limit predicts an OOM kill; CPU near the limit causes throttling and higher latency. Pod restarts (kube_pod_container_status_restarts_total) and queue length are other good saturation signals.
Variables make one dashboard work everywhere. A query variable such as label_values(kube_pod_info, namespace) fills a dropdown with every namespace; panels then filter with namespace="$namespace". Variables can depend on each other ($pod lists only Pods in the chosen $namespace) and can allow multiple values, in which case you match with =~.
- $datasource
- Pick Prometheus dev or prod
- $namespace
- label_values(kube_pod_info, namespace)
- $pod
- Filtered by the chosen namespace
- Multi-value, matched with =~
- Built-in
- $__rate_interval
- $__range
Dashboards as code: JSON export and provisioning
Every dashboard is a JSON object with a uid (a stable ID used in its URL), a title, a panels array, a templating section for variables and a version number. From the dashboard's Export option you can save that JSON to a file. Choosing to export for sharing externally replaces your data source IDs with an input placeholder, so someone else can import it and pick their own Prometheus.
To keep dashboards in Git, store the JSON files in the repo and load them through a dashboard provisioning file, or through the HTTP API (POST /api/dashboards/db) from CI. Provisioned dashboards are rebuilt from the files on every start, which means edits in the UI are lost unless you export them back to the repo. In Kubernetes, kube-prometheus-stack can load dashboards from ConfigMaps that carry a grafana_dashboard label. Some teams generate the JSON from code with tools like Grafonnet (Jsonnet) to avoid hand-editing large files.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Connect Grafana to Prometheus~45m
- 2Chart latency percentiles and error rate~45m
- 3Add saturation panels and variables~45m
- 4Export the dashboard as JSON~45m
Key points
- Add Prometheus as a data source, then build panels from PromQL queries.
- Show latency as percentiles; averages hide the slow requests users notice.
- Dashboard variables like $namespace let one dashboard serve every environment.
- Export dashboards as JSON and keep them in Git with the rest of the code.
Key terms
Tap a card to flip it. 0/5 known
Examples
# p95 latency per route
histogram_quantile(0.95,
sum by (le, route) (rate(http_request_duration_seconds_bucket{namespace="$namespace"}[5m])))
# error percentage
100 * sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))Two core panel queries using a $namespace variable.
Provision the Prometheus data source with Docker Compose
Step 1: create grafana/provisioning/datasources/prometheus.yml. Step 2: point url at the Prometheus service name, not localhost. Step 3: mount the folder into the Grafana container at /etc/grafana/provisioning. Step 4: docker compose up -d, open http://localhost:3000, and the data source is already there and marked as default. Step 5: in Explore, run up to confirm data flows.
# grafana/provisioning/datasources/prometheus.yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
# docker-compose.yml (part)
# grafana:
# image: grafana/grafana
# ports: ["3000:3000"]
# volumes: ["./grafana/provisioning:/etc/grafana/provisioning"]Build the four golden-signal queries for one service
Step 1: create a namespace variable from label_values(kube_pod_info, namespace). Step 2: add a Traffic panel with requests per second by route. Step 3: add an Errors stat panel showing the 5xx percentage with thresholds at 1 and 5. Step 4: add a Latency panel with the p95 query (repeat with 0.5 and 0.99). Step 5: add a Saturation panel with memory working set divided by the memory limit, unit percent (0.0-1.0). Save, then export the JSON into the repo.
# Traffic
sum by (route) (rate(http_requests_total{namespace="$namespace"}[$__rate_interval]))
# Errors (%)
100 * sum(rate(http_requests_total{namespace="$namespace", status=~"5.."}[$__rate_interval]))
/ sum(rate(http_requests_total{namespace="$namespace"}[$__rate_interval]))
# Latency p95
histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket{namespace="$namespace"}[$__rate_interval])))
# Saturation: memory vs limit, per Pod
sum by (pod) (container_memory_working_set_bytes{namespace="$namespace", container!=""})
/ sum by (pod) (kube_pod_container_resource_limits{namespace="$namespace", resource="memory"})Quick reference
- Grafana runs on port 3000; first login is admin / admin
- From inside Compose, the Prometheus URL is http://prometheus:9090, not localhost
- Use $__rate_interval instead of a fixed [5m] inside rate() in panels
- Keep le in sum by (le) when using histogram_quantile
- label_values(kube_pod_info, namespace) fills a namespace dropdown
- Provisioning folders: /etc/grafana/provisioning/datasources and /dashboards
- Put the golden signals at the top: traffic, errors, latency, saturation
Common mistakes
- Dashboards with dozens of panels that nobody can read during an incident.
- Plotting average latency, which hides a slow tail.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Start Grafana and Prometheus with Docker Compose, add Prometheus as a data source in the UI, and graph up in Explore.
- Exercise 2medium
Add a time series panel that shows p50, p95 and p99 latency of your API on one chart with readable legends.
- Exercise 3hard
Write a Python script that downloads a dashboard's JSON by uid through the Grafana HTTP API and saves it as dashboards/UID.json (named after the uid) for Git.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Grafana free?
- Grafana OSS is open source under the AGPLv3 license and free to self-host. Grafana Labs also sells Grafana Cloud and an Enterprise edition with extra features.
- Should I alert from Grafana or from Prometheus?
- Both work. Prometheus rules kept in Git next to the app are simple and reliable; Grafana alerting is handy when you want alerts across several data sources, such as metrics and logs together.
- Why does my latency panel show No data?
- Usually the metric name or labels do not match, the le label was dropped from the sum, or the rate() window is shorter than two scrape intervals. Run the query in Explore and remove parts until data appears.