← 返回题库
中级

生成Prometheus格式指标

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    metrics_csv = open_url(____).read()
    request_logs_csv = open_url(____).read()
    import pandas as pd
    from io import StringIO
    metrics = pd.read_csv(StringIO(____))
    logs = pd.read_csv(StringIO(____))
    lines = []
    lines.append(____)
    lines.append(____)
    for p, grp in logs.groupby(____):
        lines.append('____' + p + '____' + str(len(____)))
    lines.append(____)
    lines.append(____)
    for _, row in metrics.iterrows():
        lines.append('____' + str(____) + '____' + str(____))
    lines.append(____)
    lines.append(____)
    for _, row in metrics.iterrows():
        lines.append('____' + str(____) + '____' + str(____))
    shown = ____
    for line in lines:
        if shown < ____:
            print(____)
            shown += ____
    print('____' + str(len(____)) + '____')

示例

输入
solve()
期望输出
# HELP http_requests_total Total HTTP requests
# TYPE http_requests_total counter
http_requests_total{path="/api/v1/orders"} 16
http_requests_total{path="/api/v1/payments"} 9
http_requests_total{path="/api/v1/products"} 7
http_requests_total{path="/api/v1/search"} 7
http_requests_total{path="/api/v1/users"} 11
# HELP http_request_latency_p99_ms P99 latency in ms
# TYPE http_request_latency_p99_ms gauge
http_request_latency_p99_ms{service="user-service"} 496
http_request_latency_p99_ms{service="product-service"} 491
http_request_latency_p99_ms{service="order-service"} 488
http_request_latency_p99_ms{service="payment-service"} 498
# HELP http_errors_total Total HTTP errors
# TYPE http_errors_total counter
http_errors_total{service="user-service"} 8
http_errors_total{service="product-service"} 6
http_errors_total{service="order-service"} 8
... (total 19 metric lines)
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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