Files
wehub-resource-sync 94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

308 lines
11 KiB
Python

# Copyright 2023-2024 SGLang Team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Ray-backed implementations of the prometheus_client API surface used by
sglang's ``*MetricsCollector`` classes.
The wrappers translate prometheus_client calls into ``ray.util.metrics`` so the
metrics emitted by an embedded sglang engine flow through Ray's metric agent and
appear on Ray's Prometheus endpoint / dashboard alongside other Ray metrics.
Mirrors ``vllm/v1/metrics/ray_wrappers.py`` with two sglang-specific additions:
* ``RaySummaryWrapper`` — Ray has no Summary primitive; we fall back to a
Histogram with conservative default boundaries. Quantile queries can be
approximated through ``histogram_quantile()`` in Prometheus.
* Five collector subclasses, one per ``*MetricsCollector`` defined in
:mod:`sglang.srt.observability.metrics_collector`, overriding only the
``_xxx_cls`` attributes that the corresponding collector actually uses.
Import is lazy: the module loads in environments without Ray installed, but
instantiating a wrapper without Ray raises a clear :class:`ImportError`.
"""
from __future__ import annotations
import copy
import re
import time
from typing import List, Optional
try:
from ray import serve as ray_serve
from ray.util import metrics as ray_metrics
from ray.util.metrics import Metric
except ImportError: # pragma: no cover - covered by a dedicated test
ray_metrics = None
ray_serve = None
Metric = None # type: ignore[assignment]
from sglang.srt.observability.metrics_collector import (
ExpertDispatchCollector,
RadixCacheMetricsCollector,
SchedulerMetricsCollector,
StorageMetricsCollector,
TokenizerMetricsCollector,
)
def _get_replica_id() -> Optional[str]:
"""Return the current Ray Serve replica ID, or ``None`` outside Serve."""
if ray_serve is None:
return None
try:
return ray_serve.get_replica_context().replica_id.unique_id
except ray_serve.exceptions.RayServeException:
return None
class RayPrometheusMetric:
"""Base wrapper that exposes the prometheus_client API on Ray metrics.
Subclasses populate ``self.metric`` with a ``ray.util.metrics`` instance in
their ``__init__``. Shared behaviour:
* A ``ReplicaId`` tag is appended to every metric and populated at
instantiation (and again on each ``labels()`` call) so Ray-Serve replicas
are distinguishable on dashboards.
* ``labels()`` returns a fresh copy of the wrapper with its tags bound,
mirroring the ``prometheus_client`` pattern and avoiding state sharing
between concurrent emits.
* Metric names are sanitised to satisfy Ray's OpenTelemetry naming rule
(no ``:``, no other punctuation).
"""
_is_labeled: bool = False
def __init__(self) -> None:
if ray_metrics is None:
raise ImportError(
"RayPrometheusMetric requires Ray to be installed. "
"Install with: pip install 'ray[serve]'"
)
self.metric: Optional[Metric] = None
self._tags: dict = {"ReplicaId": _get_replica_id() or ""}
@staticmethod
def _get_tag_keys(labelnames: Optional[List[str]]) -> tuple:
labels = list(labelnames) if labelnames else []
labels.append("ReplicaId")
return tuple(labels)
def _build_tags(self, *labels: str, **labelskwargs: str) -> dict:
if labels:
# The trailing entry of ``_tag_keys`` is always ``ReplicaId`` which we
# populate ourselves; positional args fill the preceding keys only.
expected = len(self.metric._tag_keys) - 1
if len(labels) != expected:
raise ValueError(
"Number of labels must match the number of tag keys. "
f"Expected {expected}, got {len(labels)}"
)
labelskwargs.update(zip(self.metric._tag_keys, labels))
labelskwargs["ReplicaId"] = _get_replica_id() or ""
return {k: v if isinstance(v, str) else str(v) for k, v in labelskwargs.items()}
def labels(self, *labels: str, **labelskwargs: str) -> RayPrometheusMetric:
if self._is_labeled:
raise ValueError("labels() cannot be called on an already-labeled metric.")
clone = copy.copy(self)
clone._tags = self._build_tags(*labels, **labelskwargs)
clone._is_labeled = True
return clone
@staticmethod
def _coerce_positive_boundaries(buckets):
# Ray (gRPC OpenCensus / OpenTelemetry export) rejects boundaries
# <= 0. sglang ships several histograms whose lowest bucket is 0.0
# (e.g. queue_time, e2e latency). Silently drop those so we never
# break engine startup when the metrics backend is Ray.
if not buckets:
return []
return [b for b in buckets if b > 0]
@staticmethod
def _get_sanitized_opentelemetry_name(name: str) -> str:
"""Replace characters Ray's OTel-backed metric name validator rejects.
Ray is migrating from OpenCensus to OpenTelemetry, whose instrument names
only allow ``a-zA-Z0-9_``. sglang's existing names use a ``sglang:foo``
prefix; converting ``:`` (and any other punctuation) to ``_`` keeps the
names valid without churn on the prometheus_client side.
"""
return re.sub(r"[^a-zA-Z0-9_]", "_", name)
class RayCounterWrapper(RayPrometheusMetric):
"""``prometheus_client.Counter`` compatible wrapper."""
def __init__(
self,
name: str,
documentation: Optional[str] = "",
labelnames: Optional[List[str]] = None,
) -> None:
super().__init__()
tag_keys = self._get_tag_keys(labelnames)
name = self._get_sanitized_opentelemetry_name(name)
self.metric = ray_metrics.Counter(
name=name,
description=documentation,
tag_keys=tag_keys,
)
def inc(self, value: float = 1.0) -> None:
if value == 0:
return
return self.metric.inc(value, tags=self._tags)
class RayGaugeWrapper(RayPrometheusMetric):
"""``prometheus_client.Gauge`` compatible wrapper."""
def __init__(
self,
name: str,
documentation: Optional[str] = "",
labelnames: Optional[List[str]] = None,
multiprocess_mode: Optional[str] = "",
) -> None:
# Ray aggregates per WorkerId/ReplicaId at the metric agent, so the
# prometheus_client multiproc modes ("mostrecent", "all", "sum") are not
# meaningful here. Accept and discard for API parity.
del multiprocess_mode
super().__init__()
tag_keys = self._get_tag_keys(labelnames)
name = self._get_sanitized_opentelemetry_name(name)
self.metric = ray_metrics.Gauge(
name=name,
description=documentation,
tag_keys=tag_keys,
)
def set(self, value: float) -> None:
return self.metric.set(value, tags=self._tags)
def set_to_current_time(self) -> None:
return self.set(time.time())
class RayHistogramWrapper(RayPrometheusMetric):
"""``prometheus_client.Histogram`` compatible wrapper."""
def __init__(
self,
name: str,
documentation: Optional[str] = "",
labelnames: Optional[List[str]] = None,
buckets: Optional[List[float]] = None,
) -> None:
super().__init__()
tag_keys = self._get_tag_keys(labelnames)
name = self._get_sanitized_opentelemetry_name(name)
self.metric = ray_metrics.Histogram(
name=name,
description=documentation,
tag_keys=tag_keys,
boundaries=self._coerce_positive_boundaries(buckets),
)
def observe(self, value: float) -> None:
return self.metric.observe(value, tags=self._tags)
class RaySummaryWrapper(RayPrometheusMetric):
"""``prometheus_client.Summary`` compatible wrapper.
``ray.util.metrics`` does not provide a Summary primitive. We approximate by
emitting through a Histogram with conservative default boundaries; quantile
queries can be approximated downstream via ``histogram_quantile()``.
"""
DEFAULT_BOUNDARIES: List[float] = [
0.005,
0.01,
0.025,
0.05,
0.1,
0.25,
0.5,
1.0,
2.5,
5.0,
10.0,
]
def __init__(
self,
name: str,
documentation: Optional[str] = "",
labelnames: Optional[List[str]] = None,
) -> None:
super().__init__()
tag_keys = self._get_tag_keys(labelnames)
name = self._get_sanitized_opentelemetry_name(name)
self.metric = ray_metrics.Histogram(
name=name,
description=documentation,
tag_keys=tag_keys,
boundaries=self._coerce_positive_boundaries(self.DEFAULT_BOUNDARIES),
)
def observe(self, value: float) -> None:
return self.metric.observe(value, tags=self._tags)
# ---------------------------------------------------------------------------
# Collector subclasses
#
# Each subclass only overrides the ``_xxx_cls`` attributes its parent actually
# uses; the parent's ``_StatLoggerDIMixin`` defaults handle the rest.
# ---------------------------------------------------------------------------
class RaySchedulerMetricsCollector(SchedulerMetricsCollector):
"""``SchedulerMetricsCollector`` that emits via Ray's metric system."""
_counter_cls = RayCounterWrapper
_gauge_cls = RayGaugeWrapper
_histogram_cls = RayHistogramWrapper
_summary_cls = RaySummaryWrapper
class RayTokenizerMetricsCollector(TokenizerMetricsCollector):
"""``TokenizerMetricsCollector`` that emits via Ray's metric system."""
_counter_cls = RayCounterWrapper
_histogram_cls = RayHistogramWrapper
class RayStorageMetricsCollector(StorageMetricsCollector):
"""``StorageMetricsCollector`` that emits via Ray's metric system."""
_counter_cls = RayCounterWrapper
_histogram_cls = RayHistogramWrapper
class RayRadixCacheMetricsCollector(RadixCacheMetricsCollector):
"""``RadixCacheMetricsCollector`` that emits via Ray's metric system."""
_counter_cls = RayCounterWrapper
_histogram_cls = RayHistogramWrapper
class RayExpertDispatchCollector(ExpertDispatchCollector):
"""``ExpertDispatchCollector`` that emits via Ray's metric system."""
_histogram_cls = RayHistogramWrapper