关于 monitoring-expert
面向 Node.js 服务开发者的监控指南,涵盖日志、错误跟踪、健康检查和指标,并提供代码示例与排查命令。
主要功能
- 01配置 Pino 日志
- 02使用 Sentry 跟踪错误
- 03设置 HTTP 健康检查
- 04收集 Prometheus 指标
- 05查看日志与资源的命令
适用场景
- 01为 Node.js 服务添加结构化日志
- 02使用 Sentry 跟踪应用错误
- 03提供健康检查和指标端点
- 04排查日志与主机资源使用情况
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ duck4nh/ antigravity-kit --skill monitoring-expert安装后在 Claude Code 中输入/monitoring-expert确认已加载。
README 原文English
来自项目 READMEREADME 暂无中文译文,以下为英文原文;中文介绍见上方。
Monitoring Expert
Logging (Node.js)
// Using Pino (fast)
import pino from 'pino';
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: process.env.NODE_ENV !== 'production'
? { target: 'pino-pretty' }
: undefined
});
// Usage
logger.info({ userId: 123 }, 'User logged in');
logger.error({ err, requestId }, 'Failed to process');
// Express middleware
import pinoHttp from 'pino-http';
app.use(pinoHttp({ logger }));
Error Tracking (Sentry)
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV,
tracesSampleRate: 0.1,
});
// Capture errors
try {
riskyOperation();
} catch (error) {
Sentry.captureException(error);
throw error;
}
// Express error handler
app.use(Sentry.Handlers.errorHandler());
Health Checks
// /health endpoint
app.get('/health', async (req, res) => {
const health = {
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
checks: {
database: await checkDatabase(),
redis: await checkRedis(),
}
};
const isHealthy = Object.values(health.checks).every(v => v === 'ok');
res.status(isHealthy ? 200 : 503).json(health);
});
async function checkDatabase() {
try {
await prisma.$queryRaw`SELECT 1`;
return 'ok';
} catch { return 'error'; }
}
Metrics (Prometheus)
import promClient from 'prom-client';
// Default metrics
promClient.collectDefaultMetrics();
// Custom metrics
const httpRequestDuration = new promClient.Histogram({
name: 'http_request_duration_seconds',
help: 'HTTP request duration',
labelNames: ['method', 'route', 'status'],
});
// Middleware
app.use((req, res, next) => {
const end = httpRequestDuration.startTimer();
res.on('finish', () => {
end({ method: req.method, route: req.path, status: res.statusCode });
});
next();
});
// Endpoint
app.get('/metrics', async (req, res) => {
res.set('Content-Type', promClient.register.contentType);
res.end(await promClient.register.metrics());
});
Quick Debug Commands
# Check logs
journalctl -u myapp -f --since "1 hour ago"
docker logs -f --tail 100 container_name
# Check resources
htop
df -h
free -m
# Network
netstat -tlnp
ss -tlnp
curl -I http://localhost:3000/health
评论
支持 Markdown