-
Notifications
You must be signed in to change notification settings - Fork 11.3k
feat(performance): implement system performance monitoring #2835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package common | ||
|
|
||
| import "sync/atomic" | ||
|
|
||
| // PerformanceMonitorConfig 性能监控配置 | ||
| type PerformanceMonitorConfig struct { | ||
| Enabled bool | ||
| CPUThreshold int | ||
| MemoryThreshold int | ||
| DiskThreshold int | ||
| } | ||
|
|
||
| var performanceMonitorConfig atomic.Value | ||
|
|
||
| func init() { | ||
| // 初始化默认配置 | ||
| performanceMonitorConfig.Store(PerformanceMonitorConfig{ | ||
| Enabled: true, | ||
| CPUThreshold: 90, | ||
| MemoryThreshold: 90, | ||
| DiskThreshold: 90, | ||
| }) | ||
| } | ||
|
|
||
| // GetPerformanceMonitorConfig 获取性能监控配置 | ||
| func GetPerformanceMonitorConfig() PerformanceMonitorConfig { | ||
| return performanceMonitorConfig.Load().(PerformanceMonitorConfig) | ||
| } | ||
|
|
||
| // SetPerformanceMonitorConfig 设置性能监控配置 | ||
| func SetPerformanceMonitorConfig(config PerformanceMonitorConfig) { | ||
| performanceMonitorConfig.Store(config) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/shirou/gopsutil/cpu" | ||
| "github.com/shirou/gopsutil/mem" | ||
| ) | ||
|
|
||
| // DiskSpaceInfo 磁盘空间信息 | ||
| type DiskSpaceInfo struct { | ||
| // 总空间(字节) | ||
| Total uint64 `json:"total"` | ||
| // 可用空间(字节) | ||
| Free uint64 `json:"free"` | ||
| // 已用空间(字节) | ||
| Used uint64 `json:"used"` | ||
| // 使用百分比 | ||
| UsedPercent float64 `json:"used_percent"` | ||
| } | ||
|
|
||
| // SystemStatus 系统状态信息 | ||
| type SystemStatus struct { | ||
| CPUUsage float64 | ||
| MemoryUsage float64 | ||
| DiskUsage float64 | ||
| } | ||
|
|
||
| var latestSystemStatus atomic.Value | ||
|
|
||
| func init() { | ||
| latestSystemStatus.Store(SystemStatus{}) | ||
| } | ||
|
|
||
| // StartSystemMonitor 启动系统监控 | ||
| func StartSystemMonitor() { | ||
| go func() { | ||
| for { | ||
| config := GetPerformanceMonitorConfig() | ||
| if !config.Enabled { | ||
| time.Sleep(30 * time.Second) | ||
| continue | ||
| } | ||
|
|
||
| updateSystemStatus() | ||
| time.Sleep(5 * time.Second) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func updateSystemStatus() { | ||
| var status SystemStatus | ||
|
|
||
| // CPU | ||
| // 注意:cpu.Percent(0, false) 返回自上次调用以来的 CPU 使用率 | ||
| // 如果是第一次调用,可能会返回错误或不准确的值,但在循环中会逐渐正常 | ||
| percents, err := cpu.Percent(0, false) | ||
| if err == nil && len(percents) > 0 { | ||
| status.CPUUsage = percents[0] | ||
| } | ||
|
|
||
| // Memory | ||
| memInfo, err := mem.VirtualMemory() | ||
| if err == nil { | ||
| status.MemoryUsage = memInfo.UsedPercent | ||
| } | ||
|
|
||
| // Disk | ||
| diskInfo := GetDiskSpaceInfo() | ||
| if diskInfo.Total > 0 { | ||
| status.DiskUsage = diskInfo.UsedPercent | ||
| } | ||
|
|
||
| latestSystemStatus.Store(status) | ||
| } | ||
|
|
||
| // GetSystemStatus 获取当前系统状态 | ||
| func GetSystemStatus() SystemStatus { | ||
| return latestSystemStatus.Load().(SystemStatus) | ||
| } |
9 changes: 4 additions & 5 deletions
9
controller/performance_unix.go → common/system_monitor_unix.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
controller/performance_windows.go → common/system_monitor_windows.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,5 +274,9 @@ func InitResources() error { | |
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // 启动系统监控 | ||
| common.StartSystemMonitor() | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/types" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // SystemPerformanceCheck 检查系统性能中间件 | ||
| func SystemPerformanceCheck() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| // 仅检查 Relay 接口 (/v1, /v1beta 等) | ||
| // 这里简单判断路径前缀,可以根据实际路由调整 | ||
| path := c.Request.URL.Path | ||
| if strings.HasPrefix(path, "/v1/messages") { | ||
| if err := checkSystemPerformance(); err != nil { | ||
| c.JSON(err.StatusCode, gin.H{ | ||
| "error": err.ToClaudeError(), | ||
| }) | ||
| c.Abort() | ||
| return | ||
| } | ||
| } else { | ||
| if err := checkSystemPerformance(); err != nil { | ||
| c.JSON(err.StatusCode, gin.H{ | ||
| "error": err.ToOpenAIError(), | ||
| }) | ||
| c.Abort() | ||
| return | ||
| } | ||
| } | ||
| c.Next() | ||
| } | ||
| } | ||
|
|
||
| // checkSystemPerformance 检查系统性能是否超过阈值 | ||
| func checkSystemPerformance() *types.NewAPIError { | ||
| config := common.GetPerformanceMonitorConfig() | ||
| if !config.Enabled { | ||
| return nil | ||
| } | ||
|
|
||
| status := common.GetSystemStatus() | ||
|
|
||
| // 检查 CPU | ||
| if config.CPUThreshold > 0 && int(status.CPUUsage) > config.CPUThreshold { | ||
| return types.NewErrorWithStatusCode(errors.New("system cpu overloaded"), "system_cpu_overloaded", http.StatusServiceUnavailable) | ||
| } | ||
|
|
||
| // 检查内存 | ||
| if config.MemoryThreshold > 0 && int(status.MemoryUsage) > config.MemoryThreshold { | ||
| return types.NewErrorWithStatusCode(errors.New("system memory overloaded"), "system_memory_overloaded", http.StatusServiceUnavailable) | ||
| } | ||
|
|
||
| // 检查磁盘 | ||
| if config.DiskThreshold > 0 && int(status.DiskUsage) > config.DiskThreshold { | ||
| return types.NewErrorWithStatusCode(errors.New("system disk overloaded"), "system_disk_overloaded", http.StatusServiceUnavailable) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 233
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 469
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 7205
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 5987
Fix float-to-int truncation in threshold checks.
The
status.CPUUsage,status.MemoryUsage, andstatus.DiskUsagefields arefloat64, while the threshold config values areint. Casting tointtruncates (e.g., 90.9 → 90), allowing values just below the integer threshold to bypass the check. Compare as floats instead:📝 Committable suggestion
🤖 Prompt for AI Agents