Skip to content

Lesson 9 of 13 · 11m

The Dashboard Is a Query, Not a File

Recording rules move aggregation to ingest time, exemplars link a datapoint to its trace, and federation is the old way up.

A Grafana panel is a PromQL query that re-executes on every refresh, and "thirty days of histogram_quantile over 200,000 series" is a query that reads a month of data to draw one line. When the dashboard takes four seconds a panel and your Prometheus CPU spikes every time a director opens the SLO board, the problem isn't the dashboard. It's that you're computing aggregates at read time that should have been computed at ingest time.

Recording rules: precompute the aggregate

A recording rule is a PromQL expression the server evaluates on an interval and stores back as a new series:

groups:
  - name: http_rules
    interval: 30s
    rules:
      - record: job_route:http_requests:rate5m
        expr: sum(rate(http_requests_total[5m])) by (job, route)
      - record: job_route:http_errors:ratio5m
        expr: >-
          sum(rate(http_requests_total{status=~"5.."}[5m])) by (job, route)
          / sum(rate(http_requests_total[5m])) by (job, route)

Now the dashboard queries job_route:http_errors:ratio5m — one series per job/route — instead of rescanning http_requests_total. The naming convention comes from the Google SRE book: level:metric:operations, so job_route:http_requests:rate5m reads as "per job and route, the 5-minute rate of http_requests." The cost is the eval loop plus bounded new series — you're paying ingestion cardinality once for an aggregate instead of read-time CPU forever. Alerts should evaluate the same recorded series so the thing that pages you is the thing the graph shows; a burn-rate alert on raw series and a dashboard on recorded series is how you get paged for a spike nobody can see on the board.

prometheus_engine_query_duration_seconds and --query.max-samples / --query.timeout exist so you can measure the read-side abuse directly; a query hitting max_samples is a panel someone needs to convert to a recording rule. One tradeoff to hold onto: a rule evaluated every 30 s can't reconstruct sub-30s spikes, so the recorded series' resolution is the eval interval — pick interval to match the granularity the dashboard actually needs, not tighter.

Exemplars: the metric that knows its trace

A Prometheus sample can carry an exemplar — the OpenMetrics spec's mechanism for attaching trace_id and span_id to a datapoint. The OTel SDK attaches exemplars automatically when there's an active span context at record time, Prometheus stores them alongside the sample, and Grafana renders them as dots on the graph that jump straight to the trace in Tempo or Jaeger.

This is the correlation mechanism the "metrics vs. logs vs. traces" diagram always gestures at without explaining. The p99 spike on the histogram panel gets clicked, and the exemplar takes you to an actual slow request's trace — root span to leaf — instead of to a grep for a timestamp. It costs a few bytes per sample and one config line; in Grafana it's the "exemplars" toggle on the query, and exemplar storage on the Prometheus side rides the exemplar-storage feature that has shipped since the 2.x era.

Federation is the old way up

Prometheus's /federate endpoint lets a higher-tier Prometheus scrape selected series from leaf servers — the original design for fleet roll-ups, still fine for a handful of instances. It doubles scrape load on the leaves and carries whatever series you select, so it's a poor fit past a certain scale; that's the gap Thanos, Mimir, and VictoriaMetrics filled with remote-read architectures. Know /federate exists; reach for it deliberately, not by default.

Practice

  • Any panel over more than a few days or a high-series metric reads a recording-rule series, not raw counters.
  • Alerts evaluate recorded series so graphs and pages agree.
  • Turn on exemplars; a metric that can't produce its own example trace is a metric you debug by guesswork.
  • Reserve /federate for small roll-up tiers; past that, remote read.

Dashboards are consumers of telemetry, not the compute layer for it. Precompute, exemplar, then render.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.