Grafana is a popular open-source platform for visualizing and analyzing data, and PromQL is a query language used to retrieve metrics from Prometheus, a popular monitoring tool. In this tutorial, we will provide an introduction to PromQL and show how to use it in Grafana.
What is PromQL?
PromQL is a query language used to retrieve and manipulate time-series data in Prometheus. It allows you to select, filter, and aggregate metrics, as well as perform mathematical operations on them. PromQL has a simple syntax that consists of metric names, labels, and functions.
Metric Names and Labels
In Prometheus, metrics are identified by a name and a set of labels. The metric name is a string that identifies the type of metric being collected, such as cpu_usage or http_requests. Labels are key-value pairs that provide additional context about the metric, such as the server or service where it was collected, or the HTTP method or status code for an HTTP request.
Here is an example of a Prometheus metric:
http_requests_total{method="GET", endpoint="/api/v1/status", status="200"}
In this example, http_requests_total is the metric name, and {method="GET", endpoint="/api/v1/status", status="200"} is a set of labels.
PromQL Functions
PromQL provides a variety of functions that can be used to manipulate and aggregate metrics. Here are some common functions:
sum: Calculates the sum of a metric across all instances.rate: Calculates the rate of change of a metric over time.avg: Calculates the average value of a metric over a given time period.min: Calculates the minimum value of a metric over a given time period.max: Calculates the maximum value of a metric over a given time period.
There are many more functions available, and you can find a complete list in the PromQL documentation.
Using PromQL in Grafana
To use PromQL in Grafana, you need to create a data source that connects to your Prometheus server. Once you have created a data source, you can start building a dashboard by adding panels that display metrics.
To add a panel, click the Add panel button and select the type of panel you want to create, such as a graph or a table. Then, click the Panel data tab and select the data source you want to use.
In the Query field, you can enter a PromQL query to retrieve the metrics you want to display. For example, to display the total number of HTTP requests, you can enter the following query:
http_requests_total
To add labels to the query, you can use the curly braces notation. For example, to display the total number of HTTP requests with the status code 200, you can enter the following query:
http_requests_total{status="200"}
To aggregate the data over time, you can use functions such as sum and rate. For example, to display the rate of HTTP requests per second, you can enter the following query:
rate(http_requests_total[1m])
In this example, 1m specifies the time range to use for the calculation.
Conclusion
PromQL is a powerful query language that can be used to retrieve and manipulate time-series data in Prometheus. With Grafana, you can create visualizations of your metrics using a simple and intuitive interface. By using PromQL in Grafana, you can quickly gain insights into the performance and behavior of your systems.

