executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
define-slo-sli-sla建立服务级别目标(SLO)、服务级别指标(SLI)和服务级别协议(SLA), 通过错误预算追踪、燃烧率告警及使用 Prometheus 和 Sloth 或 Pyrra 等工具的自动化报告。 适用于为面向客户的服务定义可靠性目标、通过错误预算平衡功能迭代速度与系统可靠性、 将任意正常运行时间目标迁移到数据驱动的指标,或实施站点可靠性工程实践。
Independent GitHub snapshot · Automated classification · Original documentation governs
建立可衡量的可靠性目标,用服务级别目标追踪并管理错误预算。
完整配置文件和模板请参阅 Extended Examples。
了解这三个概念之间的关系和差异。
定义:
SLI (Service Level Indicator)
- **What**: A quantitative measure of service behavior
- **Example**: Request success rate, request latency, system throughput
- **Measurement**: `successful_requests / total_requests * 100`
SLO (Service Level Objective)
- **What**: Target value or range for an SLI over a time window
- **Example**: 99.9% of requests succeed in 30-day window
- **Purpose**: Internal reliability target to guide operations
SLA (Service Level Agreement)
- **What**: Contractual commitment with consequences for missing SLO
- **Example**: 99.9% uptime SLA with refunds if breached
- **Purpose**: External promise to customers with penalties
层次结构:
SLA (99.9% uptime, customer refunds)
├─ SLO (99.95% success rate, internal target)
│ └─ SLI (actual measured: 99.97% success rate)
└─ Error Budget (0.05% failures allowed per month)
关键原则:SLO 应严于 SLA,在客户受影响之前提供缓冲。
示例:
预期结果: 团队理解各概念差异,就哪些指标成为 SLI 达成共识,对 SLO 目标保持一致。
失败处理:
选择反映用户体验和业务影响的 SLI。
四个黄金信号(Google SRE):
延迟:处理请求的时间
# P95 latency
histogram_quantile(0.95,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le)
)
流量:系统需求
# Requests per second
sum(rate(http_requests_total[5m]))
错误:失败请求的速率
# Error rate percentage
sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m])) * 100
饱和度:系统的"饱满"程度
# CPU saturation
avg(rate(node_cpu_seconds_total{mode!="idle"}[5m]))
常见 SLI 模式:
# Availability SLI
availability:
description: "Percentage of successful requests"
query: |
sum(rate(http_requests_total{status!~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))
good_threshold: 0.999 # 99.9%
# Latency SLI
latency:
description: "P99 request latency under 500ms"
query: |
histogram_quantile(0.99,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le)
) < 0.5
good_threshold: 0.95 # 95% of windows meet target
# Throughput SLI
throughput:
description: "Requests processed per second"
query: |
sum(rate(http_requests_total[5m]))
good_threshold: 1000 # Minimum 1000 req/s
# Data freshness SLI (for batch jobs)
freshness:
description: "Data updated within last hour"
query: |
(time() - max(data_last_updated_timestamp)) < 3600
good_threshold: 1 # Always fresh
SLI 选择标准:
避免:
预期结果: 每个服务选择 2-4 个 SLI,至少覆盖可用性和延迟,团队对测量查询达成共识。
失败处理:
定义现实可行的可靠性目标。
SLO 规格格式:
service: user-api
slos:
- name: availability
objective: 99.9
description: |
99.9% of requests return non-5xx status codes
# ... (see EXAMPLES.md for complete configuration)
时间窗口选择:
常见窗口:
30 天窗口错误预算示例:
SLO: 99.9% availability over 30 days
Allowed failures: 0.1%
Total requests per month: 100M
Error budget: 100,000 failed requests
Daily budget: ~3,333 failed requests
设置现实目标:
建立当前性能基准:
# Check actual availability over past 90 days
avg_over_time(
(sum(rate(http_requests_total{status!~"5.."}[5m]))
/ sum(rate(http_requests_total[5m])))[90d:5m]
)
# Result: 99.95% → Set SLO at 99.9% (safer than current)
计算每个 9 的代价:
99% → 7.2 hours downtime/month (low reliability)
99.9% → 43 minutes downtime/month (good)
99.95% → 22 minutes downtime/month (very good)
99.99% → 4.3 minutes downtime/month (expensive)
99.999% → 26 seconds downtime/month (very expensive)
平衡用户满意度与工程成本:
预期结果: 在业务利益相关方认可下设定 SLO 目标,记录理由,计算错误预算。
失败处理:
使用 Sloth 从 SLO 规格生成 Prometheus 记录规则和告警。
安装 Sloth:
# Binary installation
wget https://github.com/slok/sloth/releases/download/v0.11.0/sloth-linux-amd64
chmod +x sloth-linux-amd64
sudo mv sloth-linux-amd64 /usr/local/bin/sloth
# Or Docker
docker pull ghcr.io/slok/sloth:latest
创建 Sloth SLO 规格(slos/user-api.yml):
version: "prometheus/v1"
service: "user-api"
labels:
team: "platform"
tier: "1"
slos:
# ... (see EXAMPLES.md for complete configuration)
生成 Prometheus 规则:
# Generate recording and alerting rules
sloth generate -i slos/user-api.yml -o prometheus/rules/user-api-slo.yml
# Validate generated rules
promtool check rules prometheus/rules/user-api-slo.yml
生成的记录规则(摘录):
groups:
- name: sloth-slo-sli-recordings-user-api-requests-availability
interval: 30s
rules:
# SLI: Ratio of good events
- record: slo:sli_error:ratio_rate5m
# ... (see EXAMPLES.md for complete configuration)
生成的告警:
groups:
- name: sloth-slo-alerts-user-api-requests-availability
rules:
# Fast burn: 2% budget consumed in 1 hour
- alert: UserAPIHighErrorRate
expr: |
# ... (see EXAMPLES.md for complete configuration)
将规则加载到 Prometheus:
# prometheus.yml
rule_files:
- "rules/user-api-slo.yml"
重新加载 Prometheus:
curl -X POST http://localhost:9090/-/reload
预期结果: Sloth 生成多窗口多燃烧率告警,记录规则成功评估,告警在事故期间适当触发。
失败处理:
yamllint slos/user-api.yml 验证 YAML 语法curl http://localhost:9090/api/v1/rules在 Grafana 中可视化 SLO 合规性和错误预算消耗。
Grafana 仪表板 JSON(摘录):
{
"dashboard": {
"title": "SLO Dashboard - User API",
"panels": [
{
"type": "stat",
# ... (see EXAMPLES.md for complete configuration)
需可视化的关键指标:
错误预算策略仪表板(markdown 面板):
## Error Budget Policy
**Current Status**: 78% budget remaining
### If Error Budget > 50%
- ✅ Full speed ahead on new features
# ... (see EXAMPLES.md for complete configuration)
预期结果: 仪表板显示实时 SLO 合规性,错误预算消耗可见,团队可对功能迭代速度做出明智决策。
失败处理:
curl http://localhost:9090/api/v1/rules | jq '.data.groups[].rules[] | select(.name | contains("slo:"))'定义管理错误预算的组织流程。
错误预算策略模板:
service: user-api
slo:
availability: 99.9%
latency_p99: 200ms
window: 30 days
# ... (see EXAMPLES.md for complete configuration)
自动化策略执行:
# Example: Deployment gate script
import requests
import sys
def check_error_budget(service):
# Query Prometheus for error budget
# ... (see EXAMPLES.md for complete configuration)
集成到 CI/CD 管道:
# .github/workflows/deploy.yml
jobs:
check-error-budget:
runs-on: ubuntu-latest
steps:
- name: Check SLO Error Budget
run: |
python scripts/check_error_budget.py user-api
- name: Deploy
if: success()
run: |
kubectl apply -f deploy/
预期结果: 策略文档清晰,自动化门控防止在预算耗尽时进行高风险部署,团队在可靠性优先级上保持一致。
失败处理:
setup-prometheus-monitoring - 配置 Prometheus 收集用于 SLI 计算的指标configure-alerting-rules - 将 SLO 燃烧率告警与 Alertmanager 集成用于值班通知build-grafana-dashboards - 可视化 SLO 合规性和错误预算消耗write-incident-runbook - 在运行手册中包含 SLO 影响以优先级事故响应