From 05c4a2d32ec7cbbf402d97a8b7a86c84a268a2a9 Mon Sep 17 00:00:00 2001 From: xiaowei Date: Sat, 30 May 2026 17:39:38 +0800 Subject: [PATCH] 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 --- go/internal/api/routes/core.go | 8 ++++++++ go/internal/api/server.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/go/internal/api/routes/core.go b/go/internal/api/routes/core.go index ca9dc43..06c70cf 100644 --- a/go/internal/api/routes/core.go +++ b/go/internal/api/routes/core.go @@ -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) } diff --git a/go/internal/api/server.go b/go/internal/api/server.go index f3e3aba..3af5482 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -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 == "" {