Grafana and the LGTM Stack
What Grafana is (and is not)
Grafana is an open-source web application for querying, visualizing and alerting on data from many sources. It is licensed AGPLv3 (relicensed from Apache 2.0 in April 2021, together with Loki and Tempo). Its architecture has one consequence that matters for design interviews: Grafana holds dashboard definitions, users, folders and alert rules in its own small database (SQLite or Postgres/MySQL), but none of the metrics, logs or traces. A panel is a stored query. Opening a dashboard sends those queries to the data sources and draws what comes back.
- Data source: a configured plugin that knows how to talk to one backend and what its query language looks like: Prometheus/Mimir (PromQL), Loki (LogQL), Tempo (TraceQL), Pyroscope, Elasticsearch/OpenSearch, CloudWatch, PostgreSQL/MySQL (SQL), InfluxDB, ClickHouse and many more. A single dashboard can mix sources; the Mixed data source allows it per panel.
- Dashboard: a JSON document: a list of panels, each with a visualization type, one or more queries, a layout position, plus dashboard-level variables, time range, refresh interval and annotations.
- Explore: an ad-hoc query workbench (no dashboard needed), with split view to compare two queries or a metric next to its logs. It is where investigations start; dashboards are where answers you already know live.
Panels and visualizations
| Visualization | Use it for | Note |
|---|---|---|
| Time series | Rates, gauges and counters-as-rates over time | Default; set the unit and a sane min/max or axes lie |
| Stat / Gauge | One current number: error rate, SLO budget left | Use thresholds for colour; sparkline optional |
| Heatmap | Latency distributions from histogram buckets | Shows modes and tails that one p99 line hides (see PromQL; native histograms, stable in Prometheus 3.8+, make heatmaps cheaper) |
| Table | "Top N by …", inventories | Instant query + transformations; avoid as a wall of numbers |
| Logs | Log lines alongside a graph | Backed by Loki/Elasticsearch; limit lines |
| Traces | Waterfall of one trace from Tempo/Jaeger | Usually opened by a link, not browsed (see the Distributed Tracing guide) |
| Node graph / Service map | Service dependency graph with rates and errors | Tempo can derive it from spans |
| Flame graph | CPU/memory profile from Pyroscope | Where time or bytes went in code |
Variables, intervals and the dashboard query
A template variable is a placeholder in queries, rendered as a dropdown at the top of the dashboard. One dashboard then serves every cluster, namespace and service instead of one copy each.
- Query variable: populated from a data source, e.g.
label_values(up, cluster)for$cluster. - Chained variables: a later variable depends on an earlier one:
$namespace=label_values(kube_pod_info{cluster="$cluster"}, namespace), then$appfiltered by both. Multi-value variables expand to a regex (route=~"$route"becomes(/cart|/checkout)), so always use=~with them. - Constant, interval, custom, text box, ad hoc filters: the other kinds; ad hoc filters add label matchers to every query of a data source.
Grafana also injects built-in variables that depend on the panel and time range:
| Variable | Value | Use |
|---|---|---|
$__interval | Time range ÷ the panel's max data points (≈ pixel width), rounded; floored by Min interval | A resolution hint: how far apart should points be drawn. Used for step, and for SQL GROUP BY time buckets |
$__rate_interval | max($__interval + scrape_interval, 4 × scrape_interval) | The correct range for rate()/increase() in PromQL: always wide enough to contain at least two samples |
$__range | The whole selected time range ($__range_s in seconds) | Instant queries over the full range ("total errors in the last 7d") |
$__from, $__to | Range boundaries in epoch ms | Links and SQL data sources |
The data source's configured scrape interval must match what Prometheus really uses: if it says 15 s while targets are scraped every 60 s, $__rate_interval becomes 60 s but only 1 sample fits in some windows and rate() returns no data.
Transformations, annotations and linking signals
- Transformations reshape query results in the browser/server before drawing: join two queries by label, filter, group-by, add a calculated field, convert a result to a table. Good for presentation; not for heavy computation (it happens on the already-fetched data, so the full result still crosses the wire).
- Annotations draw events as vertical lines/regions on graphs: deploys, feature-flag flips, incidents. They can be queries (e.g. a Prometheus metric, a Loki query) or posted via the API from CI. "What changed at 14:02" is the highest-value annotation.
- Data links and correlations make signals navigable. Derived fields in the Loki data source turn a regex match in a log line into a link:
trace_id=(\w+)→ internal link to Tempo with${__value.raw}. Tempo's trace to logs and trace to metrics settings go the other way, building a Loki/Prometheus query from the span's attributes. Exemplars (see Beyond the three pillars) put a clickable dot on a latency graph that opens the exact trace. Correlations is the general feature for defining such links between any two data sources.
loki datasource provisioning: derived field
apiVersion: 1
datasources:
- name: Loki
type: loki
url: http://loki-gateway
jsonData:
derivedFields:
- name: TraceID
matcherType: regex
matcherRegex: '"trace_id":"(\w+)"'
url: '$${__value.raw}'
datasourceUid: tempo-uid # internal link: open this trace in TempoDashboards as code, alerting and access
- Provisioning: Grafana loads data sources, dashboards and alert rules from YAML/JSON files on disk at startup and on interval. The UI-edited copy is then not the source of truth. It is the baseline that every other tool builds on.
- Generate the JSON: Grafonnet (a Jsonnet library, long-standing, now generated from the schema) and the newer Grafana Foundation SDK (typed builders in Go, TypeScript, Python, PHP, Java; marked public preview in its repository) emit dashboard JSON. Hand-editing exported JSON does not scale beyond a few dashboards: diffs are unreadable.
- Deploy it: the Terraform Grafana provider (folders, dashboards, data sources, alert rules, teams), the Kubernetes-style resources API, or Git Sync, which syncs dashboards with a GitHub repository and supports pull-request review. Git Sync was introduced in Grafana 12 as experimental/preview and has had restrictions (GitHub.com only at first); check the current docs before betting a team on it.
- Alerting: Grafana's unified alerting evaluates Grafana-managed rules against any data source (including SQL), then routes through contact points and notification policies (an Alertmanager-compatible tree: grouping, silences, mute timings). Alternatively data-source-managed rules live in Prometheus/Mimir/Loki rulers (preferred for large metric rule sets: evaluated next to the data, same alerting semantics as see Alerting and SLOs). Keep alert definitions in git either way.
- Folders and RBAC: dashboards live in folders; permissions attach to folders (viewer/editor/admin) for users, teams and roles; Enterprise/Cloud adds fine-grained RBAC and data source permissions. Organise by owning team, not by technology.
The LGTM stack
LGTM = Loki (logs), Grafana (visualization), Tempo (traces), Mimir (metrics). Added to it: Pyroscope (continuous profiling), Alloy (collector), and several instrumentation tools. Architecturally, Loki, Tempo, Mimir and Pyroscope share a design: horizontally scalable microservices (distributor → ingester → compactor → querier), recent data in ingesters, long-term data as immutable blocks in object storage, tenants isolated by an X-Scope-OrgID header.
Components and their status (2026-10)
| Piece | Role | Status / note |
|---|---|---|
| Mimir | Horizontally scalable Prometheus-compatible metrics store (see Scaling Prometheus) | Forked from Cortex in 2022; AGPLv3 |
| Loki | Logs (see Log pipelines and storage) | AGPLv3; bloom filters still experimental; SSD mode deprecated |
| Tempo | Traces keyed by trace id in object storage, TraceQL search (see the Distributed Tracing guide) | AGPLv3 |
| Pyroscope | Continuous profiling (see Beyond the three pillars) | Grafana Labs acquired Pyroscope in 2023 and merged it with its own Phlare; AGPLv3 |
| Alloy | Collector: Grafana's distribution of the OTel Collector with Prometheus/Loki/Pyroscope components (see OTel Collector) | Apache 2.0. Replaces Grafana Agent (Static/Flow/Operator, EOL 2025-11-01) and Promtail (EOL 2026-03-02); details in the OTel Collector part |
| Beyla → OBI | eBPF auto-instrumentation: RED metrics and spans with no code change | Donated to OpenTelemetry in 2025 as OpenTelemetry eBPF Instrumentation (OBI); Beyla is now Grafana's distribution of it |
| k6 | Load testing, scripts in JavaScript, results to Prometheus/Grafana | Part of Grafana Labs since 2021 |
| Faro | Frontend/RUM SDK (errors, web vitals, sessions) sending to a Faro receiver (in Alloy) | Pairs with Grafana Cloud Frontend Observability |
| OnCall / IRM | On-call scheduling, escalation, incidents | OnCall OSS archived Maintenance mode from 2025-03-11, archived 2026-03-24 (read-only repo, no more Cloud Connection for SMS/push). Development continues only in Grafana Cloud IRM |
Grafana Cloud vs self-hosted
Self-hosted LGTM
You run Mimir/Loki/Tempo (Helm, microservices mode for scale) plus object storage and caches (memcached). No per-series or per-GB bill, but you pay in engineers: upgrades, ring and compactor tuning, cardinality policing, capacity. Sensible when you have a platform team and large, predictable volume.
Grafana Cloud
Same open-source engines, managed, plus proprietary extras (Adaptive Telemetry, IRM, Synthetic Monitoring, SLO app). Usage billed by active series, GB of logs/traces, users and so on (see Cost and vendors). You trade money for operations, and your queries stay portable because the interfaces (PromQL, LogQL, OTLP) are open.
Good dashboard design
- One overview first. Top row per service: Rate, Errors, Duration (RED, see RED, USE and SLOs), then the SLO and remaining error budget as a stat. A reader answers "is it broken, for whom?" in five seconds.
- A drill-down hierarchy: fleet → service → instance/endpoint → logs/traces, connected by data links that carry variables and the time range. Not 40 panels on one page; if nobody can say what each panel decides, delete it.
- Latency as a heatmap (or p50/p95/p99 from histogram buckets), never an average. Do not draw an average of per-instance p99s: percentiles cannot be averaged (see Metrics); aggregate buckets, then compute the quantile.
- Consistent units and axes (seconds vs ms, bytes vs bits, percent 0–1 vs 0–100), same colours for same meaning, thresholds that match alert rules.
- Show the budget, not just the metric: burn rate and remaining error budget for each SLO (see Alerting and SLOs).
- Dashboards are for known questions; Explore is for new ones. A dashboard is a cache of questions someone once asked.
The Invariants
- Guarantees: Grafana renders whatever the data source returns at query time and keeps no copy of the telemetry;
$__rate_intervalis never smaller than four scrape intervals; the LGTM backends share one design (stateless query path, immutable blocks in object storage) so they scale and fail alike; cross-signal links work only because labels and trace ids are consistent across signals. - Does not guarantee: That a dashboard is cheap (each refresh re-runs every query); that panel numbers are right if the data source scrape interval setting is wrong; durability of dashboards made only in the UI (export or provision them); that any of the extra Grafana products are open source (OnCall OSS is archived; Adaptive Telemetry and IRM are Cloud-only).