Configuring Prometheus

Before starting Prometheus, we need to create a configuration file for it.

Prometheus collects metrics from monitored targets by scraping metrics HTTP endpoints on these targets. Since Prometheus also exposes data in the same way about itself, it can also scrape and monitor its own health. While a Prometheus server that only collects data about itself is not very useful, it is a good starting example.

Save the following Prometheus configuration as a file named prometheus.yml in the current directory (overwrite the existing example prometheus.yml file):

global:
  scrape_interval: 5s

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

This configures Prometheus to scrape metrics from itself every 5 seconds.

Note: A 5-second scrape interval is quite aggressive, but useful for demonstration purposes where you want data to be available quickly. In real-world scenarios, intervals between 10 and 60 seconds are more common.

The global section configures global settings and default values, whereas the scrape_configs section tells Prometheus which targets to scrape. In our case, we are enumerating targets manually (in a <host>:<port> format) using the static_configs directive, but most production setups would use one or more service discovery integrations to discover targets.

For a complete specification of all configuration options, see Prometheus' configuration documentation.