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
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.
We must navigate to Prometheus's official website to download the blackbox exporter for our respective operating system.
After downloading the file, we need to extract it.
Then we need to open our terminal to the downloaded file's directory.
Open the "blackbox.yml" file and replace its contents with the code given below. This code names our module as
http_2xx_exampleand probes HTTP endpoints using the HTTP probe module with a 5 seconds timeout.
modules:http_2xx_example:prober: httptimeout: 5shttp:valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]valid_status_codes: [] # Defaults to 2xxmethod: GET
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/
Then paste these lines of code where
ExecStartis the path for the blackbox_exporter andconfig.fileis the path for the blackbox.yml file.
[Unit]Description=Prometheus ServerDocumentation=https://prometheus.io/docs/introduction/overview/After=network-online.target[Service]User=rootRestart=on-failureExecStart=/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
Finally, return to the terminal and restart the daemon by typing "sudo systemctl daemon-reload."
Using the commands below, we can see that our blackbox has started and is running.
sudo systemctl start blackboxsudo systemctl status blackbox
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.
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.
Search your file and find the
scrape_configsheading. If you can't find it, paste the given code as is; however, if you do, paste it without thescrape_configsheader and save your file. Remember that the module name is the same as in the blackbox file and that inreplacement, change the port to your desired value. Add any website link that you want to monitor in yourtargetsheading.
scrape_configs:- job_name: 'blackbox'metrics_path: /probeparams: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.comrelabel_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.
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
ExecStartis the path for the "prometheus" file andconfig.fileis the path for the prometheus.yml file.
[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=rootGroup=rootExecStart=/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
Use the following code snippet to restart and check the status of your services.
sudo systemctl restart prometheussudo 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.
Which of the following statements best describes Prometheus for web application monitoring?
Prometheus is a database management system specifically designed for web applications.
Prometheus is a web server that captures HTTP requests and responses for analysis.
Prometheus is a monitoring and alerting toolkit used to collect and store time-series data from web applications.
Prometheus is a web development framework used to build scalable and high-performance web applications.
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.
Free Resources