Prometheus — Overview
Introduction
Prometheus is a de-facto standard monitoring software, which is also one of the CNCF graduated projects. To gain a deeper understanding of it, this series will cover how Prometheus works. In this post, we’ll take a look at the overview of Prometheus.
Overview of Prometheus
Functionalities
Prometheus provides a variety of functionalities including
- finding scraping targets based on the scrape configuration,
- collecting metrics by scraping HTTP endpoints exposed by target applications or exporters,
- storing the collected metrics into the database,
- PromQL query with which you can aggregate metrics easily,
- Alert manager to set alert based on a rule for the collected metrics,
and so on.
Getting started
To start using Prometheus for monitoring, we need several steps as follows:
- Run Prometheus server. ( by executing
./prometheus
after downloading the binary from https://prometheus.io/download/, mainly for local run, or by using https://github.com/prometheus-operator/prometheus-operator to manage a bunch of Prometheus-related resources in a Kubernetes cluster) - Expose metrics from your application that you want to monitor.
- Set scrape config (e.g.
prometheus.yml
) to collect the metrics exposed by your application in the previous step. (Prometheus Server needs to reload the config to reflect the newly added scrape config.) - Utilize the collected metrics. (by checking the metrics on Prometheus UI, integrating with Grafana, a visualization tool, with which you can create a dashboard, setting alerts with Alertmanager, etc.)
To realize those features, there are several components in Prometheus. Let’s take a look at the Prometheus components.
Prometheus components:
There are several components in Prometheus, which can be categorized into a few groups based on the responsibility.
Storage: where and how to store metrics
- localStorage
- remoteStorage
- fanoutStorage
Managers: manage scrape config, targets discovery, scraping, and notification
- notifierManager
- discoveryManagerScrape
- discoveryManagerNotify
- scrapeManager
- ruleManager
Web & PromQL: Prometheus Web server (Web UI & API) and PromQL engine to return metrics based on the given query.
- queryEngine
- webHandler
In the next section, we’ll try to understand the entry point of Prometheus in the source code.
Entrypoint of Prometheus implementation
Prometheus main file main.go (v2.38.0) has more than 1.5k lines, which is overwhelming for beginners to start with. I’ll make a brief explanation about the main file in this post so that we can grasp the overview of the whole logic and we can easily dive into the implementation of each component that I’ll explain in the following series.
In the Prometheus implementation, there are several components that I have just named in the previous subsection.
main.go
has the following structure:
- L17~L77: import the dependent packages.
- L79~L96: Declare variables
- L98~L106:
init()
init function - L128~L208: The definition of
flagConfig
struct: to store the configuration loaded from the given config file (e.g.prometheus.yml
). - L210~L1120:
main()
main function!! - L1122~L1155:
openDBWithMetrics()
- L1157~L1591: safePromQLNoStepSubqueryInterval, Define loader, sender, readyStorage, notReadyAppender, readyScrapeManager, tsdbOptions, logger, discoveryManager..
Those components are initialized and started in the main function of Prometheus.
The main logic of Prometheus is as follows:
- Initialize the managers (e.g. NotificationManager, DiscoveryManager, ScrapeManager..).
- Set reloaders to
reloaders
, each of which reloads the configuration based on the configuration file change. - Set
Run()
of each manager and reload handlers torun.Group
- Start everything by calling
g.Run()
set in the previous step.
The most fundamental part to start is how Prometheus retrieves metrics based on the configuration file. There are two main components involved in the Prometheus metrics retrieval; DiscoveryManager and ScrapeManager.
From the next post, we’ll start with those two managers.