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.

The global section configures certain 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.

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

For a complete specification of configuration options, see the configuration documentation.