AlgoViz

Grafana

easy 26 min

Grafana is a web application that queries many kinds of stores, draws the answers on dashboards and evaluates alert rules on them.

Video lesson · Chapter 1 of 5

Two pictures on the station wall

0:00 / 3:38

Grafana in 90 seconds

1:30Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Grafana is a server with a web interface. You tell it about data sources, such as a Prometheus server, a log store or a SQL database. A panel holds one or more queries and a way to draw the answer. A dashboard is a page of panels that share a time range and variables.

It is a reader. When you open a dashboard, the browser asks the Grafana server, the Grafana server asks each store in that store's own language, and the answers become charts. Grafana's own database holds only its things: dashboards, users, folders, alert rules, and the settings of the data sources.

The name is also a company, Grafana Labs, with other products such as Loki for logs, Tempo for traces and Mimir for metrics. This page is about Grafana, the dashboard server.

Why it exists

A system has several stores of facts about itself: metrics here, logs there, business numbers in a database. During an incident people need them side by side, over the same ten minutes. Copying everything into one store is expensive. Asking each store and drawing the answers together is cheap.

Charts are also a shared language. A dashboard in version control, with a variable for the service name, gives every team the same view. That is worth more than a private chart that only one person can read.

A short history

  1. January 2014

    Torkel Odegaard releases Grafana. Its interface began from Kibana 3, turned toward time series and easy editing.

  2. 2014

    The company behind it is founded, first under the name Raintank.

  3. April 2021

    Grafana, Loki and Tempo change from the Apache 2.0 licence to AGPLv3. Plugins, agents and some libraries stay Apache licensed.

  4. April 2026

    Grafana 13: dynamic dashboards and Git Sync become generally available.

  5. October 2026

    Version 13.3, the newest minor release at the time of writing.

Words to know

Data source
A configured connection to a store: type, address, credentials.
Panel
One picture: queries, optional transformations, a visualization.
Dashboard
Panels with a shared time range, refresh rate and variables. Saved as JSON.
Variable
A value chosen at the top of a dashboard and used inside queries.
Expression
A calculation on query results in the Grafana server, for example reduce or math.
Transformation
A change to the result in the browser before drawing, for example joining two tables.
Contact point
Where an alert message goes: email, chat, a paging service, a webhook.
Provisioning
Data sources and dashboards defined in files that are read at start.

How it works

Step through the idea, or press Play.

Step 1 of 3

Open. The browser loads a dashboard from Grafana's database

Understand it

Start here

The window-dresser

Imagine a street with three shops and one big display window. A window-dresser hangs pictures of what the shops have. The goods stay in the shops.

Pictures of other people's goods
HATSLAMPSBOOKSTOWN: NORTHAn empty window

If the picture is wrong, look in the shop first.

The idea

What is inside and what is not

One reader, several stores

Browsers reach one Grafana server inside our system over HTTPS. The server asks Prometheus for metrics with PromQL, Elasticsearch for logs, and PostgreSQL for business tables with SQL. A dashed arrow leads to its own database with dashboards, users and rules.
Losing Grafana's own database loses dashboards and users, not measurements.
  • Data sources

    Built in: Prometheus, Loki, Elasticsearch, Jaeger, Tempo, MySQL, PostgreSQL, Microsoft SQL Server, CloudWatch and more. Others as plugins.

  • Dashboards

    Panels, variables, links and annotations on one time range.

  • Explore

    A query page without a dashboard, for an investigation.

  • Alerting

    Rules on any backend data source, with routing and silences.

  • Permissions

    Organizations, folders and the roles Viewer, Editor and Admin.

  • As code

    Files, Git Sync, Terraform, an operator for Kubernetes, or a library that builds dashboards.

Three editions

Open sourceEnterpriseCloud
Who runs itYouYouGrafana Labs
LicenceAGPLv3Commercial, unlocks extra featuresSubscription, with a free tier
Query cachingNoYesYes
Permissions per data sourceNoYesYes
Dashboards and alertingYesYesYes
AGPLv3 asks you to offer the source of a changed version to the people who use it over a network. Using an unchanged server inside your company is not affected; ask a lawyer before you ship a modified one.

