Understanding PromQL: A Powerful Query Language for Prometheus Monitoring

Matías Salinas
3 min readMar 23, 2023

--

Prometheus is a popular open-source monitoring and alerting tool used to monitor systems and applications. It provides a powerful query language called PromQL to retrieve metrics and perform analysis on them. In this article, we will explore what PromQL is, what it can do, and provide some examples of its usage.

What is PromQL?

PromQL is a query language specifically designed for Prometheus. It is used to retrieve and manipulate time-series data stored in the Prometheus database. Time-series data is a sequence of values collected over time and can be used to track changes in system performance and behavior.

PromQL allows you to select and filter metrics based on various dimensions such as labels, time ranges, and aggregation functions. It also supports various operators and functions to perform complex calculations and transformations on the data.

What can you do with PromQL?

PromQL provides a range of functions to help you monitor your systems and applications effectively. Some of the key features include:

  • Selecting and filtering metrics: PromQL allows you to select and filter metrics based on various dimensions, such as labels, time ranges, and aggregation functions.
  • Calculating rate and derivatives: You can use PromQL to calculate rates and derivatives of your metrics over a specific time range.
  • Aggregating metrics: You can use PromQL to aggregate your metrics across different dimensions, such as sum, min, max, avg, and count.
  • Performing mathematical and logical operations: PromQL supports various mathematical and logical operators, such as +, -, *, /, ==, !=, <, >, and so on, to perform complex calculations and transformations on your metrics.
An little example from prometheus UI

Examples of PromQL queries

Here are some examples of PromQL queries:

  1. Selecting and filtering metrics:

The following query selects the CPU usage metric for a specific instance based on the “instance” label:

cpu_usage{instance="my-instance"}

  1. Calculating rate and derivatives:

The following query calculates the rate of increase in the HTTP requests metric over a 5-minute period:

rate(http_requests_total[5m])

  1. Aggregating metrics:

The following query calculates the average CPU usage across all instances:

avg(cpu_usage)

  1. Performing mathematical and logical operations:

The following query calculates the difference in CPU usage between two instances:

cpu_usage{instance="instance-1"} - cpu_usage{instance="instance-2"}

Conclusion

PromQL is a powerful query language that allows you to retrieve and analyze time-series data stored in the Prometheus database. It provides a wide range of functions and operators to help you monitor your systems and applications effectively. By mastering PromQL, you can gain valuable insights into your system’s performance and behavior, and take timely action to address any issues that arise.

--

--

Matías Salinas
Matías Salinas

No responses yet