Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

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:

  1. Your job runs and collects metrics.
  2. Your job pushes these metrics to the Pushgateway.
  3. Prometheus scrapes the Pushgateway to collect the metrics.

Setting Up Pushgateway

To set up Pushgateway, follow these steps:

  1. Download the Pushgateway binary from the official releases page.
  2. Run the Pushgateway using the command:
  3. ./pushgateway
  4. 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:

docker run -d -p 9091:9091 --name pushgateway prom/pushgateway

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:

Example: Push a Counter Metric
echo "my_job_requests_total 1" | curl --data-binary @- http://localhost:9091/metrics/job/my_job

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:

Prometheus Configuration Example
scrape_configs: - job_name: 'pushgateway' static_configs: - targets: ['localhost:9091']

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:

Delete Job Metrics
curl -X DELETE http://localhost:9091/metrics/job/my_job

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.