← 返回题库
中级

实现基于告警阈值的错误率检测

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    alerts_csv = open_url(____).read()
    request_logs_csv = open_url(____).read()
    import pandas as pd
    from io import StringIO
    alerts = pd.read_csv(StringIO(____))
    logs = pd.read_csv(StringIO(____))
    RULES = [
        {'____':'____','____':'____','____':____,'____':____},
        {'____':'____','____':'____','____':____,'____':____},
    ]
    def check_alert(____):
        fired = []
        if rule['____'] == '____':
            for path, grp in logs_df.groupby(____):
                rate = (____).mean()
                if rate > rule['____']:
                    fired.append({'____':path,'____':round(____),'____':rule['____']})
        elif rule['____'] == '____':
            for path, grp in logs_df.groupby(____):
                vals = grp['____'].sort_values()
                p99 = float(vals.iloc[int(len(____)*____)]) if len(____) > ____ else ____
                if p99 > rule['____']:
                    fired.append(____)
        return fired
    print(____)
    print(alerts[['____','____','____','____','____']].to_string(____))
    print(____)
    for rule in RULES:
        fired = check_alert(____)
        for f in fired:
            print(____)

示例

输入
solve()
期望输出
已有告警:
        service  alert_type  threshold  current_value   status
payment-service  error_rate       0.05           0.12 resolved
  order-service p99_latency     500.00         720.00   firing
   user-service  error_rate       0.05           0.07 resolved
 search-service p99_latency     300.00         450.00   firing
实时检测:
  ALERT [error_rate] /api/v1/payments: 0.111 > 0.1
  ALERT [error_rate] /api/v1/search: 0.286 > 0.1
  ALERT [error_rate] /api/v1/users: 0.273 > 0.1
  ALERT [p99_latency] /api/v1/payments: 423.0 > 400
  ALERT [p99_latency] /api/v1/products: 457.0 > 400
  ALERT [p99_latency] /api/v1/search: 438.0 > 400
  ALERT [p99_latency] /api/v1/users: 473.0 > 400
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码