ray.serve.metrics.Gauge#

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

Bases: Gauge

Gauges keep the last recorded value and drop everything before.

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

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_gauge = metrics.Gauge(
            "my_gauge",
            description=("The current memory usage."),
            tag_keys=("model",),
        )
        self.my_counter.set_default_tags({"model": "123"})

    def __call__(self):
        process = psutil.Process()
        self.gauge.set(process.memory_info().rss)
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

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

Set the gauge to the given value, add serve context tag values to the tags