mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(metrics): enhance initialization process in metrics collector
- Improved the initialization sequence by clearly defining steps for cache, monitoring, and object pool setup. - Enabled Feishu webhook alerts for real-time notifications based on configuration. - Ensured last save time is set during initialization to maintain accurate data persistence. - Streamlined historical data loading process to enhance metrics accuracy. These changes improve the organization and functionality of the metrics collector, ensuring better performance and alerting capabilities.
This commit is contained in:
parent
c71da4c1fe
commit
14843301f7
@ -71,13 +71,31 @@ func InitCollector(dbPath string, config *config.Config) error {
|
|||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先初始化 cache
|
// 1. 先初始化 cache
|
||||||
globalCollector.cache = cache.NewCache(constants.CacheTTL)
|
globalCollector.cache = cache.NewCache(constants.CacheTTL)
|
||||||
|
|
||||||
// 在初始化时设置最后保存时间
|
// 2. 初始化监控器
|
||||||
|
globalCollector.monitor = monitor.NewMonitor(globalCollector)
|
||||||
|
|
||||||
|
// 3. 如果配置了飞书webhook,则启用飞书告警
|
||||||
|
if config.Metrics.FeishuWebhook != "" {
|
||||||
|
globalCollector.monitor.AddHandler(
|
||||||
|
monitor.NewFeishuHandler(config.Metrics.FeishuWebhook),
|
||||||
|
)
|
||||||
|
log.Printf("Feishu alert enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 初始化对象池
|
||||||
|
globalCollector.statsPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return make(map[string]interface{}, 20)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 设置最后保存时间
|
||||||
lastSaveTime = time.Now()
|
lastSaveTime = time.Now()
|
||||||
|
|
||||||
// 加载历史数据
|
// 6. 加载历史数据
|
||||||
if lastMetrics, err := db.GetLastMetrics(); err == nil && lastMetrics != nil {
|
if lastMetrics, err := db.GetLastMetrics(); err == nil && lastMetrics != nil {
|
||||||
globalCollector.persistentStats.totalRequests.Store(lastMetrics.TotalRequests)
|
globalCollector.persistentStats.totalRequests.Store(lastMetrics.TotalRequests)
|
||||||
globalCollector.persistentStats.totalErrors.Store(lastMetrics.TotalErrors)
|
globalCollector.persistentStats.totalErrors.Store(lastMetrics.TotalErrors)
|
||||||
@ -96,25 +114,7 @@ func InitCollector(dbPath string, config *config.Config) error {
|
|||||||
lastMetrics.TotalRequests, lastMetrics.TotalErrors, lastMetrics.TotalBytes)
|
lastMetrics.TotalRequests, lastMetrics.TotalErrors, lastMetrics.TotalBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化监控器
|
// 7. 启动定时保存
|
||||||
globalCollector.monitor = monitor.NewMonitor(globalCollector)
|
|
||||||
|
|
||||||
// 如果配置了飞书webhook,则启用飞书告警
|
|
||||||
if config.Metrics.FeishuWebhook != "" {
|
|
||||||
globalCollector.monitor.AddHandler(
|
|
||||||
monitor.NewFeishuHandler(config.Metrics.FeishuWebhook),
|
|
||||||
)
|
|
||||||
log.Printf("Feishu alert enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化对象池
|
|
||||||
globalCollector.statsPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return make(map[string]interface{}, 20)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动定时保存
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Duration(rand.Int63n(60)) * time.Second)
|
time.Sleep(time.Duration(rand.Int63n(60)) * time.Second)
|
||||||
var (
|
var (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user