How to use Prometheus for web application monitoring

Prometheus is an event monitoring and alerting application. This standalone, open-source application enables users to scrape, collect, and store data as labeled metricsmetrics are statistical data. These metrics can then be used to query and locally store those metrics. Moreover, it has an alert feature that sends alerts via email and notification to its user when any metric falls out of the accepted range specified.

Simplified Prometheus architecture
Simplified Prometheus architecture

Now, let's see how we connect any web application to Prometheus and get its metrics.

Prometheus blackbox

The Prometheus blackbox exporter probes endpoints and scraps data through HTTP, HTTPS, ICMP, DNS, and TCP. It then collects website metrics to check their health, status, and responsiveness. This is our starting point.

  1. We must navigate to Prometheus's official website to download the blackbox exporter for our respective operating system.

  2. After downloading the file, we need to extract it.

  3. Then we need to open our terminal to the downloaded file's directory.

File directory
File directory
  1. Open the "blackbox.yml" file and replace its contents with the code given below. This code names our module as http_2xx_example and probes HTTP endpoints using the HTTP probe module with a 5 seconds timeout.

modules:
http_2xx_example:
prober: http
timeout: 5s
http:
valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
valid_status_codes: [] # Defaults to 2xx
method: GET
  1. Now we will create the blackbox service file and configure it. For this, go to the given directory and create a file named "blackbox.service".

/etc/systemd/system/
  1. Then paste these lines of code where ExecStart is the path for the blackbox_exporter and config.file is the path for the blackbox.yml file.

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
User=root
Restart=on-failure
ExecStart=/home/naumanijaz/Desktop/blackbox_exporter-0.24.0.linux-amd64/blackbox_exporter --config.file=/home/naumanijaz/Desktop/blackbox_exporter-0.24.0.linux-amd64/blackbox.yml
[Install]
WantedBy=multi-user.target
  1. Finally, return to the terminal and restart the daemon by typing "sudo systemctl daemon-reload."

  2. Using the commands below, we can see that our blackbox has started and is running.

sudo systemctl start blackbox
sudo systemctl status blackbox
Blackbox service

Prometheus server

After we set up our blackbox, we need to configure our Prometheus server to start scraping information from the blackbox. To do that, we need to edit the configuration file.

  1. First, redirect to your Prometheus directory (if you have not downloaded Prometheus yet, navigate to Prometheus's official website to download it) and open your prometheus.yml file.

  2. Search your file and find the scrape_configs heading. If you can't find it, paste the given code as is; however, if you do, paste it without the scrape_configs header and save your file. Remember that the module name is the same as in the blackbox file and that in replacement, change the port to your desired value. Add any website link that you want to monitor in your targets heading.

scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx_example] # Look for a HTTP 200 response.
static_configs:
- targets:
- http://prometheus.io # Target to probe with http.
- http://www.educative.io
- http:/www.google.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115 # The blackbox exporter's real hostname:port.
  1. Now we will create the Prometheus service file and configure it. For this, go to the given directory and create a "prometheus.service" file. Then paste these lines of code where ExecStart is the path for the "prometheus" file and config.file is the path for the prometheus.yml file.

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
ExecStart=/home/naumanijaz/Desktop/prometheus-2.44.0.linux-amd64/prometheus --config.file=/home/naumanijaz/Desktop/prometheus-2.44.0.linux-amd64/prometheus.yml
[Install]
WantedBy=default.target
  1. Use the following code snippet to restart and check the status of your services.

sudo systemctl restart prometheus
sudo systemctl status prometheus

Data analytics

Finally, we have created and connected our Prometheus service to our website links. To check the information received from this website, we can go to the link given below. In this code, localhost:9115 is the hostname:port from where our Prometheus is running. Our target is which website we want to probe from within our given list. Lastly, module is the name we assigned to our blackbox module.

http://localhost:9115/probe?target=google.com&module=http_2xx_example

Conclusion

Prometheus allows its users to gather all metrics from their requested websites. This, in short, provides for an organized approach to maintaining the website and working on it if need be.

1

Which of the following statements best describes Prometheus for web application monitoring?

A)

Prometheus is a database management system specifically designed for web applications.

B)

Prometheus is a web server that captures HTTP requests and responses for analysis.

C)

Prometheus is a monitoring and alerting toolkit used to collect and store time-series data from web applications.

D)

Prometheus is a web development framework used to build scalable and high-performance web applications.

Question 1 of 20 attempted

To create better visualization and add alerting and other monitoring tools, we can use other applications such as Grafana. To learn how to incorporate Grafana into our Prometheus setup, we can look at this answer.

Copyright ©2024 Educative, Inc. All rights reserved