Configuring Prometheus and Grafana - spring-boot-in-practice/repo GitHub Wiki

You can configure Prometheus and Grafana in two ways - standalone installers or running as docker containers. Let us discuss how to install and configure these applications in the next section:

Standalone Installation

This section discusses the standalone installation and configuration of Prometheus and Grafana.

Installing Prometheus (Windows)

  • You can download the latest version from the https://prometheus.io/download/ page
  • Once downloaded, extract the installer component
  • Browse inside the folder and you can find the components:
  • console_libraries
  • consoles
  • LICENSE
  • NOTICE
  • prometheus(.exe)
  • prometheus.yml
  • promtool(.exe)
  • Prometheus maintains its configuration (e.g., the HTTP port, default port is 9090) through prometheus.yml configuration file. Following is the default configuration file ( I've replaced the hash(#) with tilde (~) symbol as hash(#) has a special meaning in Markdown, replace ~ with # in actual configuration all the examples) :
global:
  scrape_interval:     15s ~~ Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s ~~ Evaluate rules every 15 seconds. The default is every 1 minute.
  ~~ scrape_timeout is set to the global default (10s).

~~ Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      ~~ - alertmanager:9093

~~ Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  ~~ - "first_rules.yml"
  ~~ - "second_rules.yml"

~~ A scrape configuration containing exactly one endpoint to scrape:
~~ Here it's Prometheus itself.
scrape_configs:
  ~~ The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    ~~ metrics_path defaults to '/metrics'
    ~~ scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

Configuring Prometheus (Windows)

Now that prometheus is installed successfully, let us configure it to pull the metric from http://localhost:8080/actuator/prometheus endpoint.

global:
  scrape_interval:     15s ~~ Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s ~~ Evaluate rules every 15 seconds. The default is every 1 minute.
  ~~ scrape_timeout is set to the global default (10s).

~~ Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      ~~ - alertmanager:9093

~~ Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  ~~ - "first_rules.yml"
  ~~ - "second_rules.yml"

~~ A scrape configuration containing exactly one endpoint to scrape:
~~ Here it's Prometheus itself.
scrape_configs:
  ~~ The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    ~~ metrics_path defaults to '/metrics'
    ~~ scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:8080']

In addition to the default configuration, we've included the following section in the scrape_configs section:

    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:8080']

We have informed Prometheus the following details:

  • The name of the Job. In this demonstration, it is spring-actuator
  • The HTTP endpoint path: /actuator/prometheus
  • The scrape interval i.e. how often prometheus needs to pull data from /actuator/prometheus endpoint
  • Lastly, the application endpoint localhost:8080

Installing Grafana (Windows)

  • Download the Grafana installer from the https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1&platform=windows URL
  • Run the installer. Grafana stores its default configuration in the defaults.ini file. For instance, if you've installed Grafana at C:\Program Files, you can access this file at C:\Program Files\GrafanaLabs\grafana\conf folder
  • Access the http://localhost:3000 URL. By default, Grafana runs on HTTP port 3000
  • You need to login using the username _admin _and password as admin
  • Post login, you need to create a data source. Browse to the Create a Datasource link in the home page and add Prometheus as the datasource and provide the Prometheus HTTP endpoint http://localhost:9090/
  • You can now create your metric dashboard. Add a panel and look for the metric you need to visualize

Running as Docker Container

This section discusses how to run Prometheus and Grafana as docker containers.

Running Prometheus as Docker container

In order to run the Prometheus as docker container, you can use the YML configuration file used previously with a small change. In the YML file, you specify the Spring Boot application target address as localhost:8080. However, while running as Docker container, it can't access the localhost:8080. instead, you need to specify the network/IP address. Following is the updated sample YML file to be used with Docker (replace the IP_ADDRESS with the actual IP address value:

global:
  scrape_interval:     15s ~~ Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s ~~ Evaluate rules every 15 seconds. The default is every 1 minute.
  ~~ scrape_timeout is set to the global default (10s).

~~ Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      ~~ - alertmanager:9093

~~ Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  ~~ - "first_rules.yml"
  ~~ - "second_rules.yml"

~~ A scrape configuration containing exactly one endpoint to scrape:
~~ Here it's Prometheus itself.
scrape_configs:
  ~~ The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    ~~ metrics_path defaults to '/metrics'
    ~~ scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
       - targets: ['<IP_ADDRESS>:8080']
    ~~ - targets: ['192.168.0.100:8080']

Run the following command (replace the PROMETHEUS_FILE_PATH):

docker run --name prometheus -d -p 9090:9090 -v <PROMETHEUS_FILE_PATH>:/etc/prometheus/prometheus.yml prom/prometheus

Running Grafana as Docker container

Run the following command:

docker run -d -p 3000:3000 grafana/grafana

Open a browser, and access the http://localhost:3000. Post login, you need to create a data source. Browse to the Create a Datasource link in the home page and add Prometheus as the datasource and provide the Prometheus HTTP endpoint http://<IP_ADDRESS>:9090/. Note that localhost won't work as you are running as docker container.

  • You can now create your metric dashboard. Add a panel and look for the metric you need to visualize
⚠️ **GitHub.com Fallback** ⚠️