Skip to main content
In this lesson you’ll learn how to push metrics to the Prometheus Pushgateway using the official Python client library, prometheus_client. This walkthrough covers the three primary push operations, the common workflow for creating and registering metrics, and examples of using the client API.
  • Keywords: Prometheus Pushgateway, prometheus_client, pushadd_to_gateway, push_to_gateway, delete_from_gateway, CollectorRegistry, Gauge

Pushgateway operations at a glance

Minimal Python example

Below is a concise example that demonstrates the typical workflow:
  1. Create a CollectorRegistry.
  2. Define and register a Gauge.
  3. Set a value.
  4. Push the registry to the Pushgateway using pushadd_to_gateway (POST-like merge behavior).

Parameters explained

  • The first argument to the push function is the Pushgateway address, e.g. host:port ('pushgateway:9091').
  • job is a required grouping label; metrics are organized by job and optional additional grouping labels in the Pushgateway.
  • registry is the CollectorRegistry instance containing the metrics to push.
When you want to replace all metrics for a job, use push_to_gateway (PUT semantics). Use pushadd_to_gateway to add/merge metrics (POST semantics). To remove metrics for a job or grouping, use delete_from_gateway.

Additional imports and examples

If you need to perform replace or delete operations, import these functions:
Example usages:
  • Replace (PUT semantics):
    • push_to_gateway('pushgateway:9091', job='batch', registry=registry') # replaces metrics for job
  • Delete (DELETE semantics):
    • delete_from_gateway('pushgateway:9091', job='batch') # deletes metrics for job
Ensure the Prometheus Pushgateway is reachable from your process. Choose job and any additional grouping labels carefully to avoid unintentionally overwriting or deleting other metrics.

Watch Video

Practice Lab