使用 OpenTelemetry 进行分布式追踪 (Distributed Tracing)
本指南介绍如何在 vLLM Semantic Router 中配置和使用分布式追踪,以增强可观测性和调试能力。
概览
vLLM Semantic Router 使用 OpenTelemetry 实现了全面的分布式追踪,提供对请求处理管道的细粒度可见性。追踪可以帮助您:
- 调试生产问题:跟踪单个请求在整个路由管道中的路径
- 优化性能:识别分类、缓存和路由中的瓶颈
- 监控安全:跟踪 PII 检测和越狱防御操作
- 分析决策:了解路由逻辑和推理模式 (Reasoning Mode) 的选择
- 关联服务:连接路由和 vLLM 后端之间的追踪信息
架构
追踪层级 (Trace Hierarchy)
一个典型的请求追踪遵循以下结构:
semantic_router.request.received [根 span]
├─ semantic_router.classification (分类)
├─ semantic_router.security.pii_detection (PII 检测)
├─ semantic_router.security.jailbreak_detection (越狱检测)
├─ semantic_router.cache.lookup (缓存查找)
├─ semantic_router.routing.decision (路由决策)
├─ semantic_router.backend.selection (后端选择)
├─ semantic_router.system_prompt.injection (系统提示词注入)
└─ semantic_router.upstream.request (上游请求)
Span 属性
每个 span 都包含丰富的属性,遵循 LLM 可观测性的 OpenInference 规范:
请求元数据:
request.id- 唯一请求标识符user.id- 用户标识符(如果可用)http.method- HTTP 方法http.path- 请求路径
模型信息:
model.name- 选定的模型名称routing.original_model- 原始请求的模型routing.selected_model- 路由选择的模型
分类:
category.name- 分类结果类别classifier.type- 分类器实现类型classification.time_ms- 分类耗时
安全:
pii.detected- 是否发现 PIIpii.types- 检测到的 PII 类型jailbreak.detected- 是否检测到越狱尝试security.action- 采取的操作(拦截、允许)
路由:
routing.strategy- 路由策略(自动、指定)routing.reason- 路由决策原因reasoning.enabled- 是否启用推理模式reasoning.effort- 推理努力等级
性能:
cache.hit- 缓存命中/未命中状态cache.lookup_time_ms- 缓存查找耗时processing.time_ms- 总处理时间
配置
基础配置
在您的 config.yaml 中添加 observability.tracing 部分:
observability:
tracing:
enabled: true
provider: "opentelemetry"
exporter:
type: "stdout" # 或 "otlp"
endpoint: "localhost:4317"
insecure: true
sampling:
type: "always_on" # 或 "probabilistic"
rate: 1.0
resource:
service_name: "vllm-semantic-router"
service_version: "v0.1.0"
deployment_environment: "production"
配置选项
Exporter 类型
stdout - 将追踪打印到控制台(开发环境)
exporter:
type: "stdout"
otlp - 导出到兼容 OTLP 的后端(生产环境)
exporter:
type: "otlp"
endpoint: "jaeger:4317" # Jaeger, Tempo, Datadog 等
insecure: true # 生产环境中配合 TLS 使用 false
采样策略 (Sampling Strategies)
always_on - 对所有请求进行采样(开发/调试)
sampling:
type: "always_on"
always_off - 禁用采样(紧急性能处理)
sampling:
type: "always_off"
probabilistic - 按百分比对请求进行采样(生产环境)
sampling:
type: "probabilistic"
rate: 0.1 # 采样 10% 的请求
环境特定配置
开发环境 (Development)
observability:
tracing:
enabled: true
provider: "opentelemetry"
exporter:
type: "stdout"
sampling:
type: "always_on"
resource:
service_name: "vllm-semantic-router-dev"
deployment_environment: "development"
生产环境 (Production)
observability:
tracing:
enabled: true
provider: "opentelemetry"
exporter:
type: "otlp"
endpoint: "tempo:4317"
insecure: false # 使用 TLS
sampling:
type: "probabilistic"
rate: 0.1 # 10% 采样率
resource:
service_name: "vllm-semantic-router"
service_version: "v0.1.0"
deployment_environment: "production"