From expensive queries to recorded series #
Context. Your dashboard recomputes the same aggregates on every refresh. You will record them in dedicated series, using Prometheus's own metrics, with no other dependency.
Prerequisites: Docker Compose, curl and jq · Estimated time: 40 min
1. Start Prometheus
Why? Prometheus scrapes itself, so you immediately get real HTTP metrics.
Writedocker-compose.ymlandprometheus.yml. Enable hot reload and reference arules.ymlfile.2. Record a first aggregate
Why? Staged aggregation avoids re-reading raw series at every level.
Createrules.ymlwith the rate perjobandhandler, then the rate perjobreusing the previous rule. Also create an emptytests.yml(touch tests.yml), check, start and query the new series after a minute.3. Add a robust error ratio and a percentile
Why? Without care, the ratio stays empty until an error exists.
Add the5xxerror rate, the error ratio (which must be 0 with no errors) and the 95th percentile of the duration. Reload and check the health of the rules.4. Write the unit tests
Why? A wrong rule gives a plausible but wrong dashboard: only a test reveals it.
Intests.yml, cover two cases: 10% errors (ratio 0.1) and no errors (ratio 0). Run the tests, then remove theor ... * 0part of the ratio, see the second test fail, and put it back.5. Measure the gain
Why? An optimisation is measured.
Compare the samples read by the original query and by the recorded series, then find the evaluation time of your rules withprometheus_rule_group_last_duration_seconds. You should see a single sample for the recorded series and several hundred for the original query (the value depends on your instance).
Difficulty level : ●●●○○ (3/5)
My progress
Sign in to track your progress and take notes. Sign in
Course Glossary
Alerting
The process of notifying teams (via email, Slack, Teams) when a specific event, threshold breach, or anomaly is detected.
Architecture
The overall structural design of an IT system, defining its various components, their relationships, and the principles guiding its evolution.
OpenTelemetry
An open source framework providing standards, APIs, and SDKs to collect and export observability data (traces, metrics, logs) in an agnostic way.
Recommended Articles
Prometheus : alerter avec Alertmanager (règles, routage, silences)
Mettre en place l'alerting Prometheus : règles d'alerte, Alertmanager, routage, regroupement, inh...
Prometheus sur Kubernetes et l'interface web de Prometheus
Déployer Prometheus sur Kubernetes avec kube-prometheus-stack (Prometheus Operator, ServiceMonito...
Prometheus : visualiser ses métriques (UI, Grafana et dashboards)
Visualiser les données Prometheus : interface web, graphes PromQL, connexion à Grafana, dashboard...
Les types de métriques dans Prometheus
Découvrez en détail les quatre types de métriques supportés par Prometheus (Counter, Gauge, Histo...
Silences, inhibitions et intervalles de temps dans Alertmanager
Réduire le bruit des alertes : silences ponctuels avec amtool, règles d'inhibition (une alerte cr...
Monitoring de site web : disponibilité, surveillance HTTPS, certificats et performances
Mettre en place le monitoring d'un site web : surveillance de la disponibilité et du HTTPS, expir...
Prometheus dans le monde Kubernetes (Operator, ServiceMonitor, kube-prometheus-stack)
Superviser Kubernetes avec Prometheus : service discovery, kube-prometheus-stack, Prometheus Oper...
Observabilité sur Azure : Azure Monitor, Log Analytics, Application Insights, Prometheus et Grafana managés
Panorama complet de l'observabilité sur Azure : Azure Monitor (métriques, journaux et Log Analyti...
Kibana avancé : data views, Lens, dashboards, alerting et reporting
Exploiter Kibana en profondeur : data views et champs runtime, Discover, Lens et formules, dashbo...