Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Querying with PromQL

Introduction to PromQL

PromQL (Prometheus Query Language) is a powerful query language used to extract and manipulate time series data stored in Prometheus. It allows users to perform various operations on metrics, such as aggregations, filtering, and mathematical calculations.

Basic Concepts

PromQL is built around several key concepts:

  • Metrics: The data points stored in Prometheus, usually representing some quantifiable measurement.
  • Labels: Key-value pairs associated with metrics that allow for filtering and grouping.
  • Time Series: A collection of data points for a specific metric over time.

Simple Queries

The simplest form of a PromQL query retrieves the value of a specific metric. For example:

up

This query returns the value of the up metric, which indicates whether a target is reachable (1) or not (0).

Filtering with Labels

Labels can be used to filter the results of a query. For example, to get the value of the up metric for a specific instance:

up{instance="localhost:9090"}

This query retrieves the status of the Prometheus server running on localhost at port 9090.

Aggregation Operators

Aggregation operators allow you to combine multiple time series into a single result. Common aggregation operators include:

  • sum: Sums the values.
  • avg: Calculates the average.
  • max: Finds the maximum value.
  • min: Finds the minimum value.

For example, to get the total number of instances that are up:

sum(up)

This query sums the values of the up metric across all instances.

Range Vectors

Range vectors allow you to query over a specific time range. You can specify a time duration using square brackets:

rate(http_requests_total[5m])

This query calculates the per-second rate of http_requests_total over the last 5 minutes.

Using Functions

PromQL includes a range of built-in functions that can be applied to metrics. For example:

histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))

This computes the 95th percentile of HTTP request durations using histogram buckets.

Conclusion

PromQL is a versatile and powerful tool for querying time series data in Prometheus. By understanding its basic syntax, filtering capabilities, aggregation functions, and range vectors, you can effectively extract insights from your monitoring data.

For further exploration, consider looking into advanced functions and query optimization for more complex scenarios.