Inside

The life of one panel

Opening a dashboard

  1. 1

    Fill in

    Variables and the time range are put into the query text.

  2. 2

    Choose a step

    Max data points is by default the panel's width in pixels. The interval follows: the range divided by that number.

  3. 3

    Ask through the server

    In proxy mode the browser never sees the store's password. The server adds it.

  4. 4

    Same shape

    Every plugin returns data frames: columns with names and types. That is why panels and stores can be mixed.

  5. 5

    Transform and draw

    Join, filter, rename, calculate, then draw. Transformations work on data that has already arrived.

Numbers

Points and load

Why a long range is not slower by itself

interval=rangemax data points=7×86,400 s1,000≈605 s\text{interval} = \frac{\text{range}}{\text{max data points}} = \frac{7 \times 86{,}400\,\text{s}}{1{,}000} \approx 605\,\text{s}
A week on a panel 1,000 pixels wide asks for about one point in ten minutes. Grafana rounds it. The store may still have to read every sample.

How much load do screens make?

20 panels×30 open screens10 s=60 queriess20×3060 s=10 queriess\frac{20\ \text{panels} \times 30\ \text{open screens}}{10\,\text{s}} = 60\ \tfrac{\text{queries}}{\text{s}} \qquad \frac{20 \times 30}{60\,\text{s}} = 10\ \tfrac{\text{queries}}{\text{s}}
Assumed: one query per panel and no caching. The decision: refresh every minute unless somebody really looks every ten seconds. min_refresh_interval can enforce it; its default is 5 seconds.

Building

Variables instead of copies

Twelve services do not need twelve dashboards. A variable named service becomes a drop-down at the top, and every query uses it. A data source can be a variable too, so one dashboard serves staging and production.

Text
# Requests per second of the chosen servicessum by (service) (  rate(http_requests_total{service=~"$service"}[$__rate_interval]))# 95th percentilehistogram_quantile(0.95, sum by (le) (  rate(http_request_seconds_bucket{service=~"$service"}[$__rate_interval])))
A multi-value variable becomes a regular expression, so the match is =~ and not =.

Built-in variables

NameMeaningUse
$__intervalThe step that fits the panelSQL grouping, averages of gauges
$__rate_intervalmax(interval + scrape interval, 4 x scrape interval)Inside rate() and increase()
$__rangeThe whole time rangeOne total for the page
$__timeFilter(col)A SQL condition for the time rangeSQL data sources

From overview to cause

Rate, errors and duration show how happy users are. Utilization, saturation and errors show how machines feel.

A dashboard people use

  • Answers one question, with the most important row on top
  • Units and axis limits are set
  • Errors as a share, not only a count
  • A short text: what it is for and who owns it
  • Links from alert to dashboard to logs

A wall of noise

  • 60 panels that nobody can name
  • Two axes with different units in one chart
  • Averages of latency and no percentiles
  • Copies with one small change
  • Refresh every 5 seconds on data that changes hourly

Alerts

Rules that run without a screen

From rule to message

Grafana Alerting follows the Prometheus model: evaluating a rule and delivering a message are two jobs. A rule holds queries and expressions and one condition. If the query returns 12 series, the rule has 12 alert instances, each with its own state. Rules run in the server, so they need a data source with a backend, not a browser.

When the answer is missing

StateHappens whenChoices
No DataThe query works but returns nothingAn alert of its own (default), treat as firing, normal, or keep the last state
ErrorThe store fails or times outAn alert of its own (default), treat as firing, normal, or keep the last state
PendingThe condition holds, but not yet long enoughThe pending period
Keep the last state hides short gaps. If the store stays down, it also hides a real problem, so add a rule that watches the store.

Rules in Grafana

  • Any supported store: logs, SQL, cloud metrics
  • One page for all teams
  • Edited next to the panel

Rules in Prometheus

  • Work when Grafana is down
  • Files, tests with promtool, review
  • Only for Prometheus data

As code

Data sources and dashboards in files

