Chances are, if your data visualization background comes from outside of IT, you have never heard anything about Grafana. Grafana has primarily been used for monitoring and logging dashboards, related to infrastructure and applications. The buzzword for that these days is Observability dashboards. There are other players in this area, like Splunk, Kibana, and many more. In this article, I’ll show you how to use Grafana in other more common scenarios such as, that chart for an article, or the dashboard for your health metrics.
Grafana was first released in 2014 by Torkel Ödegaard as a “spinoff” of a project at Orbitz. It targeted time-series databases such as InfluxDB, OpenTSDB, and Prometheus, but evolved to support relational databases such as Oracle, MySQL, PostgreSQL, and Microsoft SQL Server. As a visualization tool, Grafana is a popular component in monitoring stacks like Sensu, Icinga, Checkmark, Zabbix, Netdata, and PRTG.
Before you continue, it is important to highlight that I’m in no way trying to sell that Grafana is the best tool ever for data visualization, neither I have any personal gain in presenting the tool here. As a data visualization enthusiast, I think it is important to know tools that can help us to understand our data.
Currently, Grafana defines itself as a multi-platform, open-source analytics and interactive visualization web application. On the Grafana site, you can find their mission:
We believe in breaking traditional data boundaries, making metric visualization tools that are more accessible and easy to use across the entire organization. And most importantly, open source.
How to start with Grafana
The first thing to know about Grafana is that there are three ways to use it:
- Using the Grafana Cloud will give you access to a free version of Grafana, with some limitations related to the number of dashboards and users that can create/update dashboards;
- Installing on-premise will remove the limitations above, but you’ll need to find a way to publish on the web;
- If you want it for your company, get the Enterprise version.
In this article, you will use Grafana Cloud. Please create your account (at the time of this writing, they are promoting 10 days free of the PRO version with new registrations) to follow along. After you have logged in, you should see the following screen:
Then, click on the Login button on the Grafana box, and you’ll see the screen below:
This is the home page for your Grafana cloud. It can be customized with panels and visualizations, but you will address that later. Throughout the next sections, you will learn how to build a dashboard, step-by-step.
Creating a data source
Any dashboard needs data to show, and in Grafana you need to create a data source before you start. It is simple.
Click on the Configuration icon and then select Data Sources. You’ll see the screen below.
As you can see, there are some pre-defined data sources available. These data sources allow you to create some observability dashboards about your Grafana cloud instance. You can see the usage logs, traces, alerts, and more. But remember, your objective is to create other types of dashboards, so click on the Add Data Source button to configure your new data source.
In the list, you can see many of the available types of data sources. Grafana uses a plugin system, which allows you to add other types, as you see fit. For your dashboard, select the PostgreSQL data source.
The instructions to follow are simple, you just need your database connection information.
TLS/SSL mode: disable
Just fill the fields with your database information or the information above and click on Save and Test. You should see something like this:
Now, click on Back, and create your first dashboard.
Anatomy of a Grafana dashboard
If you have reached this point of the article, it means you are ready to created dashboards in Grafana. First, it is important to understand a few concepts about dashboards in Grafana:
- Dashboards are made of two other smaller units: Panels and Rows.
- Dashboards may have links between them.
- Dashboards may have variables that can act as filters on your visualizations.
- Dashboards may be imported/exported as JSON.
Panels are our charts, or visualizations if you prefer. Rows are a way to organize your panels inside the dashboard. Rows can help you avoid a lot of duplicated effort. Your recently-created dashboard should look like the one below.
Before you continue, save your dashboard, so you can give it a name. Click on the Save button and fill out the name of the dashboard, as in the image below.
Adding your first Panel
Now, click on Add Panel and get started.
When you select Add Panel, a new Panel will be created and you will enter the Panel Edit page, which will look like the image below:
Looking at this screen, you should be able to identify three areas of interest:
- The drawing area (1)
- The query area (2)
- The configuration area (3)
The drawing area is where the results will appear as you make changes in the two other areas. In the drawing area, you will also find the time filter and the button to refresh results. The query area is where you select the data source to work, write your query, and apply transformations when needed. The configuration area allows you to select the type of chart and all the layout configurations.
Creating your chart
Note that when you start a new panel, it defaults to a sample line chart. Line charts are the standard chart type in Grafana. If you want to change the chart type, you need to go to the configuration area and change that on the Visualization option.
For this tutorial, you will select the Bar Gauge chart. Observe that as you select it, the other options in the configuration area change.
Your next step is to change the query. The first change is on the data source. Right now, it should be showing something like ‘ — Grafana — .’ Change that to your recently created data source, called ‘PostgreSQL.’ This will change the text box right below.
Click on the ‘Edit SQL’ button and this will convert into a text box.
Now, you can write your own query. It is important to understand that you can write two types of queries: Time Series or Table.
Time Series queries need to have a field called time, which is a timestamp. If you click on Show Help, you can learn more about how to use a Time Series query. Here is a simple query to demonstrate:
Your query area would look like this:
SELECT answer, count(distinct respondent) as value
WHERE question = ‘Identifique sua faixa etária:’
GROUP BY answer
Your query area would look like this:
The query provoked an immediate change in the drawing area, as you can see below.
Now, to finalize, you need to make a few changes in the configuration area.
Change the chart to show one element for each answer
Change the chart by selecting the Display option.
There, you will change from Calculate to All values. The Calculate option was designed to display a single value, which is the result of a computation with all rows returned by the query.
This change will provoke an alteration in the drawing area.
Change the label of each element
As you can observe in the drawing area, you have now multiple bars, which is a good indication that you are on the right track. But, all the bars have the same label as ‘value.’ To fix that, you need to go to the Field Panel.
The standard options will already be opened, so you can make the following fixes:
- set the Min field to 0;
- set the Display name field to $__cell_0. This is a Grafana variable that indicates that you are going to get the value from the first column of the query, in this case, the column answer.
Again, looking at the drawing area, you can see what has changed. Woohoo! Your chart is almost perfect!
Setting the chart title
It is easy to title the chart, just click on the Settings and fill the Panel Title field.
Change the title to Age Range.
Note the change in the drawing area below.
Changing the style of the chart
Now, you can spend a little more time in the Display option on the Panel tab, making a few adjustments to improve the layout.
The main change here is to remove that gray area reserved for the bars. To simplify your visualization, remove that point of focus. To do that you just need to click on Show unfilled area. The result will be as follows:
Now, to finalize, click on Apply (at the right upper corner) and … voilá! Here is your first chart on your dashboard!
Now, you can edit the chart, add other charts, and move the charts around for better positioning. In my next article, I will cover chart configuration and variables.
In this article, you have learned how to create your first dashboard in Grafana and now you can add it to your data visualization toolbox. You have also explored the potential of Grafana beyond Observability dashboards. This link will lead you to the complete version of this dashboard for you to view. If you are interested in creating your own Grafana account and experimenting for yourself, this link will lead you to my Gitlab repository with the dashboard to be imported.