执行摘要
新增 spec_verify_calls_total 指标
监控 speculative decoding 的验证调用次数,便于性能分析和调优。
值得精读,展示如何为系统增加可观测性指标,可参考此模式添加其他监控。
监控 speculative decoding 的验证调用次数,便于性能分析和调优。
值得精读,展示如何为系统增加可观测性指标,可参考此模式添加其他监控。
TokenzierMetricsCollector.__init__ 中新增 Counter 指标 sglang:spec_verify_calls_total。observe_one_finished_request 方法签名中增加可选参数 spec_verify_ct,并在方法体内部根据 spec_verify_ct > 0 递增计数器。tokenizer_manager.py 的 collect_metrics 方法中,从 recv_obj 中安全提取 spec_verify_ct 字段并传递给 metrics collector。| 文件 | 模块 | 状态 | 重要度 |
|---|---|---|---|
python/sglang/srt/observability/metrics_collector.py |
可观测性 | modified | 5.44 |
python/sglang/srt/managers/tokenizer_manager.py |
调度器 | modified | 5.08 |
python/sglang/srt/observability/metrics_collector.py
core-logic
注册并实现新的 Prometheus 计数器。
# 在 __init__ 中注册新的 Prometheus 计数器
self.spec_verify_calls_total = Counter(
name="sglang:spec_verify_calls_total",
documentation="Number of speculative decoding verification calls.",
labelnames=labels.keys(),
)
# 修改 observe_one_finished_request 签名,增加 spec_verify_ct 参数
# 默认值为 0,确保向后兼容
def observe_one_finished_request(
self,
labels: Dict[str, str],
prompt_tokens: int,
generation_tokens: int,
cached_tokens: int,
e2e_latency: float,
has_grammar: bool,
cached_tokens_details: Optional[Dict[str, Any]] = None,
spec_verify_ct: int = 0, # 新增参数
):
self.prompt_tokens_total.labels(**labels).inc(prompt_tokens)
self.generation_tokens_total.labels(**labels).inc(generation_tokens)
# 只在 spec_verify_ct > 0 时递增计数器,避免无 spec 时增加开销
if spec_verify_ct > 0:
self.spec_verify_calls_total.labels(**labels).inc(spec_verify_ct)
# ... 其余代码保持不变
python/sglang/srt/managers/tokenizer_manager.py
core-logic
提取 spec_verify_ct 数据并传递给 metrics collector。
# 在 collect_metrics 方法中,在处理完 cached_tokens_details 之后
# 安全地从 recv_obj 提取 spec_verify_ct 字段
spec_verify_ct = (
recv_obj.spec_verify_ct[i] # 尝试按 index 取值
if hasattr(recv_obj, "spec_verify_ct") # 确保 recv_obj 有该属性
and recv_obj.spec_verify_ct # 确保属性不为 None
and len(recv_obj.spec_verify_ct) > i # 确保索引不越界
else 0 # 安全 fallback
)
# 将提取的值传递给 observe_one_finished_request
self.metrics_collector.observe_one_finished_request(
labels,
recv_obj.prompt_tokens[i],
completion_tokens,
recv_obj.cached_tokens[i],
state.time_stats.get_e2e_latency(),
self._request_has_grammar(state.obj),
cached_tokens_details,
spec_verify_ct=spec_verify_ct, # 新增参数
)
当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。
变更极小,无风险。新增计数器和可选参数均向后兼容。
影响范围小,仅增加一个 Prometheus 指标,对已有流程无影响。
当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。
参与讨论