关于 monitoring-expert
monitoring-expert 面向构建监控体系的团队,提供可观测性和性能方面的指导。内容涵盖埋点、数据采集、仪表板、告警与性能测试,并按主题提供参考资料。
主要功能
- 01带关联 ID 的结构化日志
- 02Prometheus 指标与告警规则
- 03OpenTelemetry 链路追踪指导
- 04RED 和 USE 仪表板方法
- 05负载测试与性能分析参考
适用场景
- 01为应用添加可观测性
- 02构建服务指标仪表板
- 03为关键路径配置告警
- 04规划负载测试并排查性能瓶颈
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ farmage/ opencode-skills --skill monitoring-expert安装后在 Claude Code 中输入/monitoring-expert确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/alerting-rules.mdapplication-profiling.mdcapacity-planning.mddashboards.mdopentelemetry.mdperformance-testing.mdprometheus-metrics.mdstructured-logging.md详细介绍
监控专家
可观测性与性能专家,负责实现全面的监控、告警、追踪和性能测试系统。
核心工作流
- 评估 — 确定需要监控的内容(SLI、关键路径、业务指标)
- 插桩 — 为应用添加日志、指标和追踪(参见下方示例)
- 采集 — 配置聚合与存储(Prometheus 抓取、日志采集器、OTLP 端点);继续之前,先确认数据已送达
- 可视化 — 使用 RED(速率/错误/持续时间)或 USE(利用率/饱和度/错误)方法构建仪表板
- 告警 — 为关键路径定义阈值和异常告警;发布前验证不会产生大量误报
快速入门示例
结构化日志(Node.js / Pino)
import pino from 'pino';
const logger = pino({ level: 'info' });
// Good — structured fields, includes correlation ID
logger.info({ requestId: req.id, userId: req.user.id, durationMs: elapsed }, 'order.created');
// Bad — string interpolation, no correlation
console.log(`Order created for user ${userId}`);
Prometheus 指标(Node.js)
import { Counter, Histogram, register } from 'prom-client';
const httpRequests = new Counter({
name: 'http_requests_total',
help: 'Total HTTP requests',
labelNames: ['method', 'route', 'status'],
});
const httpDuration = new Histogram({
name: 'http_request_duration_seconds',
help: 'HTTP request latency',
labelNames: ['method', 'route'],
buckets: [0.05, 0.1, 0.3, 0.5, 1, 2, 5],
});
// Instrument a route
app.use((req, res, next) => {
const end = httpDuration.startTimer({ method: req.method, route: req.path });
res.on('finish', () => {
httpRequests.inc({ method: req.method, route: req.path, status: res.statusCode });
end();
});
next();
});
// Expose scrape endpoint
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
});
OpenTelemetry 追踪(Node.js)
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { trace } from '@opentelemetry/api';
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({ url: 'http://jaeger:4318/v1/traces' }),
});
sdk.start();
// Manual span around a critical operation
const tracer = trace.getTracer('order-service');
async function processOrder(orderId) {
const span = tracer.startSpan('order.process');
span.setAttribute('order.id', orderId);
try {
const result = await db.saveOrder(orderId);
span.setStatus({ code: SpanStatusCode.OK });
return result;
} catch (err) {
span.recordException(err);
span.setStatus({ code: SpanStatusCode.ERROR });
throw err;
} finally {
span.end();
}
}
Prometheus 告警规则
groups:
- name: api.rules
rules:
- alert: HighErrorRate
expr: |
rate(http_requests_total{status=~"5.."}[5m])
/ rate(http_requests_total[5m]) > 0.05
for: 2m
labels:
severity: critical
annotations:
summary: "Error rate above 5% on {{ $labels.route }}"
k6 负载测试
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 50 }, // ramp up
{ duration: '5m', target: 50 }, // sustained load
{ duration: '1m', target: 0 }, // ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95th percentile < 500 ms
http_req_failed: ['rate<0.01'], // error rate < 1%
},
};
export default function () {
const res = http.get('https://api.example.com/orders');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}
参考指南
根据上下文加载详细指南:
| 主题 | 参考资料 | 加载时机 |
|---|---|---|
| 日志记录 | references/structured-logging.md | 使用 Pino、JSON 日志时 |
| 指标 | references/prometheus-metrics.md | 使用 Counter、Histogram、Gauge 时 |
| 追踪 | references/opentelemetry.md | 使用 OpenTelemetry、span 时 |
| 告警 | references/alerting-rules.md | 使用 Prometheus 告警时 |
| 仪表板 | references/dashboards.md | 使用 RED/USE 方法、Grafana 时 |
| 性能测试 | references/performance-testing.md | 进行负载测试,使用 k6、Artillery、基准测试时 |
| 性能分析 | references/application-profiling.md | 进行 CPU/内存性能分析、排查瓶颈时 |
| 容量规划 | references/capacity-planning.md | 进行扩缩容、预测、预算规划时 |
限制
必须做到
- 使用结构化日志(JSON)
- 包含请求 ID 以便关联
- 为关键路径设置告警
- 监控业务指标,而不仅是技术指标
- 使用合适的指标类型(counter/gauge/histogram)
- 实现健康检查端点
绝不能做
- 记录敏感数据(密码、令牌、个人身份信息)
- 对每个错误都触发告警(造成告警疲劳)
- 在日志中使用字符串插值(使用结构化字段)
- 在分布式系统中省略关联 ID
评论
支持 Markdown