Sep 3rd, ‘24/2 min read

PromQL for Beginners: Getting Started with Prometheus Query Language

New to Prometheus? My PromQL beginner's guide teaches you how to write queries, understand data types, and use key functions.

PromQL for Beginners: Getting Started with Prometheus Query Language

PromQL (Prometheus Query Language) is a powerful query language used by Prometheus, a popular open-source monitoring and alerting toolkit. This guide will introduce you to the basics of PromQL and help you get started with writing queries.

What is PromQL?

PromQL is a query language designed to work with time series data, allowing you to select and aggregate data in real time. It's used for both graphing and alerting in Prometheus.

Basic Concepts

1. Metrics: Named measurements collected at regular intervals

2. Labels: Key-value pairs that add dimensions to metrics

3. Samples: Individual data points consisting of a float64 value and a millisecond-precision timestamp

📑
Also read: A Developer's Guide to PromQL in Prometheus

PromQL Structure

Anatomy of a Basic PromQL Query

This image illustrates the three main components of a basic PromQL query:

Metric Name: This is the name of the metric you want to query, such as http_requests_total

Label Selectors: These allow you to filter the metric based on specific labels, like method="GET" and status="200"

Time Range This optional component specifies the time range for the query, such as [5m] for the last 5 minutes. In the example query:

http_requests_total{method="GET", status="200"}[5m]

Simple Queries

The most basic PromQL query is just the name of a metric:

http_requests_total

Basic PromQL Query

This returns the current value of the `http_requests_total` metric for all monitored endpoints.

Label Matchers

You can filter metrics using label matchers:

http_requests_total{status="200", method="GET"}

Filter a metric for GET endpoints and status 200

This query selects only the HTTP requests with a status code of 200 and GET method.

Range Vectors

To query data over time, use range vectors:

http_requests_total[5m]

This returns all values of `http_requests_total` over the last 5 minutes.

Basic Functions

PromQL provides various functions to work with data:

  1. rate(): Calculate per-second rate of increase
   rate(http_requests_total[5m])
  1. sum(): Add up values
   sum(http_requests_total)
  1. avg(): Calculate average
   avg(node_cpu_utilization)

This guide has introduced you to the basics of PromQL. As you become more comfortable with these concepts, you'll be able to write more complex queries to gain deeper insights into your systems.

📑
Also read: The Awesome Prometheus Toolkit—Your Ideal Companion to Kickstart Monitoring

Next Steps

- Experiment with different functions and operators

- Learn about aggregation and grouping

- Explore more advanced PromQL features like subqueries and offset modifiers.

📑
Share your SRE experiences, and thoughts on reliability, observability, or monitoring. Let's connect on the SRE Discord community!

Newsletter

Stay updated on the latest from Last9.

Authors

Gabriel Diaz

Software Engineer at Last9

Handcrafted Related Posts