Prometheus Tutorial for Beginners: Monitoring Guide

Prometheus Tutorial for Beginners

Introduction

It is 2 AM, and your application suddenly becomes slow. Is it CPU, memory, disk, or network? Without proper monitoring, you are left guessing. With the right monitoring setup, a dashboard can help you identify where the problem is coming from.

In today’s DevOps landscape, we have several observability tools that provide rich visualizations and valuable insights into server and application health. These tools help us monitor infrastructure, identify performance issues, and understand what is happening behind the scenes.

One of the key tools that completes a modern observability stack is Prometheus.

In this article, we will explore Prometheus Tutorial for Beginners, covering its fundamentals, how it works, its core components, and how to use it to monitor servers and applications.

What is Prometheus?

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.
It collects and stores its metrics as time series data, i.e. metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.

Before diving into Prometheus architecture, let’s first take a look at its key features.

Key Features of Prometheus

Prometheus offers several powerful features that make it a popular choice for monitoring infrastructure and applications:

  • Multi-dimensional Data Model: Stores metrics as time-series data, identified by metric names and key-value pairs called labels.
  • PromQL Query Language: Provides a flexible query language that allows you to filter, aggregate, and analyze metrics based on different dimensions.
  • Independent Server Architecture: Does not depend on distributed storage. Each Prometheus server operates independently.
  • Pull-Based Metrics Collection: Collects metrics from monitored targets by periodically scraping their HTTP endpoints.
  • Push Gateway Support: Supports pushing metrics through an intermediary component called the Pushgateway, particularly useful for short-lived jobs.
  • Service Discovery: Automatically discovers monitoring targets through supported service discovery mechanisms or allows them to be configured manually.
  • Visualization and Dashboarding: Supports multiple visualization options and integrates with tools such as Grafana to create interactive monitoring dashboards.

Understanding Prometheus Architecture and Its Core Components

Before setting up Prometheus, it is important to understand the different components that make up its ecosystem and how they work together.

Prometheus consists of several components, each designed to handle a specific monitoring task. Some are essential, while others are optional and can be added depending on your monitoring requirements.

Key Components of Prometheus

The Prometheus ecosystem includes the following components:

  • Prometheus Server: The core component responsible for collecting (scraping) metrics from monitoring targets, storing time-series data, and evaluating rules.
  • Client Libraries: Libraries that help developers instrument application code and expose custom metrics for Prometheus to collect.
  • Pushgateway: An intermediary component that allows short-lived jobs and batch processes to push their metrics to Prometheus.
  • Exporters: Specialized tools that expose metrics from systems and applications that do not natively support Prometheus. Examples include HAProxy, StatsD, and Graphite.
  • Alertmanager: Handles alerts generated by Prometheus, including grouping, deduplication, routing, and sending notifications.
  • Supporting Tools: Additional tools that extend Prometheus functionality and help with monitoring, visualization, and integrations like Grafana.

Most Prometheus components are written in Go, which makes them relatively easy to compile, distribute, and deploy as standalone binaries.

Source: https://prometheus.io/docs/introduction/overview/#architecture

How Does Prometheus Work?

Now that we understand the major components, let’s look at how they interact with each other in a typical monitoring environment.

At a high level, Prometheus follows a pull-based monitoring approach. It periodically scrapes metrics from instrumented applications and exporters through HTTP endpoints. Loki, by contrast, receives logs through push. If you read our Grafana Loki tutorial, you will see Loki borrows the same label idea.

For short-lived jobs that cannot be scraped directly, metrics can be sent through the Push gateway, which makes them available for Prometheus to collect.

Once the metrics are collected, Prometheus stores them locally as time-series data. It can then evaluate rules to:

  • Generate recording rules: Pre-compute and store frequently used queries as new time series.
  • Generate alerting rules: Identify conditions that require attention and send alerts to Alertmanager.

Finally, tools such as Grafana can connect to Prometheus through its query API to retrieve metrics and display them in dashboards.

This helps administrators and DevOps engineers monitor infrastructure health, identify performance bottlenecks, and troubleshoot issues.

Metric Types

Prometheus supports four core metric types. Each type is designed to capture a different kind of measurement.

1. Counter

  • Represents a cumulative value that can only increase over time.
  • Can reset to zero when the application restarts.
  • Used to track total requests, completed tasks, and application errors.
  • Example: Total number of HTTP requests received.

2. Gauge

  • Represents a numerical value that can increase or decrease.
  • Useful for monitoring values that change over time.
  • Commonly used for CPU usage, memory consumption, temperature, and active connections.
  • Example: Current memory usage of a server.

3. Histogram

  • Measures observations and groups them into configurable buckets.
  • Tracks the total number of observations and their sum.
  • Commonly used to monitor request duration, response size, and latency distribution.
  • Example: Measuring how long API requests take to complete.

4. Summary

  • Similar to a histogram, it tracks observations such as request duration and response size.
  • Calculates configurable quantiles over a sliding time window.
  • Also exposes the total count and sum of observations.
  • Example: Measuring API response latency at the 50th, 90th, and 99th percentiles.

4. Install Prometheus on Linux

Let’s start by installing Prometheus on a Linux server. For this tutorial, we will use a Linux x86_64 system with internet access.

Step 1: Download and Extract Prometheus

First, visit the official Prometheus download page and check the latest stable version. Replace <VERSION> in the commands below with the version you want to install.

$ wget https://github.com/prometheus/prometheus/releases/download/v<VERSION>/prometheus-<VERSION>.linux-amd64.tar.gz

