上周五凌晨三点,我被一通电话叫醒——生产环境的 AI 对话接口彻底崩溃。用户反馈"服务不可用",监控大屏一片红色报警。登录服务器一看,日志里清一色的 ConnectionError: timeout after 30s。紧急排查后发现,供应商的 API 在那个时间段可用性骤降至 95%,远低于宣称的 99.9%。这次事故让我深刻意识到:选 AI API 不只是看价格和模型能力,SLA(服务等级协议)才是生产环境的生命线。
本文将带你深入理解 AI API 的服务等级协议核心指标,解析 HolySheep AI 等主流平台在 SLA 保障上的差异,并通过真实报错案例教你构建高可用的 AI 调用架构。文中所有代码示例均基于 HolySheep API 进行演示,国内直连延迟低于 50ms,支持微信/支付宝充值,汇率 ¥1=$1 无损。
一、为什么 AI API 的 SLA 比想象中更重要
很多开发者在选型时只盯着模型能力和价格,却忽略了 SLA 这个关键指标。我见过太多团队因为 API 可用性不达标导致业务中断的案例。根据我的经验,一个日均调用量 10 万次的 AI 应用:
- 可用性从 99% 提升到 99.9%:每年可减少约 87 小时的停机时间
- 可用性从 99.9% 提升到 99.99%:每年可减少约 52 分钟的停机时间
- 每次 1 分钟的服务中断,可能导致 10-30 个用户流失
更重要的是,AI API 的 SLA 不仅仅关乎"能不能用",还直接影响响应延迟和错误率。以下是 2026 年主流 AI 平台的服务等级对比:
| 平台 | 基础 SLA | 延迟承诺 | 错误率上限 |
|---|---|---|---|
| HolySheep AI | 99.9% | <50ms(国内) | <0.1% |
| 某国际大厂 | 99.5% | 200-500ms | <0.5% |
| 某国产平台 | 99% | 100-300ms | <1% |
二、AI API SLA 的核心指标解析
1. 可用性(Availability)
这是最核心的指标,通常以"9"的个数来衡量。一个 99.9% 的 SLA 意味着每月允许的停机时间不超过 43.8 分钟。但要注意,SLA 承诺的可用性是平台层面的,实际用户体验还受网络链路影响。
2. 延迟(Latency)
AI API 的延迟由多个部分组成:
- 首字节时间(TTFB):从请求发出到收到第一个字节的时间
- Token 生成速率:每秒生成的 token 数量,影响整体响应时间
- 网络链路延迟:服务器到客户端的物理延迟
我之前用的某国际大厂 API,虽然模型能力强,但国内访问延迟经常超过 500ms,极其影响用户体验。切换到 HolySheep AI 后,同一接口延迟稳定在 50ms 以内,加载速度快了将近 10 倍。
3. 错误率与熔断机制
一个成熟的 AI API 平台会提供完善的熔断机制,当系统负载过高时主动返回 503 错误而不是超时等待。HolySheep AI 在高峰期的错误率控制在 0.1% 以内,并且提供清晰的错误码体系,方便开发者进行针对性处理。
三、构建高可用 AI 调用架构实战
下面我将分享一个完整的生产级 Python SDK 封装示例,集成了重试机制、超时控制、熔断降级等高可用特性。
"""
HolySheep AI 高可用 SDK 封装
基于服务等级协议的智能路由与容错处理
"""
import time
import asyncio
import aiohttp
from typing import Optional, Dict, Any, Callable
from dataclasses import dataclass
from enum import Enum
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class CircuitState(Enum):
CLOSED = "closed" # 正常状态
OPEN = "open" # 熔断状态
HALF_OPEN = "half_open" # 半开状态
@dataclass
class SLAConfig:
"""SLA 配置参数"""
timeout: float = 30.0 # 超时时间(秒)
max_retries: int = 3 # 最大重试次数
retry_delay: float = 1.0 # 重试延迟(秒)
circuit_threshold: int = 5 # 熔断阈值(连续错误次数)
circuit_duration: float = 60.0 # 熔断恢复时间(秒)
error_rate_threshold: float = 0.01 # 错误率阈值(1%)
class HolySheepAIClient:
"""HolySheep AI 高可用客户端"""
def __init__(
self,
api_key: str,
base_url: str = "https://api.holysheep.ai/v1",
sla_config: Optional[SLAConfig] = None
):
self.api_key = api_key
self.base_url = base_url
self.sla = sla_config or SLAConfig()
# 熔断器状态
self._circuit_state = CircuitState.CLOSED
self._failure_count = 0
self._circuit_open_time = 0
self._total_requests = 0
self._failed_requests = 0
# Semaphore 控制并发
self._semaphore = asyncio.Semaphore(100)
async def chat_completion(
self,
messages: list,
model: str = "gpt-4.1",
temperature: float = 0.7,
max_tokens: int = 2048
) -> Dict[str, Any]:
"""
对话补全 API,集成 SLA 保障机制
关键 SLA 承诺(HolySheep AI):
- 可用性:99.9%
- 国内延迟:<50ms
- 错误率:<0.1%
"""
# 检查熔断器状态
if self._should_open_circuit():
raise ServiceUnavailableError(
f"Circuit breaker is OPEN. Service degraded. "
f"Will retry after {self._time_until_retry():.1f}s"
)
async with self._semaphore:
last_error = None
for attempt in range(self.sla.max_retries):
try:
result = await self._execute_request(
messages=messages,
model=model,
temperature=temperature,
max_tokens=max_tokens
)
self._on_success()
return result
except (TimeoutError, ConnectionError) as e:
last_error = e
logger.warning(
f"Attempt {attempt + 1}/{self.sla.max_retries} failed: {e}"
)
if attempt < self.sla.max_retries - 1:
# 指数退避
delay = self.sla.retry_delay * (2 ** attempt)
await asyncio.sleep(delay)
# 检查是否应该触发熔断
if self._should_open_circuit():
break
except ServiceUnavailableError as e:
# 服务端明确不可用,不重试
self._on_failure()
raise
self._on_failure()
raise RetryExhaustedError(
f"All {self.sla.max_retries} attempts failed. Last error: {last_error}"
)
async def _execute_request(
self,
messages: list,
model: str,
temperature: float,
max_tokens: int
) -> Dict[str, Any]:
"""执行实际的 API 请求"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
timeout = aiohttp.ClientTimeout(
total=self.sla.timeout,
connect=10.0 # 连接超时单独设置
)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=payload
) as response:
self._total_requests += 1
if response.status == 200:
return await response.json()
elif response.status == 401:
# HolySheep API 返回 401 表示认证失败
error_detail = await response.text()
raise AuthenticationError(
f"Invalid API key or unauthorized access. Detail: {error_detail}"
)
elif response.status == 429:
# 请求频率超限
retry_after = response.headers.get("Retry-After", "60")
raise RateLimitError(
f"Rate limit exceeded. Retry after {retry_after}s. "
f"HolySheep AI supports 1000 req/min on standard plan."
)
elif response.status == 500:
raise InternalServerError(
"HolySheep AI internal server error. This will be retried."
)
elif response.status == 503:
raise ServiceUnavailableError(
"Service temporarily unavailable. "
"HolySheep AI SLA: 99.9% availability, retrying..."
)
else:
error_detail = await response.text()
raise APIError(
f"API returned error {response.status}: {error_detail}"
)
def _should_open_circuit(self) -> bool:
"""判断是否应该打开熔断器"""
current_time = time.time()
if self._circuit_state == CircuitState.OPEN:
# 检查是否应该进入半开状态
if current_time - self._circuit_open_time >= self.sla.circuit_duration:
self._circuit_state = CircuitState.HALF_OPEN
logger.info("Circuit breaker: OPEN -> HALF_OPEN")
return False
return True
elif self._circuit_state == CircuitState.HALF_OPEN:
return False
# CLOSED 状态:检查是否应该打开
if self._failure_count >= self.sla.circuit_threshold:
self._circuit_state = CircuitState.OPEN
self._circuit_open_time = current_time
logger.warning(
f"Circuit breaker: CLOSED -> OPEN. "
f"Failures: {self._failure_count}, threshold: {self.sla.circuit_threshold}"
)
return True
return False
def _on_success(self):
"""请求成功处理"""
if self._circuit_state == CircuitState.HALF_OPEN:
logger.info("Circuit breaker: HALF_OPEN -> CLOSED")
self._circuit_state = CircuitState.CLOSED
self._failure_count = 0
def _on_failure(self):
"""请求失败处理"""
self._failure_count += 1
self._failed_requests += 1
error_rate = self._failed_requests / max(self._total_requests, 1)
if error_rate > self.sla.error_rate_threshold:
logger.warning(
f"Error rate {error_rate:.2%} exceeds threshold "
f"{self.sla.error_rate_threshold:.2%}"
)
def _time_until_retry(self) -> float:
"""计算距离熔断恢复的时间"""
if self._circuit_state != CircuitState.OPEN:
return 0
elapsed = time.time() - self._circuit_open_time
return max(0, self.sla.circuit_duration - elapsed)
自定义异常类
class APIError(Exception):
pass
class AuthenticationError(APIError):
"""401 认证错误 - 常见原因见下文排查章节"""
pass
class RateLimitError(APIError):
"""429 频率限制错误"""
pass
class ServiceUnavailableError(APIError):
"""503 服务不可用错误"""
pass
class InternalServerError(APIError):
"""500 内部服务器错误"""
pass
class RetryExhaustedError(APIError):
"""重试次数耗尽"""
pass
四、实际使用示例与价格对比
下面是调用 HolySheep API 的完整示例,覆盖了常见的使用场景。我个人在生产环境中使用 HolySheep 已经有三个月,最大的感受是:稳定、便宜、响应快。
"""
HolySheep AI 实际调用示例
包含完整的错误处理和 SLA 监控
"""
import asyncio
import logging
from datetime import datetime
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
async def main():
# 初始化客户端
client = HolySheepAIClient(
api_key="YOUR_HOLYSHEEP_API_KEY", # 替换为你的 HolySheep API Key
sla_config=SLAConfig(
timeout=30.0,
max_retries=3,
circuit_threshold=5
)
)
# 监控指标
start_time = datetime.now()
total_calls = 0
successful_calls = 0
failed_calls = 0
try:
# 示例 1: 基础对话调用
logger.info("=== 示例 1: 基础对话调用 ===")
response = await client.chat_completion(
messages=[
{"role": "system", "content": "你是一个专业的技术顾问"},
{"role": "user", "content": "解释一下什么是 AI API 的 SLA"}
],
model="gpt-4.1",
temperature=0.7
)
logger.info(f"响应 ID: {response.get('id')}")
logger.info(f"使用的模型: {response.get('model')}")
logger.info(f"Token 使用: {response.get('usage')}")
# 提取回复内容
content = response['choices'][0]['message']['content']
logger.info(f"AI 回复: {content[:100]}...")
successful_calls += 1
except AuthenticationError as e:
# 401 错误 - 立即终止,不重试
logger.error(f"🚨 认证失败: {e}")
logger.error("请检查 API Key 是否正确配置")
failed_calls += 1
except RateLimitError as e:
# 429 错误 - 等待后重试
logger.warning(f"⚠️ 请求频率超限: {e}")
logger.info("建议:HolySheep 标准版支持 1000 请求/分钟")
failed_calls += 1
except ServiceUnavailableError as e:
# 503 错误 - 服务端问题,可能需要降级
logger.error(f"🚨 服务不可用: {e}")
logger.info("建议:启用备用服务或显示降级消息")
failed_calls += 1
except RetryExhaustedError as e:
# 重试耗尽 - 最严重的情况
logger.critical(f"💥 所有重试失败: {e}")
logger.critical("建议:检查网络连接或联系 HolySheep 技术支持")
failed_calls += 1
finally:
total_calls += 1
elapsed = (datetime.now() - start_time).total_seconds()
# 打印 SLA 监控报告
logger.info("=" * 50)
logger.info("📊 SLA 监控报告")
logger.info(f"总调用次数: {total_calls}")
logger.info(f"成功次数: {successful_calls}")
logger.info(f"失败次数: {failed_calls}")
logger.info(f"成功率: {(successful_calls/total_calls)*100:.2f}%")
logger.info(f"总耗时: {elapsed:.2f}s")
logger.info(f"平均延迟: {elapsed/total_calls*1000:.2f}ms")
logger.info("=" * 50)
价格对比示例
def show_pricing_comparison():
"""
2026年主流模型 Output 价格对比($/MTok)
HolySheep AI 汇率优势:¥1=$1 无损
"""
pricing = {
"GPT-4.1": {"price": 8.00, "currency": "USD"},
"Claude Sonnet 4.5": {"price": 15.00, "currency": "USD"},
"Gemini 2.5 Flash": {"price": 2.50, "currency": "USD"},
"DeepSeek V3.2": {"price": 0.42, "currency": "USD"},
}
print("\n" + "=" * 50)
print("💰 2026年主流模型 Output 价格对比($/MTok)")
print("=" * 50)
for model, info in pricing.items():
print(f"{model:20s}: ${info['price']:>6.2f} / MTok")
print("-" * 50)
print("💡 HolySheep AI 优势:")
print(" - 汇率 ¥1=$1 无损(官方 ¥7.3=$1)")
print(" - 节省超过 85% 的汇率损失")
print(" - 支持微信/支付宝充值,即时到账")
print(" - 注册即送免费额度")
print("=" * 50)
if __name__ == "__main__":
asyncio.run(main())
show_pricing_comparison()
五、常见报错排查
根据我过去一年处理 AI API 问题的经验,80% 的报错都集中在以下几类。下面我详细讲解每个错误的成因、排查步骤和解决方案。
错误 1: 401 Unauthorized - 认证失败
报错信息:
AuthenticationError: Invalid API key or unauthorized access.
Detail: {"error": {"message": "Incorrect API key provided", "type": "invalid_request_error", "code": 401}}
常见原因:
- API Key 拼写错误或包含多余空格
- 使用了过期的 Key
- Key 没有该接口的访问权限
- 请求头 Authorization 格式错误
解决代码:
# ❌ 错误示例
headers = {
"Authorization": f"Bearer {api_key}", # 注意 Bearer 后面有多余空格
}
❌ 错误示例
headers = {
"Authorization": f"Token {api_key}", # 应该用 Bearer 而不是 Token
}
✅ 正确写法
headers = {
"Authorization": f"Bearer {api_key.strip()}", # 去除首尾空格
}
✅ 使用环境变量(更安全)
import os
headers = {
"Authorization": f"Bearer {os.environ.get('HOLYSHEEP_API_KEY', '').strip()}",
}
✅ 添加 Key 验证函数
def validate_api_key(api_key: str) -> bool:
"""
验证 API Key 格式是否正确
HolySheep API Key 格式:hs_ 开头,32位随机字符串
"""
if not api_key:
return False
if not api_key.startswith("hs_"):
return False
if len(api_key) < 40: # 包含前缀和密钥部分
return False
return True
使用前验证
api_key = "YOUR_HOLYSHEEP_API_KEY"
if not validate_api_key(api_key):
raise ValueError("Invalid API key format. Please check your HolySheep AI key.")
错误 2: ConnectionError: timeout - 连接超时
报错信息:
ConnectionError: TimeoutError occurred
- HTTPSConnectionPool(host='api.holysheep.ai', port=443):
Max retries exceeded with url: /v1/chat/completions
(Caused by ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection
object at 0x...>, 'Connection timed out after 30 seconds'))
aiohttp.client_exceptions.ClientConnectorError:
Cannot connect to host api.holysheep.ai:443
ssl:default [Connection timed out]
常见原因:
- 网络防火墙阻止了出站请求
- DNS 解析失败
- 代理服务器配置错误
- 服务端过载,响应缓慢
解决代码:
import asyncio
import aiohttp
import socket
from urllib.parse import urlparse
诊断函数
async def diagnose_connection(base_url: str):
"""诊断连接问题的完整流程"""
parsed = urlparse(base_url)
host = parsed.netloc
port = parsed.port or (443 if parsed.scheme == "https" else 80)
print(f"🔍 诊断连接: {host}:{port}")
# 1. DNS 解析测试
try:
ip = socket.gethostbyname(host)
print(f"✅ DNS 解析成功: {host} -> {ip}")
except socket.gaierror as e:
print(f"❌ DNS 解析失败: {e}")
return False
# 2. TCP 连接测试
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
try:
result = sock.connect_ex((host, port))
if result == 0:
print(f"✅ TCP 连接成功: {host}:{port}")
else:
print(f"❌ TCP 连接失败,错误码: {result}")
print("💡 建议:检查防火墙规则或代理设置")
sock.close()
except Exception as e:
print(f"❌ TCP 连接异常: {e}")
# 3. HTTPS 握手测试
timeout = aiohttp.ClientTimeout(total=10)
try:
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.get(f"https://{host}/v1/models") as resp:
print(f"✅ HTTPS 握手成功,状态码: {resp.status}")
except aiohttp.ClientConnectorError as e:
print(f"❌ HTTPS 连接失败: {e}")
print("💡 可能原因:代理设置错误 / 证书验证失败 / 防火墙阻止")
except asyncio.TimeoutError:
print(f"❌ 连接超时(10秒)")
print("💡 建议:")
print(" 1. 检查本地网络连接")
print(" 2. 确认防火墙允许出站 HTTPS")
print(" 3. 如果使用代理,配置环境变量:")
print(" export HTTPS_PROXY=http://proxy:8080")
return True
运行诊断
await diagnose_connection("https://api.holysheep.ai")
✅ 使用代理的正确方式
proxy = os.environ.get("HTTPS_PROXY", "http://proxy.example.com:8080")
connector = aiohttp.TCPConnector(ssl=False) # 如果遇到证书问题
async with aiohttp.ClientSession() as session:
async with session.get(
"https://api.holysheep.ai/v1/models",
proxy=proxy, # 通过代理访问
connector=connector
) as response:
print(await response.json())
错误 3: 429 Rate Limit Exceeded - 请求频率超限
报错信息:
RateLimitError: Rate limit exceeded. Retry after 60s.
HolySheep AI supports 1000 req/min on standard plan.
或者返回 header:
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1703012400
Retry-After: 60
常见原因:
- 并发请求数超过配额
- Token 用量超限
- 短时间内发送请求过于频繁
- 未实现请求队列化,高峰期瞬时并发过高
解决代码:
import asyncio
import time
from collections import deque
from typing import Optional
import aiohttp
class RateLimitHandler:
"""
智能速率限制器
基于滑动窗口算法,精确控制请求频率
"""
def __init__(self, max_requests: int = 1000, window_seconds: int = 60):
self.max_requests = max_requests
self.window_seconds = window_seconds
self.request_times: deque = deque()
self._lock = asyncio.Lock()
async def acquire(self, client: aiohttp.ClientSession) -> None:
"""
获取请求许可,自动限流
当速率接近限制时,会智能等待而不是直接拒绝
"""
async with self._lock:
now = time.time()
# 清理过期的时间戳
while self.request_times and self.request_times[0] < now - self.window_seconds:
self.request_times.popleft()
# 如果已达到限制,等待
if len(self.request_times) >= self.max_requests:
oldest = self.request_times[0]
wait_time = oldest + self.window_seconds - now
if wait_time > 0:
print(f"⏳ 速率限制:等待 {wait_time:.1f} 秒...")
await asyncio.sleep(wait_time)
# 再次清理
now = time.time()
while self.request_times and self.request_times[0] < now - self.window_seconds:
self.request_times.popleft()
# 记录本次请求
self.request_times.append(time.time())
def get_remaining(self) -> int:
"""获取剩余请求配额"""
now = time.time()
while self.request_times and self.request_times[0] < now - self.window_seconds:
self.request_times.popleft()
return max(0, self.max_requests - len(self.request_times))
class AdaptiveRateLimiter:
"""
自适应速率限制器
根据 429 响应自动调整请求频率
"""
def __init__(self):
self.base_rate = 800 # 基础速率(留 20% 余量)
self.current_rate = 800
self.min_rate = 100 # 最低速率保护
self.backoff_factor = 0.8 # 降级因子
self.recovery_factor = 1.1 # 恢复因子
def report_success(self):
"""成功处理,可适当提高速率"""
if self.current_rate < self.base_rate:
self.current_rate = min(
self.base_rate,
self.current_rate * self.recovery_factor
)
def report_rate_limit(self, retry_after: Optional[int] = None):
"""遇到 429,大幅降低速率"""
if retry_after:
# 使用服务端建议的等待时间
recommended_rate = self.max_requests / retry_after * 60
self.current_rate = min(self.current_rate, recommended_rate * 0.8)
else:
# 指数降级
self.current_rate = max(
self.min_rate,
self.current_rate * self.backoff_factor
)
print(f"📉 速率限制触发,当前速率调整为: {self.current_rate:.0f} req/min")
使用示例
async def rate_limited_request():
rate_limiter = AdaptiveRateLimiter()
for i in range(100):
try:
# 模拟请求
await asyncio.sleep(60 / rate_limiter.current_rate)
# response = await client.chat_completion(...)
rate_limiter.report_success()
print(f"✅ 请求 {i+1} 成功,当前速率: {rate_limiter.current_rate:.0f}")
except RateLimitError as e:
# 提取 Retry-After
retry_after = int(e.args[0].split("Retry after ")[1].split("s")[0])
rate_limiter.report_rate_limit(retry_after)
# 等待后重试
await asyncio.sleep(retry_after)
六、生产环境 SLA 监控实战
作为一个有三年 AI 系统运维经验的工程师,我强烈建议在生产环境部署完整的 SLA 监控体系。以下是我在 HolySheep AI 生产环境中使用的监控方案:
"""
SLA 监控与告警系统
实时追踪可用性、延迟、错误率等核心指标
"""
import time
import asyncio
from dataclasses import dataclass, field
from typing import List, Optional
from collections import defaultdict
import logging
logger = logging.getLogger(__name__)
@dataclass
class SLAReport:
"""SLA 报告数据结构"""
total_requests: int = 0
successful_requests: int = 0
failed_requests: int = 0
total_latency_ms: float = 0.0
max_latency_ms: float = 0.0
min_latency_ms: float = float('inf')
errors_by_type: dict = field(default_factory=lambda: defaultdict(int))
@property
def availability(self) -> float:
"""计算可用性百分比"""
if self.total_requests == 0:
return 100.0
return (self.successful_requests / self.total_requests) * 100
@property
def average_latency_ms(self) -> float:
"""计算平均延迟"""
if self.successful_requests == 0:
return 0.0
return self.total_latency_ms / self.successful_requests
@property
def error_rate(self) -> float:
"""计算错误率"""
if self.total_requests == 0:
return 0.0
return (self.failed_requests / self.total_requests) * 100
def meets_sla(self, target_availability: float = 99.9) -> bool:
"""检查是否满足 SLA 目标"""
return self.availability >= target_availability
class SLAMonitor:
"""
SLA 监控器
关键 SLA 指标(以 HolySheep AI 为例):
- 可用性: 99.9%
- P50 延迟: <30ms
- P95 延迟: <100ms
- P99 延迟: <200ms
- 错误率: <0.1%
"""
def __init__(
self,
window_seconds: int = 300, # 5分钟滑动窗口
sla_targets: Optional[dict] = None
):
self.window_seconds = window_seconds
self.sla_targets = sla_targets or {
"availability": 99.9,
"p50_latency": 30,
"p95_latency": 100,
"p99_latency": 200,
"error_rate": 0.1
}
self._records: List[tuple] = [] # (timestamp, latency_ms, success, error_type)
self._lock = asyncio.Lock()
async def record_request(
self,
latency_ms: float,
success: bool,
error_type: Optional[str] = None
):
"""记录单个请求"""
async with self._lock:
now = time.time()
self._records.append((now, latency_ms, success, error_type))
# 清理过期记录
cutoff = now - self.window_seconds
self._records = [(t, l, s, e) for t, l, s, e in self._records if t >= cutoff]
async def get_report(self) -> SLAReport:
"""生成当前窗口的 SLA 报告"""
async with self._lock:
now = time.time()
cutoff = now - self.window_seconds
active_records = [
(t, l, s, e) for t, l, s, e in self._records if t >= cutoff
]
report = SLAReport()
report.total_requests = len(active_records)
latencies = []
for timestamp, latency, success, error_type in active_records:
if success:
report.successful_requests += 1
report.total_latency_ms += latency
latencies.append(latency)
report.max_latency_ms = max(report.max_latency_ms, latency)
report.min_latency_ms = min(report.min_latency_ms, latency)
else:
report.failed_requests += 1
if error_type:
report.errors_by_type[error_type] += 1
return report
async def check_and_alert(self):
"""检查 SLA 状态,触发告警"""
report = await self.get_report()
alerts = []
# 可用性检查
if report.availability < self.sla_targets["availability"]:
alerts.append(
f"🚨 [CRITICAL] 可用性告警: {report.availability:.2f}% "
f"< {self.sla_targets['availability']:.1f}% SLA目标"
)
# P95 延迟检查
if report.successful_requests > 0:
sorted_latencies = sorted(latencies)
p95_idx = int(len(sorted_latencies) * 0.95)
p95_latency = sorted_latencies[p95_idx] if p95_idx < len(sorted_latencies) else 0
if p95_latency > self.sla_targets["p95_latency"]:
alerts.append(
f"⚠️ [WARNING] P95延迟告警: {p95_latency:.0f}ms "
f"> {self.sla_targets['p95_latency']:.0f}ms SLA目标"
)
# 错误率检查
if report.error_rate > self.sla_targets["error_rate"]:
alerts.append(
f"🚨 [CRITICAL] 错误率告警: {report.error_rate:.2f}% "
f"> {self.sla_targets['error_rate']:.2f}% SLA目标"
)
# 打印告警
for alert in alerts:
logger.critical(alert)
return alerts
使用示例
async def demo_sla