Grafana is a web-based UI for visualizing data. It can consume data from many different sources, and it's particularly well matched with Prometheus. You might use the Prometheus UI to fine-tune your queries, and then you'd take the PromQL and use it in Grafana.
Grafana is a cross-platform app, written in Go. You can download Grafana for different platforms - but we'll run in a Docker container:
tier
labelRun the metrics components:
docker-compose -f labs/grafana/grafana.yml up -d
Check the Prometheus status at http://localhost:9090/targets
You'll see all the targets are down, but the tier
label is set along with instance
and job
. You can add to target labels like this to include infrastructure-specific details; you might include the region for cloud services, or the operating system and CPU architecture.
Browse to Grafana at http://localhost:3000
Sign in with the default credentials - you'll be asked to create a new password, but you can skip that step:
admin
admin
The homepage has a link to create a data source:
📋 Add a Prometheus data source, with the URL http://prometheus:9090
and test it.
Click the Add data source panel from the homepage, or browse to http://localhost:3000/datasources/new.
http://prometheus:9090
as the URLClick Save & test and you'll see the message Data source is working.
That's all the setup you need - now you can create a dashboard with panels to query Prometheus.
Click on the plus icon from the left menu and select Create...Dashboard. You'll see a screen like this:
Every visualization is a panel within a dashboard.
📋 Add a new panel to show the uptime of Prometheus targets using the up
metric.
Click Add empty panel and enter up
in the query panel. You'll see a preview of the visualization, which is not quite what we want:
The panel designer is in three sections:
The defaults are to use a Time series visualization, representing a range vector from Prometheus.
Now it's time to find your way around :)
📋 Change the panel setup so Grafana treats this as a table showing an instant vector, using a table visualization. Set up the table so the instance name and status are the only fields, showing ok
for instances which are up and OFFLINE
for others.
Here's what you're aiming for:
In the Query panel:
In the display options:
In the Transform panel:
Back in display options:
Add a title
scroll to Value mappings and click Edit value mappings
OFFLINE
ok
Click Apply at the top-right to update your panel and return to the dashboard.
Grafana dashboards can be set to auto-refresh. Select to refresh every 5 seconds (top-right of the dashboard), so when we start the apps you'll see the table update as they come online.
Start the apps, so Prometheus will have some metrics to collect:
Run the apps:
docker-compose -f labs/grafana/apps.yml up -d
Switch back to Grafana - you'll see the table update as instances come online.
If you watch long enough you'll see processor instance 1 flicks between ok
and OFFLINE
. The table we have shows the current status, and it would also be useful to see the history of each component's uptime.
Click the add panel icon in the top menu (it's the bar chart with a plus sign), and select Add an empty panel.
Last time we used a transform to hide labels, but we can do that in the query too:
sum without(job, tier) (up)
OFFLINE
and 1 as ok
We're getting there, just some tidying up to do.
📋 Edit the query and display options so the legend on the y-axis is more useful, hide the x-axis legend, and show OFFLINE
as a red bar and ok
as green.
This is the goal:
In the display options:
add a panel title
scroll to Legend
scroll to Thresholds
In the query panel:
Click Apply.
That's better - the dashboard now shows current and historical status for all the instances. We can see there's a recurring problem with processor 1, which isn't clear from the table alone.
You'll have several dashboards for your apps, showing information at different levels.
For this lab we'll stick with instance uptime, but your job is to split the dashboard into multiple rows, one for each tier of the application:
Each row should have a current status table and an timeline for the instances in that tier, something like this:
Save your dashboard as JSON so you can load it into another Grafana instance later.
Cleanup by removing all containers:
docker rm -f $(docker ps -aq)