
CPRA is a high-performance infrastructure monitoring system designed for platform teams managing large-scale microservice architectures. Built on Entity-Component-System (ECS) architecture and queueing theory principles, CPRA handles 1,000,000+ concurrent health checks with automatic worker pool scaling to meet SLO targets.
并发监控数百万服务,具备自动修复和动态工作线程扩展功能。
CPRA 是一个高性能基础设施监控系统,专为管理大规模微服务架构的平台团队设计。基于实体-组件系统(ECS)架构和排队论原理,CPRA 可处理超过 100 万并发健康检查,并通过自动工作线程池扩展以满足服务等级目标(SLO)要求。
当您需要以下场景时使用 CPRA:
详细信息请参见架构概览中的基准测试与分析。
CPRA 采用基于实体-组件系统原则的三管道架构:


每个管道独立运行,拥有自己的队列和动态扩展的工作线程池,从而实现:

队列实现:
动态工作线程池:
关于架构的全面说明,请参见架构概览。
# 前置条件:Go 1.25 或更高版本
go version # 应显示 go1.25 或更高
# 从源码构建
git clone https://github.com/ziad/cpra.git
cd cpra
go build .
# 使用示例配置运行
./cpra --yaml mock-servers/test_10k.yaml
预期输出:
Starting CPRA Optimized Controller for 1M Monitors
Profiling server listening at http://localhost:6060/debug/pprof/
Loading monitors from mock-servers/test_10k.yaml...
Monitor loading completed in 1.2s
[INFO] Controller started successfully
[INFO] Pulse pipeline processing 10,000 monitors
[INFO] Worker pool scaled to 143 workers (target SLO: 100ms)
克隆仓库:
git clone https://github.com/ziad/cpra.git
cd cpra
下载依赖:
go mod download
构建应用:
go build .
验证安装:
./cpra --help
构建 Docker 镜像:
docker build -f docker/Dockerfile -t cpra:latest .
运行容器:
docker run -it --rm \
-v $(pwd)/my-monitors.yaml:/app/monitors.yaml \
cpra:latest \
./cpra --yaml monitors.yaml
创建 monitors.yaml 文件以定义健康检查:
monitors:
- name: "my-service-health-check"
pulse_check:
type: http
interval: 30s
timeout: 5s
max_failures: 3
config:
method: GET
url: http://my-service.example.com/health
retries: 2
intervention:
action: docker
config:
container: my-service-container
action: restart
codes:
red:
dispatch: true
notify: pagerduty
config:
url: https://events.pagerduty.com/v2/enqueue
yellow:
dispatch: true
notify: log
config:
file: /var/log/cpra-alerts.log
生成测试配置:
使用 mock-servers/generate_monitors.py 生成任意数量监控的测试配置。
以编程方式配置 CPRA 行为:
package main
import (
"cpra/internal/controller"
)
func main() {
config := controller.DefaultConfig()
// 调试模式
config.Debug = true
// 工作线程池设置(适用于所有三个管道)
config.WorkerConfig.MinWorkers = 10
config.WorkerConfig.MaxWorkers = 500
// 队列设置
config.QueueCapacity = 131072 // 必须是 2 的幂
// 性能调优
config.BatchSize = 2000
config.SizingServiceTime = 20 * time.Millisecond // 平均作业持续时间
config.SizingSLO = 100 * time.Millisecond // 目标延迟
config.SizingHeadroomPct = 0.15 // 15% 安全缓冲
ctrl := controller.NewController(config)
// ... 其余初始化代码
}
完整的配置选项请参见API 参考。
./cpra [OPTIONS]
示例:
# 以调试日志运行
./cpra --yaml monitors.yaml --debug
# 使用自定义 pprof 端口运行
./cpra --yaml monitors.yaml --pprof.addr localhost:8080
# 禁用性能分析
./cpra --yaml monitors.yaml --pprof=false
问题:YAML file not found
Warning: YAML file monitors.yaml not found, starting without loading monitors
解决方案: 验证文件路径是否正确。使用绝对路径或相对于二进制文件运行位置的路径:
./cpra --yaml $(pwd)/monitors.yaml
问题:Build fails with Go version error
go.mod requires go >= 1.25
解决方案: 将 Go 升级到 1.25 或更高版本:
go version # 检查当前版本
# 从 https://go.dev/dl/ 下载 Go 1.25+
问题:High memory usage
解决方案: 使用 pprof 检查内存使用情况:
# 当 CPRA 运行时,访问 pprof
go tool pprof http://localhost:6060/debug/pprof/heap
# 查看内存消耗最高的项
(pprof) top
在配置中调整内存限制:
config.WorkerConfig.MaxWorkers = 200 // 减少最大工作线程数
config.QueueCapacity = 65536 // 减少队列大小
问题:Worker pool not scaling
解决方案: 启用调试日志以查看扩展决策:
./cpra --yaml monitors.yaml --debug
检查排队论参数:
config.SizingServiceTime = 50 * time.Millisecond // 如果作业耗时更长,增加该值
config.SizingSLO = 200 * time.Millisecond // 如有需要放宽 SLO
问题:Monitors not executing
解决方案: 验证监控配置格式并检查日志:
./cpra --yaml monitors.yaml --debug 2>&1 | grep ERROR
验证 YAML 语法:
# 使用 YAML 验证工具
python -m yaml monitors.yaml
--debug 标志)我们欢迎来自社区的贡献!CPRA 是一个开源项目,我们感谢:
开始贡献:
good first issue 的问题开发资源:
本项目采用 MIT 许可证 授权 - 详情请参见 LICENSE 文件。
CPRA 基于优秀的开源库构建:
| 指标 | 值 |
|---|
| 最大并发监控数 | 1,000,000+ |
| 吞吐量 | 每秒 10,000+ 次检查/管道 |
| 延迟(P95) | < 100ms(可通过 SLO 配置) |
| 每个监控的内存 | ~100 字节 |
| 总内存(100 万监控) | ~100 MB + 工作线程池开销 |
| 工作线程扩展 | 动态(基于 M/M/c) |
| 选项 | 类型 | 默认值 | 描述 |
|---|
--yaml | string | internal/loader/replicated_test.yaml | 监控 YAML 文件路径 |
--config | string | - | 配置文件路径(可选) |
--debug | bool | false | 启用调试级别日志 |
--pprof | bool | true | 启用 pprof 性能分析服务器 |
--pprof.addr | string | localhost:6060 | Pprof 服务器监听地址 |