|
| 1 | +import logging |
| 2 | + |
| 3 | +from ray import serve |
| 4 | +from vllm.v1.metrics.ray_wrappers import ( |
| 5 | + RayPrometheusStatLogger, |
| 6 | + RaySpecDecodingProm, |
| 7 | + RayKVConnectorPrometheus, |
| 8 | + RayGaugeWrapper, |
| 9 | + RayCounterWrapper, |
| 10 | + RayHistogramWrapper, |
| 11 | +) |
| 12 | + |
| 13 | +logger = logging.getLogger("ray.serve") |
| 14 | + |
| 15 | + |
| 16 | +def _make_extended_metric_cls(base_cls, extra_labels): |
| 17 | + """Create a metric wrapper that transparently extends labelnames.""" |
| 18 | + |
| 19 | + class Extended(base_cls): |
| 20 | + def __init__(self, name, documentation=None, labelnames=None, **kwargs): |
| 21 | + extended_names = list(labelnames or []) + list(extra_labels.keys()) |
| 22 | + super().__init__(name=name, documentation=documentation, |
| 23 | + labelnames=extended_names, **kwargs) |
| 24 | + |
| 25 | + def labels(self, *args, **kwargs): |
| 26 | + if args: |
| 27 | + args = args + tuple(extra_labels.values()) |
| 28 | + if kwargs: |
| 29 | + kwargs.update(extra_labels) |
| 30 | + return super().labels(*args, **kwargs) |
| 31 | + |
| 32 | + return Extended |
| 33 | + |
| 34 | + |
| 35 | +def _make_extended_spec_decoding_cls(base_cls, extra_labels): |
| 36 | + """Extend SpecDecodingProm with custom labels via its _counter_cls.""" |
| 37 | + |
| 38 | + class Extended(base_cls): |
| 39 | + _counter_cls = _make_extended_metric_cls(RayCounterWrapper, extra_labels) |
| 40 | + |
| 41 | + return Extended |
| 42 | + |
| 43 | + |
| 44 | +def _make_extended_kv_connector_cls(base_cls, extra_labels): |
| 45 | + """Extend KVConnectorPrometheus with custom labels via its _cls vars.""" |
| 46 | + |
| 47 | + class Extended(base_cls): |
| 48 | + _gauge_cls = _make_extended_metric_cls(RayGaugeWrapper, extra_labels) |
| 49 | + _counter_cls = _make_extended_metric_cls(RayCounterWrapper, extra_labels) |
| 50 | + _histogram_cls = _make_extended_metric_cls(RayHistogramWrapper, extra_labels) |
| 51 | + |
| 52 | + return Extended |
| 53 | + |
| 54 | + |
| 55 | +class NeutreeRayStatLogger(RayPrometheusStatLogger): |
| 56 | + """RayPrometheusStatLogger with Ray Serve context labels injected. |
| 57 | +
|
| 58 | + Transparently extends all vLLM metrics with deployment, replica, |
| 59 | + and application labels from the Ray Serve replica context. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, vllm_config, engine_indexes=None): |
| 63 | + extra_labels = {} |
| 64 | + try: |
| 65 | + ctx = serve.get_replica_context() |
| 66 | + extra_labels = { |
| 67 | + "deployment": ctx.deployment, |
| 68 | + "replica": ctx.replica_tag, |
| 69 | + } |
| 70 | + if hasattr(ctx, "app_name"): |
| 71 | + extra_labels["application"] = ctx.app_name |
| 72 | + except RuntimeError: |
| 73 | + logger.warning( |
| 74 | + "NeutreeRayStatLogger: not running in Ray Serve context, " |
| 75 | + "skipping custom labels" |
| 76 | + ) |
| 77 | + |
| 78 | + if extra_labels: |
| 79 | + self._gauge_cls = _make_extended_metric_cls( |
| 80 | + RayGaugeWrapper, extra_labels) |
| 81 | + self._counter_cls = _make_extended_metric_cls( |
| 82 | + RayCounterWrapper, extra_labels) |
| 83 | + self._histogram_cls = _make_extended_metric_cls( |
| 84 | + RayHistogramWrapper, extra_labels) |
| 85 | + self._spec_decoding_cls = _make_extended_spec_decoding_cls( |
| 86 | + RaySpecDecodingProm, extra_labels) |
| 87 | + self._kv_connector_cls = _make_extended_kv_connector_cls( |
| 88 | + RayKVConnectorPrometheus, extra_labels) |
| 89 | + |
| 90 | + super().__init__(vllm_config, engine_indexes) |
| 91 | + |
| 92 | + logger.info( |
| 93 | + f"NeutreeRayStatLogger initialized with extra labels: " |
| 94 | + f"{list(extra_labels.keys())}" |
| 95 | + ) |
0 commit comments