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:- Create a
CollectorRegistry. - Define and register a
Gauge. - Set a value.
- 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'). jobis a required grouping label; metrics are organized by job and optional additional grouping labels in the Pushgateway.registryis theCollectorRegistryinstance 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:- 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.Links and references
- Prometheus Pushgateway — pushing metrics
- prometheus_client (Python) — GitHub
- Prometheus documentation