# Metrics

It is possible to export custom user metrics by adding the `metrics_client` argument to the task definition constructor. Below there is an example of how to use the metrics client.

Currently, it is only possible to instantiate the metrics client from a class task definition.

```python
class Task:
    def __init__(self, metrics_client):
        self.metrics = metrics_client

    def __call__(self, config):
        # --- my task code here ---
        ...

        # increment a counter with name "my_metric" and tags model:v1
        self.metrics.increment(metric="my_counter", value=1, tags={"model": "v1"})

        # set the value for a gauge with name "my_gauge" and tags model:v1
        self.metrics.gauge(metric="my_gauge", value=42, tags={"model": "v1"})

        # set the value for an histogram with name "my_histogram" and tags model:v1
        self.metrics.histogram(metric="my_histogram", value=100, tags={"model": "v1"})
```

**Note**: The metrics client uses the UDP protocol to push metrics, so if it fails during a metrics push, no exception is thrown.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cortexlabs.com/0.35/workloads/task-apis/metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
