> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Client Library

> Guide to using the Python prometheus_client to push, merge, and delete metrics to Prometheus Pushgateway with examples and registry usage

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

| Operation           | HTTP method | Python function       | Behavior                                                           | Example                                                               |
| ------------------- | ----------- | --------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------- |
| push (replace)      | PUT         | `push_to_gateway`     | Replace all metrics for the specified job/grouping labels          | `push_to_gateway('pushgateway:9091', job='batch', registry=registry)` |
| pushadd (merge/add) | POST        | `pushadd_to_gateway`  | Add or merge metrics into the group without deleting other metrics | `pushadd_to_gateway('user2:9091', job='batch', registry=registry)`    |
| delete              | DELETE      | `delete_from_gateway` | Remove all metrics for the specified job/grouping labels           | `delete_from_gateway('pushgateway:9091', job='batch')`                |

## 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).

```python theme={null}
from prometheus_client import CollectorRegistry, Gauge, pushadd_to_gateway

# Create a registry to hold the metrics for this job/process
registry = CollectorRegistry()

# Define a Gauge and register it with the registry
test_metric = Gauge(
    'test_metric',                 # metric name
    'This is an example metric',   # help/description
    registry=registry
)

# Set the metric value
test_metric.set(10)

# Push the registry to the Pushgateway (pushadd = POST-like behavior)
pushadd_to_gateway('user2:9091', job='batch', registry=registry)
```

## 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.

<Callout icon="lightbulb" color="#1CB2FE">
  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`.
</Callout>

## Additional imports and examples

If you need to perform replace or delete operations, import these functions:

```python theme={null}
from prometheus_client import push_to_gateway, delete_from_gateway
```

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

<Callout icon="warning" color="#FF6B6B">
  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.
</Callout>

## Links and references

* [Prometheus Pushgateway — pushing metrics](https://prometheus.io/docs/practices/pushing/)
* [prometheus\_client (Python) — GitHub](https://github.com/prometheus/client_python)
* [Prometheus documentation](https://prometheus.io/docs/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prometheus-certified-associate-pca/module/18b41166-411a-42a8-91c2-18a5b49bc189/lesson/bd98ee9b-5815-453b-a3b6-f261ae83377c" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prometheus-certified-associate-pca/module/18b41166-411a-42a8-91c2-18a5b49bc189/lesson/1b35c5df-3f0d-46cb-b867-50af1283ff50" />
</CardGroup>
