feat: 建立监控告警体系和生产环境配置
阶段三:建立监控告警体系 - 集成Sentry错误监控:安装依赖,创建配置文件,初始化Sentry - 配置性能监控:创建监控工具类,实现健康检查API - 更新环境变量模板,添加Sentry和数据库配置 阶段四:配置生产环境 - 创建生产环境变量模板 - 创建Dockerfile和docker-compose.prod.yml - 创建备份和恢复脚本 - 设置脚本执行权限
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { monitor } from '@/lib/monitoring';
|
||||
|
||||
export async function GET() {
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const health = {
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
version: process.env.npm_package_version || '0.1.0',
|
||||
environment: process.env.NODE_ENV,
|
||||
memory: {
|
||||
heapUsed: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
|
||||
heapTotal: Math.round(process.memoryUsage().heapTotal / 1024 / 1024),
|
||||
rss: Math.round(process.memoryUsage().rss / 1024 / 1024),
|
||||
},
|
||||
metrics: {
|
||||
responseTime: monitor.getStats('response_time'),
|
||||
requestCount: monitor.getCount('requests'),
|
||||
},
|
||||
checks: {
|
||||
database: await checkDatabase(),
|
||||
memory: checkMemory(),
|
||||
},
|
||||
};
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
monitor.recordMetric('response_time', responseTime);
|
||||
|
||||
return NextResponse.json(health, { status: 200 });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
status: 'error',
|
||||
timestamp: new Date().toISOString(),
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
},
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function checkDatabase(): Promise<{ status: string; latency?: number }> {
|
||||
try {
|
||||
const start = Date.now();
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
latency: Date.now() - start,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'error',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function checkMemory(): { status: string; usage: number } {
|
||||
const memUsage = process.memoryUsage();
|
||||
const heapUsedMB = memUsage.heapUsed / 1024 / 1024;
|
||||
const heapTotalMB = memUsage.heapTotal / 1024 / 1024;
|
||||
const usagePercent = (heapUsedMB / heapTotalMB) * 100;
|
||||
|
||||
return {
|
||||
status: usagePercent > 90 ? 'warning' : 'ok',
|
||||
usage: Math.round(usagePercent),
|
||||
};
|
||||
}
|
||||
@@ -7,6 +7,9 @@ import { OrganizationSchema, WebsiteSchema } from "@/components/seo/structured-d
|
||||
import { MobileTabBar } from "@/components/layout/mobile-tab-bar";
|
||||
import { ErrorBoundary } from "@/components/ui/error-boundary";
|
||||
import { SessionProvider } from "@/providers/session-provider";
|
||||
import { initSentry } from "@/lib/sentry";
|
||||
|
||||
initSentry();
|
||||
|
||||
const geistSans = Geist({
|
||||
variable: "--font-geist-sans",
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
export class PerformanceMonitor {
|
||||
private static instance: PerformanceMonitor;
|
||||
private metrics: Map<string, number[]> = new Map();
|
||||
|
||||
static getInstance(): PerformanceMonitor {
|
||||
if (!PerformanceMonitor.instance) {
|
||||
PerformanceMonitor.instance = new PerformanceMonitor();
|
||||
}
|
||||
return PerformanceMonitor.instance;
|
||||
}
|
||||
|
||||
recordMetric(name: string, value: number) {
|
||||
if (!this.metrics.has(name)) {
|
||||
this.metrics.set(name, []);
|
||||
}
|
||||
this.metrics.get(name)!.push(value);
|
||||
|
||||
if (this.metrics.get(name)!.length > 1000) {
|
||||
this.metrics.get(name)!.shift();
|
||||
}
|
||||
}
|
||||
|
||||
getAverage(name: string): number {
|
||||
const values = this.metrics.get(name) || [];
|
||||
if (values.length === 0) return 0;
|
||||
return values.reduce((a, b) => a + b, 0) / values.length;
|
||||
}
|
||||
|
||||
getPercentile(name: string, percentile: number): number {
|
||||
const values = this.metrics.get(name) || [];
|
||||
if (values.length === 0) return 0;
|
||||
|
||||
const sorted = [...values].sort((a, b) => a - b);
|
||||
const index = Math.ceil((percentile / 100) * sorted.length) - 1;
|
||||
return sorted[Math.max(0, index)];
|
||||
}
|
||||
|
||||
getCount(name: string): number {
|
||||
return this.metrics.get(name)?.length || 0;
|
||||
}
|
||||
|
||||
getMin(name: string): number {
|
||||
const values = this.metrics.get(name) || [];
|
||||
if (values.length === 0) return 0;
|
||||
return Math.min(...values);
|
||||
}
|
||||
|
||||
getMax(name: string): number {
|
||||
const values = this.metrics.get(name) || [];
|
||||
if (values.length === 0) return 0;
|
||||
return Math.max(...values);
|
||||
}
|
||||
|
||||
getStats(name: string) {
|
||||
return {
|
||||
count: this.getCount(name),
|
||||
avg: this.getAverage(name),
|
||||
min: this.getMin(name),
|
||||
max: this.getMax(name),
|
||||
p50: this.getPercentile(name, 50),
|
||||
p95: this.getPercentile(name, 95),
|
||||
p99: this.getPercentile(name, 99),
|
||||
};
|
||||
}
|
||||
|
||||
clearMetrics(name?: string) {
|
||||
if (name) {
|
||||
this.metrics.delete(name);
|
||||
} else {
|
||||
this.metrics.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const monitor = PerformanceMonitor.getInstance();
|
||||
@@ -0,0 +1,13 @@
|
||||
import * as Sentry from '@sentry/nextjs';
|
||||
|
||||
export function initSentry() {
|
||||
if (process.env.NODE_ENV === 'production' && process.env.NEXT_PUBLIC_SENTRY_DSN) {
|
||||
Sentry.init({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
environment: process.env.NODE_ENV,
|
||||
tracesSampleRate: 0.1,
|
||||
replaysSessionSampleRate: 0.1,
|
||||
replaysOnErrorSampleRate: 1.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user