Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type Config struct {
// (issue #41:截断的 JSON 让上游 unmarshal 报 unexpected EOF,网关却罚号)。
// 0/负数视为非法 → normalize 回落默认并记录。
MaxBodyMB int `json:"max_body_mb"`

// MetricsEnabled 是否采集按模型的请求统计(默认 true)。
MetricsEnabled bool `json:"metrics_enabled"`
// MetricsFile 统计持久化文件;空 = 纯内存(重启清零)。
// 默认 ./data/metrics.json,重启后累计值不丢。
MetricsFile string `json:"metrics_file"`
} `json:"server"`

Cooldown struct {
Expand Down Expand Up @@ -152,6 +158,8 @@ func Default() *Config {
c.Cooldown.SoftRate = "600s"
c.Cooldown.SoftRateMax = "2h"
c.Server.MaxBodyMB = 8 // 请求体上限默认 8MB
c.Server.MetricsEnabled = true
c.Server.MetricsFile = "./data/metrics.json"
// 排程段默认值由 internal/config 集中维护(cmd/server 与 cmd/activity 共用,
// 消除 issue #49 的默认值漂移)。
c.Schedule = config.DefaultSchedule()
Expand Down
8 changes: 8 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"workbuddy2api/internal/auth"
"workbuddy2api/internal/metrics"
"workbuddy2api/internal/pool"
"workbuddy2api/internal/redisstore"
"workbuddy2api/internal/scheduler"
Expand Down Expand Up @@ -171,7 +172,14 @@ func main() {
log.Printf("夜猫子任务已启用:%v 点(task_runner.py ALL --yes --only black_cat)", cfg.Schedule.CatHours)
}

var metricsCollector *metrics.Collector
if cfg.Server.MetricsEnabled {
metricsCollector = metrics.New(cfg.Server.MetricsFile)
defer metricsCollector.Flush() // 退出前落盘
}

h := server.NewHandler(server.Config{
Metrics: metricsCollector,
Pool: p,
Upstream: up,
APIKey: cfg.APIKey,
Expand Down
Loading
Loading