Pushgateway Tutorial
What is Pushgateway?
Pushgateway is a service that allows you to push metrics from batch jobs to Prometheus. It is particularly useful for jobs that are not continuously running and cannot expose metrics over HTTP. With Pushgateway, you can push metrics from your short-lived jobs, allowing Prometheus to scrape these metrics later.
How Does Pushgateway Work?
Pushgateway works by allowing clients to push metrics to it. Once these metrics are pushed, the Pushgateway stores them and exposes an HTTP endpoint that Prometheus can scrape. The typical flow is as follows:
- Your job runs and collects metrics.
- Your job pushes these metrics to the Pushgateway.
- Prometheus scrapes the Pushgateway to collect the metrics.
Setting Up Pushgateway
To set up Pushgateway, follow these steps:
- Download the Pushgateway binary from the official releases page.
- Run the Pushgateway using the command:
- By default, Pushgateway listens on port 9091. You can access it via http://localhost:9091.
You can also run Pushgateway in a Docker container with the following command:
Pushing Metrics to Pushgateway
You can push metrics using the HTTP API provided by Pushgateway. Here’s an example of how to push a simple counter metric:
In this example, we push a metric named my_job_requests_total
with a value of 1 associated
with the job my_job
.
Scraping Pushgateway with Prometheus
After pushing metrics, you need to configure Prometheus to scrape the Pushgateway.
Add the following configuration to your prometheus.yml
file:
With this configuration, Prometheus will scrape the Pushgateway every 15 seconds by default.
Cleaning Up Metrics
If you have metrics that are no longer needed, you can delete them using the HTTP API.
For example, to delete the metrics for my_job
, you can use:
Conclusion
Pushgateway is a powerful tool for pushing metrics from batch jobs to Prometheus. It allows you to monitor jobs that do not run continuously and provides a simple API for managing your metrics. With the steps outlined in this tutorial, you should be able to set up Pushgateway, push metrics, and configure Prometheus to scrape those metrics effectively.