No history yet

Introduction to Prometheus

What is Prometheus?

Prometheus is an open-source tool for monitoring systems and sending out alerts. It was originally built at SoundCloud in 2012, inspired by Google's internal monitoring system, Borgmon. The project quickly gained popularity and is now a standalone open-source project maintained by the community.

At its heart, Prometheus is a time-series database. It records streams of timestamped values for different metrics, like CPU usage or the number of requests a web server receives. But what makes it powerful is its dimensional data model and its flexible query language, PromQL.

Lesson image

The Dimensional Data Model

Instead of identifying metrics with long, rigid names, Prometheus uses a metric name plus a set of key-value pairs called labels. These labels provide dimensions to the data, making it easy to slice and dice.

For example, a metric for the total number of HTTP requests might look like this:

http_requests_total{method="POST", handler="/api/messages"}

Here, http_requests_total is the metric name. The labels {method="POST", handler="/api/messages"} add context. This single metric can track requests for all HTTP methods and handlers. You can then easily query for all POST requests, all requests to the /api/messages handler, or a specific combination of both, all from the same metric.

How Prometheus Works

Prometheus has a pull-based architecture. This means the central Prometheus server scrapes, or fetches, metrics from target applications at regular intervals. These targets expose their metrics over an HTTP endpoint. For applications that can't expose metrics directly, a special program called an exporter is used to translate their data into the Prometheus format.

This architecture is simple and reliable. If a target is down, Prometheus simply fails to scrape it and can raise an alert. Each target doesn't need to know where the Prometheus server is, which simplifies configuration.

Querying with PromQL

Prometheus comes with a powerful query language called PromQL (Prometheus Query Language). It's designed specifically for time-series data and allows you to select and aggregate data in real time. You can use it to create graphs, dashboards, and alerts.

For instance, you can calculate the rate of HTTP requests per second over the last five minutes for a specific job.

rate(http_requests_total{job="api-server"}[5m])

PromQL has many functions for performing calculations, like predicting future values or calculating percentiles. This flexibility makes it an essential part of the Prometheus toolkit.

Prometheus serves as the primary engine for real-time performance metric collection, enabling efficient monitoring across multiple nodes.

Alerting

Collecting metrics is only half the battle. You also need to know when something is wrong. Prometheus handles this by allowing you to define alerting rules. An alerting rule is a PromQL expression that is evaluated at regular intervals. If it returns a value, the alert is considered firing.

Firing alerts are then sent to a separate component called the Alertmanager. The Alertmanager is responsible for deduplicating, grouping, and routing alerts to the correct destination, such as email, Slack, or PagerDuty. It can also silence alerts for a specific period, which is useful during maintenance windows.

Now that you have a grasp of what Prometheus is, let's test your knowledge.

Quiz Questions 1/5

What is the primary mechanism Prometheus uses to collect metrics from its targets?

Quiz Questions 2/5

In the Prometheus data model, what are the key-value pairs used to add dimensions to a metric name called?

Prometheus provides the foundation for reliable system monitoring. Its dimensional data model, pull-based architecture, and powerful query language make it a popular choice for cloud-native environments.