feat: 建立监控告警体系和生产环境配置
阶段三:建立监控告警体系 - 集成Sentry错误监控:安装依赖,创建配置文件,初始化Sentry - 配置性能监控:创建监控工具类,实现健康检查API - 更新环境变量模板,添加Sentry和数据库配置 阶段四:配置生产环境 - 创建生产环境变量模板 - 创建Dockerfile和docker-compose.prod.yml - 创建备份和恢复脚本 - 设置脚本执行权限
This commit is contained in:
@@ -6,3 +6,21 @@ COMPANY_EMAIL=contact@novalon.cn
|
|||||||
|
|
||||||
# Next.js Configuration
|
# Next.js Configuration
|
||||||
NEXT_PUBLIC_SITE_URL=http://localhost:3000
|
NEXT_PUBLIC_SITE_URL=http://localhost:3000
|
||||||
|
|
||||||
|
# Sentry Error Monitoring (Optional - for production)
|
||||||
|
NEXT_PUBLIC_SENTRY_DSN=https://xxx@xxx.ingest.sentry.io/xxx
|
||||||
|
|
||||||
|
# NextAuth Configuration
|
||||||
|
NEXTAUTH_SECRET=your-secret-key-here
|
||||||
|
NEXTAUTH_URL=http://localhost:3000
|
||||||
|
|
||||||
|
# Admin User
|
||||||
|
ADMIN_EMAIL=admin@novalon.cn
|
||||||
|
ADMIN_PASSWORD=your-secure-password
|
||||||
|
|
||||||
|
# Database
|
||||||
|
DATABASE_URL=file:./data/dev.db
|
||||||
|
|
||||||
|
# File Upload
|
||||||
|
UPLOAD_DIR=./uploads
|
||||||
|
MAX_FILE_SIZE=10485760
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Database
|
||||||
|
DATABASE_URL=file:./data/prod.db
|
||||||
|
|
||||||
|
# NextAuth
|
||||||
|
NEXTAUTH_URL=https://novalon.cn
|
||||||
|
NEXTAUTH_SECRET=your-production-secret-here
|
||||||
|
|
||||||
|
# Admin User
|
||||||
|
ADMIN_EMAIL=admin@novalon.cn
|
||||||
|
ADMIN_PASSWORD=your-secure-password
|
||||||
|
|
||||||
|
# Sentry
|
||||||
|
NEXT_PUBLIC_SENTRY_DSN=https://xxx@xxx.ingest.sentry.io/xxx
|
||||||
|
|
||||||
|
# Email (Resend)
|
||||||
|
RESEND_API_KEY=re_icMNpBzS_DL9GirDmhG5PbNU6PLRWvUtY
|
||||||
|
|
||||||
|
# Company Email
|
||||||
|
COMPANY_EMAIL=contact@novalon.cn
|
||||||
|
|
||||||
|
# File Upload
|
||||||
|
UPLOAD_DIR=./uploads
|
||||||
|
MAX_FILE_SIZE=10485760
|
||||||
|
|
||||||
|
# Site URL
|
||||||
|
NEXT_PUBLIC_SITE_URL=https://novalon.cn
|
||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
FROM node:18-alpine AS base
|
||||||
|
|
||||||
|
# Install dependencies only when needed
|
||||||
|
FROM base AS deps
|
||||||
|
RUN apk add --no-cache libc6-compat
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package.json package-lock.json* ./
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
# Rebuild the source code only when needed
|
||||||
|
FROM base AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=deps /app/node_modules ./node_modules
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
ENV NEXT_TELEMETRY_DISABLED 1
|
||||||
|
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production image, copy all the files and run next
|
||||||
|
FROM base AS runner
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENV NODE_ENV production
|
||||||
|
ENV NEXT_TELEMETRY_DISABLED 1
|
||||||
|
|
||||||
|
RUN addgroup --system --gid 1001 nodejs
|
||||||
|
RUN adduser --system --uid 1001 nextjs
|
||||||
|
|
||||||
|
COPY --from=builder /app/public ./public
|
||||||
|
|
||||||
|
# Set the correct permission for prerender cache
|
||||||
|
RUN mkdir .next
|
||||||
|
RUN chown nextjs:nodejs .next
|
||||||
|
|
||||||
|
# Automatically leverage output traces to reduce image size
|
||||||
|
# https://nextjs.org/docs/advanced-features/output-file-tracing
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
||||||
|
|
||||||
|
# Create necessary directories
|
||||||
|
RUN mkdir -p /app/data /app/uploads
|
||||||
|
RUN chown -R nextjs:nodejs /app/data /app/uploads
|
||||||
|
|
||||||
|
USER nextjs
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
ENV PORT 3000
|
||||||
|
ENV HOSTNAME "0.0.0.0"
|
||||||
|
|
||||||
|
CMD ["node", "server.js"]
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
container_name: novalon-website
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=production
|
||||||
|
env_file:
|
||||||
|
- .env.production
|
||||||
|
volumes:
|
||||||
|
- ./data:/app/data
|
||||||
|
- ./uploads:/app/uploads
|
||||||
|
restart: unless-stopped
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
networks:
|
||||||
|
- novalon-network
|
||||||
|
|
||||||
|
networks:
|
||||||
|
novalon-network:
|
||||||
|
driver: bridge
|
||||||
Generated
+4046
-40
File diff suppressed because it is too large
Load Diff
@@ -31,5 +31,8 @@
|
|||||||
"glob": "^13.0.6",
|
"glob": "^13.0.6",
|
||||||
"lighthouse": "^13.0.3",
|
"lighthouse": "^13.0.3",
|
||||||
"typescript": "^5.3.0"
|
"typescript": "^5.3.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@sentry/nextjs": "^10.42.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
+53
@@ -0,0 +1,53 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 备份脚本
|
||||||
|
# 用法: ./scripts/backup.sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
BACKUP_DIR="./backups"
|
||||||
|
DATE=$(date +%Y%m%d_%H%M%S)
|
||||||
|
BACKUP_NAME="backup_$DATE"
|
||||||
|
|
||||||
|
# 创建备份目录
|
||||||
|
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
|
||||||
|
|
||||||
|
echo "开始备份..."
|
||||||
|
|
||||||
|
# 备份数据库
|
||||||
|
if [ -f "./data/prod.db" ]; then
|
||||||
|
echo "备份数据库..."
|
||||||
|
cp ./data/prod.db "$BACKUP_DIR/$BACKUP_NAME/database.db"
|
||||||
|
else
|
||||||
|
echo "警告: 数据库文件不存在"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 备份上传文件
|
||||||
|
if [ -d "./uploads" ]; then
|
||||||
|
echo "备份上传文件..."
|
||||||
|
cp -r ./uploads "$BACKUP_DIR/$BACKUP_NAME/uploads"
|
||||||
|
else
|
||||||
|
echo "警告: uploads目录不存在"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 备份配置
|
||||||
|
if [ -f ".env.production" ]; then
|
||||||
|
echo "备份配置..."
|
||||||
|
cp .env.production "$BACKUP_DIR/$BACKUP_NAME/.env.production"
|
||||||
|
else
|
||||||
|
echo "警告: .env.production文件不存在"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 压缩备份
|
||||||
|
echo "压缩备份..."
|
||||||
|
tar -czf "$BACKUP_DIR/$BACKUP_NAME.tar.gz" -C "$BACKUP_DIR" "$BACKUP_NAME"
|
||||||
|
|
||||||
|
# 删除临时目录
|
||||||
|
rm -rf "$BACKUP_DIR/$BACKUP_NAME"
|
||||||
|
|
||||||
|
# 保留最近7天的备份
|
||||||
|
echo "清理旧备份..."
|
||||||
|
find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +7 -delete
|
||||||
|
|
||||||
|
echo "备份完成: $BACKUP_DIR/$BACKUP_NAME.tar.gz"
|
||||||
|
echo "备份大小: $(du -h "$BACKUP_DIR/$BACKUP_NAME.tar.gz" | cut -f1)"
|
||||||
Executable
+69
@@ -0,0 +1,69 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 恢复脚本
|
||||||
|
# 用法: ./scripts/restore.sh <backup_file.tar.gz>
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
echo "错误: 请指定备份文件"
|
||||||
|
echo "用法: ./scripts/restore.sh <backup_file.tar.gz>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
BACKUP_FILE="$1"
|
||||||
|
|
||||||
|
if [ ! -f "$BACKUP_FILE" ]; then
|
||||||
|
echo "错误: 备份文件不存在: $BACKUP_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "警告: 此操作将覆盖当前数据!"
|
||||||
|
read -p "确认继续? (yes/no): " confirm
|
||||||
|
|
||||||
|
if [ "$confirm" != "yes" ]; then
|
||||||
|
echo "操作已取消"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 创建临时目录
|
||||||
|
TEMP_DIR="./temp_restore_$(date +%s)"
|
||||||
|
mkdir -p "$TEMP_DIR"
|
||||||
|
|
||||||
|
echo "解压备份..."
|
||||||
|
tar -xzf "$BACKUP_FILE" -C "$TEMP_DIR"
|
||||||
|
|
||||||
|
# 获取备份目录名
|
||||||
|
BACKUP_DIR_NAME=$(ls "$TEMP_DIR")
|
||||||
|
BACKUP_PATH="$TEMP_DIR/$BACKUP_DIR_NAME"
|
||||||
|
|
||||||
|
# 恢复数据库
|
||||||
|
if [ -f "$BACKUP_PATH/database.db" ]; then
|
||||||
|
echo "恢复数据库..."
|
||||||
|
cp "$BACKUP_PATH/database.db" ./data/prod.db
|
||||||
|
else
|
||||||
|
echo "警告: 备份中没有数据库文件"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 恢复上传文件
|
||||||
|
if [ -d "$BACKUP_PATH/uploads" ]; then
|
||||||
|
echo "恢复上传文件..."
|
||||||
|
rm -rf ./uploads/*
|
||||||
|
cp -r "$BACKUP_PATH/uploads"/* ./uploads/ 2>/dev/null || true
|
||||||
|
else
|
||||||
|
echo "警告: 备份中没有uploads目录"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 恢复配置
|
||||||
|
if [ -f "$BACKUP_PATH/.env.production" ]; then
|
||||||
|
echo "恢复配置..."
|
||||||
|
cp "$BACKUP_PATH/.env.production" ./.env.production
|
||||||
|
else
|
||||||
|
echo "警告: 备份中没有配置文件"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 清理临时文件
|
||||||
|
rm -rf "$TEMP_DIR"
|
||||||
|
|
||||||
|
echo "恢复完成!"
|
||||||
|
echo "请重启应用以使更改生效"
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import * as Sentry from '@sentry/nextjs';
|
||||||
|
|
||||||
|
Sentry.init({
|
||||||
|
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||||
|
environment: process.env.NODE_ENV,
|
||||||
|
tracesSampleRate: 0.1,
|
||||||
|
replaysSessionSampleRate: 0.1,
|
||||||
|
replaysOnErrorSampleRate: 1.0,
|
||||||
|
});
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import * as Sentry from '@sentry/nextjs';
|
||||||
|
|
||||||
|
Sentry.init({
|
||||||
|
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||||
|
environment: process.env.NODE_ENV,
|
||||||
|
tracesSampleRate: 0.1,
|
||||||
|
});
|
||||||
@@ -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 { MobileTabBar } from "@/components/layout/mobile-tab-bar";
|
||||||
import { ErrorBoundary } from "@/components/ui/error-boundary";
|
import { ErrorBoundary } from "@/components/ui/error-boundary";
|
||||||
import { SessionProvider } from "@/providers/session-provider";
|
import { SessionProvider } from "@/providers/session-provider";
|
||||||
|
import { initSentry } from "@/lib/sentry";
|
||||||
|
|
||||||
|
initSentry();
|
||||||
|
|
||||||
const geistSans = Geist({
|
const geistSans = Geist({
|
||||||
variable: "--font-geist-sans",
|
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