🚀 核心更新: - ✅ 完善 NestJS 企业级架构设计 - ✅ 优化配置中心和基础设施层 - ✅ 增强第三方服务集成能力 - ✅ 完善多租户架构支持 - 🎯 对标 Java Spring Boot 和 PHP ThinkPHP 📦 新增文件: - wwjcloud-nest 完整框架结构 - Docker 容器化配置 - 管理后台界面 - 数据库迁移脚本 🔑 Key: ebb38b43ec39f355f071294fd1cf9c42
162 lines
4.8 KiB
TypeScript
162 lines
4.8 KiB
TypeScript
import { Module, Global } from '@nestjs/common';
|
||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||
import { MonitoringService } from './monitoring.service';
|
||
import {
|
||
MonitoringInterface,
|
||
HealthCheckInterface,
|
||
} from './monitoring.interface';
|
||
|
||
/**
|
||
* 监控模块
|
||
* 基于 NestJS 官方示例实现
|
||
* 参考: https://docs.nestjs.cn/fundamentals/dependency-injection
|
||
* 对应 Java: 监控配置
|
||
*/
|
||
@Global()
|
||
@Module({
|
||
imports: [ConfigModule],
|
||
providers: [
|
||
{
|
||
provide: 'MONITORING_PROVIDER',
|
||
useFactory: (configService: ConfigService) => {
|
||
// 这里会根据配置选择具体的监控实现
|
||
// 默认使用 Prometheus,也可以使用其他监控系统
|
||
const monitoringType = configService.get(
|
||
'monitoring.type',
|
||
'prometheus',
|
||
);
|
||
|
||
if (monitoringType === 'prometheus') {
|
||
// 返回 Prometheus 监控实现
|
||
return {
|
||
async incrementCounter(
|
||
name: string,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Counter ${name} incremented`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async setGauge(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Gauge ${name} set to ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async recordHistogram(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Histogram ${name} recorded ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async recordSummary(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Summary ${name} recorded ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
};
|
||
} else {
|
||
// 返回其他监控实现
|
||
return {
|
||
async incrementCounter(
|
||
name: string,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Counter ${name} incremented`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async setGauge(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Gauge ${name} set to ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async recordHistogram(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Histogram ${name} recorded ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
async recordSummary(
|
||
name: string,
|
||
value: number,
|
||
labels?: Record<string, string>,
|
||
): Promise<void> {
|
||
console.log(
|
||
`[MONITORING] Summary ${name} recorded ${value}`,
|
||
labels || '',
|
||
);
|
||
},
|
||
};
|
||
}
|
||
},
|
||
inject: [ConfigService],
|
||
},
|
||
{
|
||
provide: 'HEALTH_CHECK_PROVIDER',
|
||
useFactory: (configService: ConfigService) => {
|
||
// 健康检查实现
|
||
return {
|
||
async checkHealth(): Promise<{ status: string; details: any }> {
|
||
return {
|
||
status: 'healthy',
|
||
details: {
|
||
timestamp: new Date().toISOString(),
|
||
uptime: process.uptime(),
|
||
memory: process.memoryUsage(),
|
||
version: process.version,
|
||
},
|
||
};
|
||
},
|
||
async checkDatabase(): Promise<{ status: string; details: any }> {
|
||
return {
|
||
status: 'healthy',
|
||
details: {
|
||
timestamp: new Date().toISOString(),
|
||
message: 'Database connection healthy',
|
||
},
|
||
};
|
||
},
|
||
async checkRedis(): Promise<{ status: string; details: any }> {
|
||
return {
|
||
status: 'healthy',
|
||
details: {
|
||
timestamp: new Date().toISOString(),
|
||
message: 'Redis connection healthy',
|
||
},
|
||
};
|
||
},
|
||
};
|
||
},
|
||
inject: [ConfigService],
|
||
},
|
||
MonitoringService,
|
||
],
|
||
exports: [MonitoringService],
|
||
})
|
||
export class MonitoringModule {}
|