fix(metrics): sync Prometheus zhiyi_total_memories from LanceDB stats

- Stats handler now updates TotalMemories/TotalEpisodes gauges on each call
- Startup goroutine initializes metrics 1s after boot (waits for Rust sidecar)
- Fixes zhiyi_total_memories showing 0 instead of actual count
This commit is contained in:
xiaowei 2026-05-30 17:39:38 +08:00
parent daabd54649
commit 05c4a2d32e
2 changed files with 22 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/xiaoxue/memoryweave/internal/metrics"
"github.com/xiaoxue/memoryweave/internal/models"
"github.com/xiaoxue/memoryweave/internal/selfoptimize"
"github.com/xiaoxue/memoryweave/internal/storage"
@ -384,6 +385,13 @@ func (a *API) Stats(w http.ResponseWriter, r *http.Request) {
respondError(w, 500, "stats failed: "+err.Error())
return
}
// 同步 Prometheus 指标
if total, ok := s["total_memories"].(float64); ok {
metrics.TotalMemories.Set(total)
}
if total, ok := s["total_episodes"].(float64); ok {
metrics.TotalEpisodes.Set(total)
}
respond(w, 200, s)
}

View File

@ -76,6 +76,20 @@ func NewServer() http.Handler {
}
api := routes.NewAPI(ldb, emb, rerank)
// 启动时初始化 Prometheus 指标
go func() {
time.Sleep(1 * time.Second) // 等待 Rust sidecar 就绪
if stats, err := ldb.Stats(); err == nil {
if total, ok := stats["total_memories"].(float64); ok {
metrics.TotalMemories.Set(total)
}
if total, ok := stats["total_episodes"].(float64); ok {
metrics.TotalEpisodes.Set(total)
}
log.Printf("[metrics] 初始化指标: memories=%.0f", stats["total_memories"])
}
}()
// 图谱SQLite (graph_nodes/graph_edges) — 设计要求,非 FileGraph JSON
graphPath := os.Getenv("GRAPH_PATH")
if graphPath == "" {