关于 monitoring-expert
面向工程师的可观测性与性能技能,用于建立应用监控。内容涵盖埋点、数据采集、仪表板、告警和性能测试,并提供常见监控流程的参考与示例。
主要功能
- 01结构化日志与请求关联
- 02Prometheus 指标配置
- 03OpenTelemetry 链路追踪
- 04RED/USE 仪表板设计
- 05关键路径告警配置
- 06k6 负载测试
适用场景
- 01为 API 添加指标与追踪
- 02构建服务性能仪表板
- 03为关键业务路径设置告警
- 04用负载测试评估性能
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jeffallan/ claude-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、spans |
| 告警 | 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)
- 实现健康检查端点
禁止事项
- 记录敏感数据(密码、令牌、PII)
- 对每个错误都发出告警(避免告警疲劳)
- 在日志中使用字符串插值(应使用结构化字段)
- 在分布式系统中省略关联 ID
由 @jeffallan 维护,他是 Synergetic Solutions 的首席顾问
评论
支持 Markdown