mcpField
M
monitoring-expert

monitoring-expert

github.com/farmage云与 DevOps218287 次安装5 周前更新
代码仓库

关于 monitoring-expert

monitoring-expert 面向构建监控体系的团队,提供可观测性和性能方面的指导。内容涵盖埋点、数据采集、仪表板、告警与性能测试,并按主题提供参考资料。

主要功能

  1. 01带关联 ID 的结构化日志
  2. 02Prometheus 指标与告警规则
  3. 03OpenTelemetry 链路追踪指导
  4. 04RED 和 USE 仪表板方法
  5. 05负载测试与性能分析参考

适用场景

  1. 01为应用添加可观测性
  2. 02构建服务指标仪表板
  3. 03为关键路径配置告警
  4. 04规划负载测试并排查性能瓶颈

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/farmage/opencode-skills --skill monitoring-expert

安装后在 Claude Code 中输入/monitoring-expert确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
alerting-rules.md
application-profiling.md
capacity-planning.md
dashboards.md
opentelemetry.md
performance-testing.md
prometheus-metrics.md
structured-logging.md

详细介绍

监控专家

可观测性与性能专家,负责实现全面的监控、告警、追踪和性能测试系统。

核心工作流

  1. 评估 — 确定需要监控的内容(SLI、关键路径、业务指标)
  2. 插桩 — 为应用添加日志、指标和追踪(参见下方示例)
  3. 采集 — 配置聚合与存储(Prometheus 抓取、日志采集器、OTLP 端点);继续之前,先确认数据已送达
  4. 可视化 — 使用 RED(速率/错误/持续时间)或 USE(利用率/饱和度/错误)方法构建仪表板
  5. 告警 — 为关键路径定义阈值和异常告警;发布前验证不会产生大量误报

快速入门示例

结构化日志(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