SKILL.md
唯讀
名稱
monitoring-expert
描述
配置監控系統、實作結構化日誌管道、建立 Prometheus/Grafana 儀表板、定義警示規則,以及檢測分散式追蹤。實作 Prometheus/Grafana 堆疊、執行負載測試、進行應用程式效能分析,並規劃基礎設施容量。適用於設定應用程式監控、為服務加入可觀測性、使用日誌/指標/追蹤除錯生產問題、使用 k6 或 Artillery 執行負載測試、分析 CPU/記憶體瓶頸,或預測容量需求。
監控專家
可觀測性與效能專家,實作全面的監控、警示、追蹤與效能測試系統。
核心工作流程
- 評估 — 識別需要監控的項目(SLI、關鍵路徑、業務指標)
- 檢測 — 在應用程式中加入日誌、指標與追蹤(請參閱下方範例)
- 收集 — 設定彙總與儲存(Prometheus 抓取、日誌轉送器、OTLP 端點);確認資料送達後再繼續
- 視覺化 — 使用 RED(速率/錯誤/持續時間)或 USE(使用率/飽和度/錯誤)方法建立儀表板
- 警示 — 在關鍵路徑上定義閾值與異常警示;出貨前驗證無假陽性洪水
快速入門範例
結構化日誌(Node.js / Pino)
import pino from 'pino';
const logger = pino({ level: 'info' });
// 良好 — 結構化欄位,包含關聯 ID
logger.info({ requestId: req.id, userId: req.user.id, durationMs: elapsed }, 'order.created');
// 不良 — 字串插值,無關聯
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],
});
// 檢測路由
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();
});
// 公開抓取端點
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();
// 在關鍵操作周圍手動建立 span
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 }, // 逐步增加
{ duration: '5m', target: 50 }, // 持續負載
{ duration: '1m', target: 0 }, // 逐步減少
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95 百分位數 < 500 ms
http_req_failed: ['rate<0.01'], // 錯誤率 < 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




