可观测性(Telemetry)
Telemetry addon 提供 OpenTelemetry 分布式追踪、Prometheus 指标和健康检查。
0.5 新增
RustPBX 还支持不依赖 Prometheus 的本地统计日志:设置 stats_log 为文件路径后,每隔 stats_interval_secs 写入一行 JSON,汇总系统 + PBX 健康状况——适合未部署 Prometheus 的主机做丢包/负载诊断。集群部署的会话心跳 gauges 也会在此处与 /metrics 中导出。
1. 启用
[proxy]
addons = ["telemetry"]
无需额外数据库表。
2. Prometheus 指标
2.1 端点
GET /metrics
可选 Bearer Token 认证:
[metrics]
bearer_token = "my-metrics-token"
2.2 RustPBX 核心指标
| 指标 | 类型 | 说明 |
|---|---|---|
rustpbx_calls_total | Counter | 通话总数 |
rustpbx_call_duration_seconds | Histogram | 通话时长分布 |
rustpbx_call_talk_time_seconds | Histogram | 实际通话时长 |
2.3 指标配置
[metrics]
enabled = true
bearer_token = "" # 留空则无需认证
export_interval_secs = 15 # 推送间隔(OTLP)
3. OpenTelemetry 追踪
3.1 配置
config/otel.toml:
[otel]
enabled = true
endpoint = "http://jaeger:4317" # OTLP gRPC 端点
service_name = "rustpbx"
sample_ratio = 0.1 # 10% 采样率
export_metrics = true # 同时导出指标
export_logs = false # 是否导出日志
3.2 支持的后端
| 后端 | 端点格式 |
|---|---|
| Jaeger | http://jaeger:4317 |
| Grafana Tempo | http://tempo:4317 |
| Datadog | https://trace.agent.datadoghq.com/api/v0.2/traces |
| 任何 OTLP 兼容后端 | http://host:4317 |
3.3 采样策略
- Head-based sampling(头部采样)
sample_ratio = 1.0:100% 采样(开发/调试)sample_ratio = 0.1:10% 采样(生产推荐)sample_ratio = 0.01:1% 采样(高流量)
3.4 Trace ID 注入
Trace ID 自动注入到日志记录中,方便日志与追踪关联。
4. 健康检查
GET /healthz
返回 JSON:
{
"status": "ok",
"uptime_secs": 86400,
"version": "0.4.4",
"active_calls": 42
}
用途:
- Kubernetes liveness/readiness probe
- Load Balancer 健康检查
- 监控告警
5. Grafana 集成
5.1 Prometheus 采集
# prometheus.yml
scrape_configs:
- job_name: 'rustpbx'
bearer_token: 'my-metrics-token'
static_configs:
- targets: ['203.0.113.10:8080']
5.2 推荐 Dashboard 面板
| 面板 | 指标 |
|---|---|
| 通话总量 | rustpbx_calls_total |
| 通话时长分布 | rustpbx_call_duration_seconds histogram |
| 活跃通话 | rustpbx_active_calls gauge |
| CPS | rate(rustpbx_calls_total[1m]) |
5.3 Wholesale 指标(如启用)
| 面板 | 指标 |
|---|---|
| 收入 | wholesale_revenue_microcurrency_total |
| 成本 | wholesale_cost_microcurrency_total |
| 利润 | wholesale_profit_microcurrency_total |
| ASR | 通过 CDR 计算 |
5.4 Call Center 指标(如启用)
| 面板 | 指标 |
|---|---|
| 排队数 | cc_queue_length |
| 可用坐席 | cc_agents_available |
| SLA | cc_sla_percentage |