Files
wx_service/docs/ops/observability_baseline.md
T
2026-02-28 16:37:37 +08:00

44 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 日志收集与基础监控落地
对应 issue`#11 [P1][T8] 日志收集与基础监控落地`
## 1. 结构化日志
- 中间件:`internal/observability/http.go::RequestLogMiddleware`
- 每个请求输出一条 JSON 日志,包含:
- `request_id`
- `method/path/status`
- `latency_ms`
- `client_ip`
- `uid`(若已鉴权)
- `errors`(若有)
## 2. 核心指标
- 采集器:`internal/observability/collector.go`
- 暴露接口:`GET /metrics/basic`
- 指标字段:
- `total_requests`
- `client_errors`
- `server_errors`
- `client_error_rate_pct`
- `server_error_rate_pct`
- `avg_latency_ms`
- `max_latency_ms`
## 3. 基础告警阈值
- 脚本:`scripts/ops/check_basic_metrics.sh`
- 默认阈值:
- `SERVER_ERROR_RATE_THRESHOLD=5`%
- `AVG_LATENCY_THRESHOLD_MS=800`ms
- 触发后行为:
- 返回非 0
- 若配置 `OPS_ALERT_WEBHOOK` 则发送告警
推荐 cron(每 5 分钟):
```bash
*/5 * * * * METRICS_URL=http://127.0.0.1:8080/metrics/basic SERVER_ERROR_RATE_THRESHOLD=5 AVG_LATENCY_THRESHOLD_MS=800 OPS_ALERT_WEBHOOK="https://example.com/webhook" /path/to/wx_service/scripts/ops/check_basic_metrics.sh >> /var/log/wx_service-metrics-check.log 2>&1
```