agentleFS
28672032962867203296/hermes-agent/skills/devops/monitoring-observability/SKILL.md

monitoring-observability

Skill0 starsChanged 3 months agoMITView on GitHub

可观测性:Prometheus指标、Grafana仪表盘、OpenTelemetry追踪、日志.

6 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

可观测性

三大支柱

Metrics(指标):数字,CPU/内存/QPS/延迟
Traces(追踪):请求全链路,A→B→C 各花了多久
Logs(日志):文本,"用户登录失败"

日志最佳实践

import logging

# 结构化日志(JSON格式,方便机器解析)
logging.basicConfig(
    format='{"time":"%(asctime)s","level":"%(levelname)s","msg":"%(message)s"}',
    level=logging.INFO
)

logging.info("用户登录", extra={"user_id": 42, "ip": "1.2.3.4"})

# 关键原则:
# 1. INFO:关键业务流程(用户登录、下单、支付)
# 2. WARNING:可能需要关注(重试、降级)
# 3. ERROR:需要立即处理(支付失败、数据库连接断开)
# 4. 永远不记录密码/密钥/身份证号
# 5. 每条日志带 request_id → 关联同一个请求的所有操作

指标监控

# 用 prometheus_client
from prometheus_client import Counter, Histogram, Gauge

# 计数器(只增不减)
requests_total = Counter('http_requests_total', '总请求数',
                         ['method', 'endpoint'])
requests_total.labels(method='GET', endpoint='/api/users').inc()

# 直方图(分布统计)
request_duration = Histogram('http_request_duration_seconds', '请求耗时')
@request_duration.time()
def handle_request():
    pass

# 仪表盘(可增可减)
active_connections = Gauge('active_connections', '当前连接数')
active_connections.inc()   # +1
active_connections.dec()   # -1

告警规则

不要告警过多 → 告警疲劳 → 忽略真正重要的

关键告警(必须响应):
- 服务挂了(健康检查失败)
- 错误率 > 5%
- P95 延迟 > 1s
- 磁盘使用 > 85%

非关键告警(白天看看):
- CPU 使用 > 80%
- 内存使用 > 80%
- 慢查询 > 1s

全链路追踪

一个请求进来:
  API网关(2ms) → 认证服务(15ms) → 业务服务(50ms) → 数据库(3ms)
                                               → Redis(1ms)

OpenTelemetry 自动追踪这个链路。
看哪个环节慢 → 优化那个。

轻量方案(无需 Prometheus/Grafana)

# 最简单的健康检查端点
@app.get("/health")
def health():
    return {"status": "ok", "timestamp": time.time()}

# 简单的请求日志中间件
@app.middleware("http")
async def log_requests(request, call_next):
    start = time.perf_counter()
    response = await call_next(request)
    duration = time.perf_counter() - start
    logger.info(f"{request.method} {request.url.path} {response.status_code} {duration:.3f}s")
    return response

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.