Grafana gauge - the-code-camp/hpe-sre GitHub Wiki
Grafana - Adding a Gauge Panel
Sometimes you may want to visualize the current value of a time series as a speedometer-style gauge. This allows focusing on the current state of a metric, as well as giving you a clearer idea of where the current value lies within the range of possible values (often a gauge will have a natural minimum and maximum value). Grafana has a "Gauge" visualization type for this purpose.
Let's look at how we can show the demo service's average CPU usage in a gauge panel, as a value between 0% and 100%.
Creating the gauge panel
Add a new panel as you did previously. Then, click on the visualization type picker at the top of the right-hand-side menu:
In the visualization type selector, choose "Gauge":
Setting a PromQL query
Now set the PromQL query to the following expression:
sum(rate(demo_cpu_usage_seconds_total{mode!="idle"}[5m]) * 100) / 3 / 4
In this example, we aggregate over all the non-idle CPU usage percentages from the three demo instances, and then scale the value down to a maximum of 100% by dividing the result by the three demo service instances (/ 3) as well as the four CPU cores (/ 4) that each instance has.
Grafana will now show the current average CPU usage percentage in a gauge panel:
Configuring the gauge settings
Let's change a couple of settings to improve on the gauge's default display mode.
By default, Grafana will query values for the above PromQL expression over a range of time (corresponding to the dashboard's overall display time range) and then only show the latest non-null value within that range in the gauge panel. While this final aggregation step is configurable (e.g. choosing the average, min, or max value), running a range query is wasteful if we really only care about the very latest value.
To only query the latest value from Prometheus, enable the "Instant" toggle in the "Query" pane:
Our PromQL expression returns a value between 0 and 100 that should be interpreted as a percentage. To make Grafana show the correct unit, set the "Unit" field to "Misc > Percent (0-100)":
To scale the display of the current CPU usage value properly, we can also tell Grafana what the possible range of our values is (in our case, this is between 0 and 100). Set the "Min" and "Max" fields under the "Standard options" section on the right-hand side to 0 and 100, respectively:
Finally, set the title of the panel to "Average CPU Usage". The panel should now look like this:
Apply the panel again and save the dashboard to persist your changes.