$ tar -xvf prometheus-<VERSION>.linux-amd64.tar.gz

$ cd prometheus-<VERSION>.linux-amd64

Step 2: Configure Prometheus

Prometheus uses a YAML configuration file named prometheus.yml to define its scraping interval and monitoring targets.

For our initial setup, let’s use the following minimal configuration:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

Here, scrape_interval: 15s tells Prometheus to collect metrics every 15 seconds. We have also added Prometheus itself as the first monitoring target.

Step 3: Validate and Start Prometheus

Before starting the service, validate the configuration file using promtool.

$ ./promtool check config prometheus.yml

If the configuration is valid, start Prometheus:

$ ./prometheus --config.file=prometheus.yml

Prometheus will now start listening on port 9090.

Open the following URL in your browser:

http://localhost:9090

Navigate to Status → Targets. You should see the prometheus job with its state showing UP.

5. Monitor a Linux Server Using Node Exporter

Now that Prometheus is running, let’s monitor our Linux server.

Prometheus does not collect all Linux system-level metrics by itself. For this, we use Node Exporter, which exposes hardware and operating system metrics such as CPU, memory, disk, and network statistics.

Step 1: Download and Start Node Exporter

Download the latest stable version from the official Prometheus download page.

$ wget https://github.com/prometheus/node_exporter/releases/download/v<VERSION>/node_exporter-<VERSION>.linux-amd64.tar.gz

$ tar -xvf node_exporter-<VERSION>.linux-amd64.tar.gz

$ cd node_exporter-<VERSION>.linux-amd64

$ ./node_exporter

By default, Node Exporter listens on port 9100.

Step 2: Verify Node Exporter Metrics

Run the following command to check whether Node Exporter is exposing Linux system metrics:

$ curl http://localhost:9100/metrics | head

You should see metrics in plain-text format, including information about CPU, memory, filesystem, and other system resources.

Step 3: Add Node Exporter to Prometheus

Now we need to tell Prometheus to collect metrics from Node Exporter.

Open prometheus.yml and add the following configuration under scrape_configs:

 - job_name: "node"
    static_configs:
      - targets: ["localhost:9100"]

Your updated configuration should look like this:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node"
    static_configs:
      - targets: ["localhost:9100"]

Step 4: Validate and Reload Prometheus

Validate the updated configuration:

$ ./promtool check config prometheus.yml

If Prometheus was started with the –web.enable-lifecycle flag, you can reload the configuration without restarting the process:

$ curl -X POST http://localhost:9090/-/reload

Otherwise, restart Prometheus using the updated configuration.

Step 5: Verify the Monitoring Target

Go back to the Prometheus web interface:

http://<server-ip>:9090

Navigate to Status → Targets.

You should now see two monitoring jobs:

  • prometheus – Monitors Prometheus itself.
  • node – Collects Linux server metrics through Node Exporter.

Both targets should show the status UP.

Congratulations! You have successfully configured Prometheus to monitor your Linux server.

Common Issues and Fixes

Let’s now see some common issues that occurs while working with Prometheus and their fixes.

  • Target DOWN: run curl http://<target>:9100/metrics from the Prometheus server. Check that the exporter is running and that the port is open.
  • No data in graph: check the metric name, the time range, and that the target is UP.
  • Config change not applied: run promtool check config. Reload only works with –web.enable-lifecycle; otherwise restart.
  • Disk filling up: set –storage.tsdb.retention.time=15d and size the disk for your series count.
  • High memory: avoid high-cardinality labels like user_id or request_id.

Key Takeaways for Interviews

If you’re preparing for a DevOps or SRE interview, make sure you understand these important Prometheus concepts:

  • Pull-Based Monitoring: Prometheus collects metrics using a pull model, while Pushgateway helps collect metrics from short-lived jobs.
  • Metric Types: Understand the four metric types (Counter, Gauge, Histogram, and Summary) and how the rate() function helps calculate the per-second increase of counter metrics.
  • Exporters and Alertmanager: Know how exporters expose system and application metrics, and how Alertmanager manages and routes alerts.
  • Observability Stack: Prometheus is used for metrics monitoring, Loki handles log aggregation, and Grafana provides dashboards to visualize metrics and logs.

Frequently Asked Questions (FAQ)

1. Is Prometheus the same as Grafana?

No. Prometheus is responsible for collecting and querying metrics, whereas Grafana is a visualization tool that creates dashboards using data from sources such as Prometheus.

2. Can Prometheus monitor application logs?

No. Prometheus is designed for metrics monitoring, not log management. You can use Grafana Loki to collect, store, and query application logs.

3. Does Prometheus use a push or pull-based monitoring approach?

Prometheus primarily follows a pull-based approach, where it periodically scrapes metrics from configured targets over HTTP. However, it also supports pushing metrics through Pushgateway for short-lived jobs.

4. Can Prometheus monitor Kubernetes clusters?

Yes. Prometheus supports Kubernetes monitoring through service discovery and exporters. It can collect metrics from Kubernetes nodes, pods, containers, and applications.

Conclusion

If you’re new to Prometheus, start with the basics. Install Prometheus, configure Node Exporter, explore a few PromQL queries, and create your first alert. Once you’re comfortable with these concepts, move on to Alertmanager and Grafana dashboards.

Remember, metrics help you identify what is going wrong, while logs help you understand why it happened. To complete your observability learning journey, check out our Grafana Loki tutorial as well.

Need a quick reference while practising? Download my free Prometheus and PromQL Cheat Sheet, which includes ready-to-use queries, alert templates, and a troubleshooting checklist to help you during real-world monitoring tasks.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *