Skip to main content
This guide shows how to push metrics from Python to a Prometheus Pushgateway using the Prometheus client library for Python. It focuses on the three primary operations you will use when pushing metrics and provides clear, copy-paste examples for common workflows.
  • push — replaces all metrics for a given job (HTTP PUT).
  • pushadd — merges the pushed metrics with any existing metrics for the same job, overriding any metrics with identical names while leaving other metrics intact (HTTP POST).
  • delete — removes all metrics for a given job/group (HTTP DELETE).
All three functions accept a registry argument (a CollectorRegistry instance) that contains the metrics you want to push. They also accept a job name and an optional grouping_key to distinguish different groups within the same job. Summary of Pushgateway client functions CollectorRegistry and registry usage
  • Use CollectorRegistry to create an isolated registry that contains only the metrics you intend to push.
  • Register metrics with the registry either by passing registry=... when creating them, or by registering collectors explicitly.
Example — pushadd (merge) This example creates a CollectorRegistry, defines a Gauge, sets its value, and merges it into the Pushgateway’s existing metrics for job batch.
Example — push (replace) Use push_to_gateway to replace all metrics for a job. This will overwrite any metrics previously stored for the specified job.
Example — delete (remove metrics for a job or grouping) When a job finishes or you need to remove metrics, call delete_from_gateway. You can delete all metrics for a job or target a specific grouping using a grouping key.
Notes on grouping keys
  • Use the grouping_key parameter to distinguish different subsets of metrics within the same job (for example, one job per instance).
  • Pass a dict of label names to values, for example: grouping_key={'instance': 'worker-1'}.
  • The grouping_key argument is supported by push_to_gateway, pushadd_to_gateway, and delete_from_gateway.
The Pushgateway is intended for short-lived batch jobs or ephemeral processes that cannot be scraped directly by Prometheus. For long-running services, prefer exposing metrics via an HTTP endpoint and letting Prometheus scrape them directly.
Links and references

Watch Video

Practice Lab