ray.serve.metrics.Counter#

class ray.serve.metrics.Counter(name: str, description: str = '', tag_keys: Tuple[str] | None = None)[source]#

Bases: Counter

A serve cumulative metric that is monotonically increasing.

This corresponds to Prometheus’ counter metric: https://prometheus.io/docs/concepts/metric_types/#counter

Serve-related tags (“deployment”, “replica”, “application”, “route”) are added automatically if not provided.

@serve.deployment
class MyDeployment:
    def __init__(self):
        self.num_requests = 0
        self.my_counter = metrics.Counter(
            "my_counter",
            description=("The number of odd-numbered requests "
                "to this deployment."),
            tag_keys=("model",),
        )
        self.my_counter.set_default_tags({"model": "123"})

    def __call__(self):
        self.num_requests += 1
        if self.num_requests % 2 == 1:
            self.my_counter.inc()

Note

Before Ray 2.10, this exports a Prometheus gauge metric instead of a counter metric. Starting in Ray 2.10, this exports both the proper counter metric (with a suffix “_total”) and gauge metric (for compatibility). The gauge metric will be removed in a future Ray release and you can set RAY_EXPORT_COUNTER_AS_GAUGE=0 to disable exporting it in the meantime.

Parameters:
  • name – Name of the metric.

  • description – Description of the metric.

  • tag_keys – Tag keys of the metric.

PublicAPI (beta): This API is in beta and may change before becoming stable.

set_default_tags(default_tags: Dict[str, str])[source]#

Set default tags of metrics.

Example

>>> from ray.util.metrics import Counter
>>> # Note that set_default_tags returns the instance itself.
>>> counter = Counter("name", tag_keys=("a",))
>>> counter2 = counter.set_default_tags({"a": "b"})
>>> assert counter is counter2
>>> # this means you can instantiate it in this way.
>>> counter = Counter("name", tag_keys=("a",)).set_default_tags({"a": "b"})
Parameters:

default_tags – Default tags that are used for every record method.

Returns:

it returns the instance itself.

Return type:

Metric

inc(value: int | float = 1.0, tags: Dict[str, str] | None = None)[source]#

Increment the counter by the given value, add serve context tag values to the tags