Implementation:Eventual Inc Daft Increment Counter
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Observability |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for incrementing custom counter metrics within UDF execution provided by the Daft library.
Description
The increment_counter function increments a named counter metric during UDF execution. It validates that the metric name is a non-empty string, the description (if provided) is a string, and all attribute keys and values are strings. If called outside a UDF execution context, a RuntimeWarning is emitted and the call is a no-op. Internally, it delegates to the Rust-backed OperatorMetrics.inc_counter method for efficient metric storage.
Usage
Import from daft.udf.metrics and call within UDF function or class method bodies to track custom counters.
Code Reference
Source Location
- Repository: Daft
- File:
daft/udf/metrics.py - Lines: L19-55
Signature
def increment_counter(
name: str,
amount: int = 1,
*,
description: str | None = None,
attributes: Mapping[str, str] | None = None,
) -> None
Import
from daft.udf.metrics import increment_counter
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | The metric counter name. Must be a non-empty string. |
| amount | int | No | The amount to increment the counter by. Defaults to 1. |
| description | None | No | Optional description of the metric. |
| attributes | None | No | Optional key-value attribute tags for categorizing the metric. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | No return value. Metrics are collected internally for aggregation by the execution framework. |
Usage Examples
Basic Usage
import daft
from daft.udf.metrics import increment_counter
@daft.func
def process_row(value: str) -> str:
if is_cached(value):
increment_counter("cache_hits")
else:
increment_counter("cache_misses")
return transform(value)
With Attributes
from daft.udf.metrics import increment_counter
increment_counter(
"api_calls",
amount=1,
description="Number of external API calls",
attributes={"endpoint": "/v1/predict", "status": "200"},
)