YAML
# provisioning/datasources/main.yamlapiVersion: 1datasources:  - name: Prometheus    uid: prom-main    type: prometheus    access: proxy    url: http://prometheus:9090    isDefault: true    jsonData:      timeInterval: 15s  - name: Orders    uid: orders-db    type: postgres    url: db-replica:5432    user: grafana_reader    jsonData:      database: orders      sslmode: require    secureJsonData:      password: $ORDERS_DB_PASSWORD
The password comes from the environment, and the database user may only read, on a replica.
YAML
# provisioning/dashboards/main.yamlapiVersion: 1providers:  - name: shop    type: file    allowUiUpdates: false    updateIntervalSeconds: 30    options:      path: /var/lib/grafana/dashboards      foldersFromFilesStructure: true
With allowUiUpdates: false people can try changes in the browser, but not save them over the file.
WayGood forMind
Files (provisioning)Containers, a fixed setThe file wins over changes in the browser
Git SyncPeople edit in the browser, changes become commits or pull requestsGenerally available since 13
TerraformDashboards together with other infrastructureState files
Foundation SDKMany similar dashboards generated by a programA build step
Browser onlyExperimentsKeeps the last 20 versions, but no review

Operate

Running it for a company

Two servers, one database

Browsers reach a load balancer inside our system. It sends traffic to two machines, Grafana 1 and Grafana 2, and both use one shared MySQL or PostgreSQL database.
Any server can answer any request. The database is now the part to protect.

From one to two servers

  1. 1

    Leave SQLite

    The default database is a file. Two servers need MySQL 8.0 or newer, or PostgreSQL 12 or newer.

  2. 2

    Put a balancer in front

    Active-active. See load balancing.

  3. 3

    Connect the alerting

    Set ha_peers so the servers gossip on port 9094, TCP and UDP. Otherwise people get every message twice.

  4. 4

    Know what is doubled

    By default every server evaluates every rule, so the stores see the rule queries twice. A single-node evaluation mode exists. Duplicates can still happen: delivery is preferred over silence.

  5. 5

    Same key everywhere

    All servers need the same secret_key, or they cannot read the stored passwords of data sources.

The project's size classes

ClassUsers at onceAlert rulesDashboards
SmallUnder 25Under 100Under 200
Medium25 to 200100 to 1,000200 to 2,000
Large200 or more1,000 or more2,000 or more
The guide assumes 10 to 20 panels per dashboard and a refresh of 30 seconds or longer.

More settings to change on day one: the first administrator is admin with password admin, cookie_secure is false until you set it, and port 3000 speaks plain HTTP. Put TLS in front, use single sign-on, and limit which addresses data sources may point to.

A panel is empty or slow

Decide

When to choose it, and when not

Choose Grafana when

  • Data lives in several stores
  • Prometheus, Loki or SQL need a common view
  • Dashboards should be code
  • You want to run it yourself or move between clouds

Look elsewhere when

  • You need a place to store metrics: that is another product
  • Business users need pivot tables and reports: use a BI tool
  • Charts are part of your product for customers: draw them in your app
  • One vendor should do collection, storage and screens: see Datadog

Neighbours

ProductAlikeDifferent
Kibana, in the ELK StackDashboards, exploring logsMade for Elasticsearch; it does not query your other stores
Prometheus web pageRuns PromQL, draws a graphFor trying queries, not for saved dashboards
DatadogDashboards and alertsAlso collects and stores; a paid service
Jaeger web pageShows tracesOnly traces; Grafana can show them beside metrics

Summary

The whole topic in one minute.

Grafana asks other stores and draws. It does not collect and, apart from its own settings, it does not keep. So a slow dashboard is usually a slow query, and an empty panel is usually a store that is down or a wrong time range.

Use variables instead of copies, keep dashboards as code, run two servers with one shared database, and decide on one place for alert rules. Remember that a user of an organization can by default send any query to any of its data sources.

  • Grafana asks and draws. Measurements stay in their stores.
  • Panel = queries + transformations + a picture. Dashboard = panels + time range + variables.
  • Load = panels x open screens / refresh time.
  • Use $__rate_interval and set the scrape interval.
  • Two servers need a shared database, gossip for alerts and the same secret key.
  • Roles do not limit queries: limit the data source's account.

Sources